14 KiB
DGR-032 evidence — deterministic fake ShardEngine
Completed: 2026-07-23
Branch: ralph/distributed-gguf-runtime
Authority: .scratch/distributed-gguf-runtime/prd.json
Dependencies: DGR-031 (evidence/DGR-031/README.md — the project-owned
ShardEngine abstract contract, tests/shard_engine_contract.py's
assert_shard_engine_contract, and its own dependency-handoff note that
DGR-032 should "subclass ShardEngine, add delay/memory-pressure/malformed-
output/crash injection, and pass the same assert_shard_engine_contract
... — no new contract vocabulary should be needed").
Objective
Provide an engine fixture that deterministically transforms typed boundary bundles and session state: head/middle/tail, prefill/decode, cancellation, release, isolated per-session epoch state, deterministic cache-miss/stale- epoch failures, and configurable delay/memory-pressure/malformed-output/ crash-injection fault surfaces — all without llama.cpp, a GPU, or any I/O.
What was found live before changing code
packages/node/meshnet_node/shard_engine.py(DGR-031): the abstractShardEnginewith eight operations (load,capabilities,prefill,decode,cancel,release,health,metrics) and its project-owned dataclasses (LoadRequest,EngineCapabilities,PrefillRequest/DecodeRequest,StepResult,BoundaryBundle/EngineTensor,TokenOutput,HealthResult,MetricsResult).tests/shard_engine_contract.py(DGR-031): the reusableassert_shard_engine_contract(make_engine)helper — nine lifecycle checks any implementation must pass, explicitly designed to be imported by DGR-032 and DGR-037 against their own engines.tests/test_shard_engine.py(DGR-031): its_ReferenceEngineis explicitly documented as not the DGR-032 fixture ("no delay/memory- pressure/malformed/crash injection... that is a separate, larger story") — confirming this story starts from nothing, not inherited credit.grep -rn -i "fakeshardengine\|fake_shard_engine"across.py/.mdreturned no prior matches — no fake engine existed before this story.- No file in
packages/node/meshnet_node/wires aShardEngineintoshard_runtime_server.pyyet (confirmed by grep forShardEngine/shard_enginein that file — no matches); that wiring is DGR-037's scope, so this fixture is a standalone, importable engine only.
Live verification of the pre-existing dependency contract before adding new code:
PYTHONPATH=packages/node:packages/tracker .venv/bin/python3 -m pytest -q tests/test_shard_engine.py
12 passed in 0.13s
What was added (this story's change)
packages/node/meshnet_node/fake_shard_engine.py (new)
FakeShardEngine(ShardEngine) — a pure-Python, deterministic fixture:
- Determinism. Every
prefill/decodeoutput isSHA-256(seed_bytes + idempotency_step), whereseed_bytesis derived fromtoken_ids(head) or the inputBoundaryBundle's tensor bytes plus anytoken_id_sideband(middle/tail-in). Replaying identical inputs on a brand-new session produces byte-identical output — proven byassert_shard_engine_contract's own determinism check and reused directly. - Head/middle/tail. Tail shards (
shard_end >= total_layers - 1) return aTokenOutputsampled into[0, TOKEN_ID_VOCAB_SIZE); head/middle shards return aBoundaryBundletaggedboundary_point="post_head_residual"or"post_middle_residual"respectively, so the three cases are distinguishable in fixture output, not just in the load request. A middle shard'stoken_id_sidebandpasses through unchanged from its input bundle to its output bundle (the V4 first-three-hash-routed-layers requirement RALPH-CONTEXT documents), never invented or dropped. - Isolated session/epoch state.
_sessions: dict[str, _SessionState]keyed bysession_id; each session tracks its ownepoch/cancelledflag. A stale epoch, cancel, or release on one session never touches another's state (test_session_state_is_isolated_between_two_concurrent_sessionsproves a stale-epoch rejection and a cancel on session"a"leave session"b"fully serviceable). Decoding an unopened session is a deterministicNOT_FOUND/CacheResult.MISS, not an exception. - Configurable delay.
FakeShardEngineConfig.step_delay_seconds+ injectablesleephook (defaults totime.sleep, overridable in tests so they don't block wall-clock time) — invoked once perprefill/decodecall before computing the deterministic output. - Configurable memory pressure.
FakeShardEngineConfig.memory_budget_bytes— the engine accumulates_bytes_usedacross every step's seed bytes; once a step would push cumulative usage past the budget, that step deterministically returnsStatusCode.RESOURCE_EXHAUSTED(retryable=True) with no output, instead of computing one. - Configurable malformed output.
FakeShardEngineConfig.malformed_output— when set, the engine still reportsStatusCode.OK(the point is a buggy-but-"successful"-looking response, not a status-coded failure) but the payload is structurally valid, semantically wrong: a tailTokenOutputis pushed pastMALFORMED_TOKEN_ID_FLOOR(outside the fixture's own advertised vocab), and a head/middleBoundaryBundlegets anarchitecturefield prefixed"malformed:"and its tensordatatruncated to one byte — both structurally valid perEngineTensor's andBoundaryBundle's own__post_init__validation (which does not cross-checkdatalength againstshape/dtype), so a consumer must actually check shape/semantics, not just status codes, to catch it. - Configurable crash injection.
FakeShardEngineConfig.crash_after_callscrash_exception_factory— after the configured number ofprefill/decodecalls, the engine raises an arbitrary exception (defaultRuntimeError, injectable) directly out of the call instead of returning aStepResult. This is deliberately not wrapped inEngineError/StructuredStatus: it simulates a whole-process failure (what a worker supervisor — DGR-040 — must catch and restart around), which is a different failure mode from a graceful status-coded rejection.
- Fixture-vs-real marker.
FakeShardEngine.EVIDENCE_CLASS = "fixture"— a structural constant (not just docstring prose) so DGR-036's fixture-vs- real-model parity check can assert programmatically that it is comparing a fixture engine against a real one, never two fixtures. - Every fault-injection knob defaults to off (
0/None/False), so a bareFakeShardEngine()passesassert_shard_engine_contractunmodified — fault injection is opt-in, never a baseline behavior change.
tests/test_fake_shard_engine.py (new)
test_fake_shard_engine_obeys_the_shared_shard_engine_contract— runs the full DGR-031 contract against a bareFakeShardEngine.test_fake_shard_engine_declares_fixture_evidence_class— pins theEVIDENCE_CLASSmarker DGR-036 will rely on.- Head/middle/tail output-shape tests (
boundary_point, token-id-sideband pass-through, tail vocab range). test_session_state_is_isolated_between_two_concurrent_sessions— a stale-epoch rejection and a cancel on one session leave a second, concurrently open session fully serviceable.- One test per fault-injection knob (delay hook invocation, memory-budget
trip, malformed tail/boundary-bundle output, crash-after-N-calls,
configurable crash exception type) plus
FakeShardEngineConfig's own__post_init__validation (negative delay, negative budget, non-positivecrash_after_calls). test_load_result_and_capabilities_report_recipe_architecture— the fixture threadsLoadRequest.recipe["architecture"]through to bothLoadResult.architectureandEngineCapabilities.architecturerather than hardcoding"dense"/"fake"everywhere, so a future V4 recipe is visible in fixture output too.
.scratch/distributed-gguf-runtime/prd.json / issue markdown
Marked DGR-032.passes = true with completionNotes; regenerated
issues/032-implement-deterministic-fake-shardengine.md via
scripts/ralph_prd_schema.py render so it matches prd.json byte-for-byte.
Acceptance criteria → evidence
- Head, middle, tail, prefill, decode, cancellation, release with
deterministic outputs —
FakeShardEngine's_transform, boundary-point tagging, andassert_shard_engine_contract's own determinism/cancel/ release checks. Verified bytest_fake_shard_engine_obeys_the_shared_shard_engine_contract,test_head_shard_returns_boundary_bundle_with_post_head_residual_point,test_middle_shard_returns_boundary_bundle_and_passes_through_token_sideband,test_tail_shard_returns_token_output_within_advertised_vocab. - Isolated session/epoch state and deterministic cache-miss/stale-epoch
failures —
_sessionsdict keyed per session;test_session_state_is_isolated_between_two_concurrent_sessionsplus the shared contract's own cache-miss/stale-epoch checks. - Configurable delay, memory pressure, malformed output, crash
injection —
FakeShardEngineConfig; verified bytest_step_delay_seconds_invokes_the_configured_sleep_hook,test_memory_budget_bytes_trips_deterministic_resource_exhausted,test_malformed_output_is_structurally_valid_but_semantically_wrong_for_tail,test_malformed_output_is_structurally_valid_but_semantically_wrong_for_boundary_bundle,test_crash_after_calls_raises_instead_of_returning_a_structured_status,test_crash_exception_factory_is_configurable,test_config_rejects_invalid_knob_values. - Contract tests distinguish fixture evidence from real-model
certification — module docstring and this README are explicit that
this is FIXTURE evidence only (numeric parity is DGR-036 onward); the
EVIDENCE_CLASS = "fixture"constant makes that distinction structurally checkable, not just prose, pinned bytest_fake_shard_engine_declares_fixture_evidence_class. - Gates + this handoff — below.
Commands and results
PYTHONPATH=packages/node:packages/tracker .venv/bin/python3 -m pytest -q tests/test_fake_shard_engine.py tests/test_shard_engine.py
26 passed in 0.17s
PYTHONPATH=packages/node:packages/tracker .venv/bin/python3 -m pytest -q \
tests/test_fake_shard_engine.py tests/test_shard_engine.py tests/test_shard_lifecycle.py \
tests/test_architecture_boundary.py tests/test_activation_envelope.py \
tests/test_native_shard_protocol.py tests/test_shard_runtime_harness.py
109 passed, 3 skipped in 3.78s
.venv/bin/python3 -m compileall -q packages tests
(no output — clean; exit 0)
git diff --check
(no output — clean)
Limitations
tests/as a whole does not collect cleanly in this environment: the same pre-existing collection errors DGR-031's evidence recorded (missing optional dependencies such ascryptography) are still present and are unrelated to this story. This story's own gates were run as the targeted, scoped test set above per the shared quality gates' wording ("Targeted deterministic tests pass").- This is FIXTURE evidence only.
FakeShardEngineproves lifecycle, session/epoch isolation, and fault-injection semantics; it proves nothing about numerical parity with a real model. That is DGR-036's explicit job once DGR-037's real engine exists, and DGR-053/054 for V4 alpha certification. FakeShardEngineis not wired intoshard_runtime_server.pyor any gRPC surface — it is a standalone, importable engine only. Wiring aShardEngine(fake or real) into the gRPC servicer is DGR-037's scope for the real engine; DGR-033 covers a C++ worker surface, which is a separate native executable, not a consumer of this Python module.- No gRPC/CMake/native-build changes were needed or made — this story is
pure Python fixture code (
evidenceClass: fixture,hardware: none), so the native CMake/CTest and patch-stack gates in the shared quality-gate list do not apply here, consistent with DGR-031's own README recording the same non-applicability.
Dependency handoff
- DGR-033 (standalone fake C++ gRPC Shard worker): its own issue describes a native C++ executable serving the lifecycle/stream RPC contract "using the fake engine" — that is a native analogue, not a consumer of this Python module; DGR-033 should still read this README for the exact deterministic-output/session-isolation/fault-injection semantics its C++ fake engine needs to reproduce so both fakes behave identically from a client's point of view.
- DGR-034/DGR-035 (range-aware GGUF ownership, boundary I/O):
FakeShardEnginealready demonstrates range-driven head/middle/tail behavior purely fromLoadRequest.shard_start/shard_end/total_layers; no new range vocabulary was needed. - DGR-036 (fixture vs real-model parity): compare a
FakeShardEngineinstance'sEVIDENCE_CLASS("fixture") against DGR-037's real engine's equivalent marker (expected"real") to assert the parity check is actually comparing two different implementations; reuseassert_shard_engine_contractagainst both to prove lifecycle parity before attempting numeric parity. - DGR-037 (bind llama.cpp to the worker):
FakeShardEngineis the reference implementation to diff a real engine's lifecycle behavior against — same request/result types, same session/epoch model, no new contract vocabulary. - DGR-040 (worker supervision): the crash-injection knob
(
crash_after_calls/crash_exception_factory) exists specifically so supervision/restart logic has a deterministic way to trigger and test an unhandled engine failure distinct from a gracefulStructuredStatusrejection.