Skip to content

fork: bind-on-activation + registry epoch + ne-guard + fail-open (hydra_vortex #368) - #23

Merged
ddvnguyen merged 2 commits into
hydra-forkfrom
fork/hydra-368-bind-on-activation
Jul 1, 2026
Merged

ddvnguyen merged 2 commits into
hydra-forkfrom
fork/hydra-368-bind-on-activation

Conversation

@ddvnguyen

Copy link
Copy Markdown
Owner

Implements the C++ side of ddvnguyen/hydra_vortex#368 — the forward-compatible slice of the dynamic-quant-swap × COMBINED zero-copy reconciliation design. Builds on the existing llama.cpp#20 zero-copy bind infrastructure (RPC_CMD_RESOLVE_TENSOR + ggml_backend_rpc_register_local_tensor/bind_remote_tensor).

This lands the protocol + binding + fail-open + tests now, in preparation for the actual SWAP_QUANT model free/reload (lands with ggml-org#263). It also fixes ddvnguyen/hydra_vortex#357 (the COMBINED startup race) by binding on demand at SET_EXPERT_MODE("combined") instead of one-shot at startup.

Cross-repo links

What

1. Registry epoch (ggml-rpc.cpp)

  • rpc_msg_resolve_tensor_rsp gets a new uint32_t registry_epoch; the server returns its g_hydra_registry_epoch with every resolve.
  • g_hydra_registry_epoch (atomic<uint32_t>) is bumped on every ggml_backend_rpc_clear_local_tensors() call. A head re-checks ggml_backend_rpc_get_remote_registry_epoch() before each use of a binding; mismatch = stale = the head must rebind or stay solo.

2. ggml_backend_rpc_clear_local_tensors (new public proc)

Clears the local-tensor registry and bumps the epoch. Idempotent. Foreign-buffer clearing is a defense-in-depth follow-up (the head's epoch check is the primary safety).

3. ggml_backend_rpc_get_remote_registry_epoch (new public proc)

Probes the peer's current epoch over RPC (reuses RPC_CMD_RESOLVE_TENSOR with a sentinel name and reads back the epoch field of the response — single round trip). Returns 0 on any failure; callers must treat 0 as "couldn't verify" not "epoch is 0".

4. Fail-open in ggml_backend_rpc_bind_remote_tensor

Replaced RPC_STATUS_ASSERT(status) (which calls GGML_ABORT) with "if (!status) { log; return nullptr; }" — peer drop, malformed response, or transient RPC error must NOT crash the engine. The bind result is still observable: nullptr means "fall back to solo". Folds in review finding #1 from PR #19.

5. ne-guard

ggml_backend_rpc_bind_remote_tensor takes a new const uint32_t* expected_ne parameter (may be NULL to skip). After resolve, the response's ne[] is compared element-wise; mismatch = refuse the bind (return nullptr) — only type/nb (the quant layout) may differ between bindings; ne (the count along each axis) is structural.

6. llama_hydra_rebind_combined_experts (new)

Extracted from llama_hydra_load_combined_experts. Reuses the per-peer backend (which is registered once at startup), frees the previous binding's meta_ctx, allocates a new one, and re-runs the bind loop with the new epoch + ne-guard. Per-peer resource map (s_hydra_combined_bindings) owns the meta_ctx — replacing an entry drops the previous ctx (the per-rebind leak fix; old code pushed ctxs into a static vector and never freed).

7. llama_hydra_register_local_tensors_for_rpc re-callable

Now calls ggml_backend_rpc_clear_local_tensors before the registration loop. First call: clear is a no-op (epoch 0 → 1). Subsequent calls (after a model swap / re-register on the peer) bump the epoch again, so any outstanding binding becomes observably stale.

8. SET_EXPERT_MODE(combined) handler now binds on activation

server-context.cpp:3504. Replaces the implicit reliance on a startup-only binding. On each SET_EXPERT_MODE("combined") the head calls llama_hydra_rebind_combined_experts and degrades to solo if the rebind fails (peer unreachable, ne-guard, RPC error). This is the ggml-org#357 fix: the head binds on demand when the peer is reachable, every time. No abort path.

Tests

tests/test-hydra-rpc-bind.cpp now exercises:

  • original round-trip (zero-copy read of server's live tensor)
  • registry_epoch in resolve response
  • ne-guard: rejected bind with mismatched shape (returns null)
  • ne-guard: NULL expected_ne skips the check (legacy callers)
  • missing name: returns null
  • clear → epoch bumps; re-register → epoch unchanged (only clear bumps)
  • re-bind after clear+re-register succeeds and observes the new epoch
  • fail-open: bind to unreachable endpoint returns null (no abort)
  • re-callable register is monotonic (no spurious epoch bump)

Run: ./bin/test-hydra-rpc-bind → "OK: zero-copy RESOLVE_TENSOR/bind_remote_tensor round-trips correctly (incl. ggml-org#368 epoch, ne-guard, fail-open, re-callable register)"

Build verification

  • sm_120 (CUDA 13.2): cmake --build build_sm120 --target llama-engine test-hydra-rpc-bind — clean. Test runs and passes.
  • sm_60 build deferred to P100 host (the AGENTS.md note about the gcc-15 / cuda-12.9 toolchain incompatibility is unchanged).

Out of scope (lands with ggml-org#263)

The actual SWAP_QUANT model free/reload + re-register on the engine (0x45 handler body) — slots into this contract once built. The Core side (WorkerSchedulerService SWAPPING state) is in the parent repo (ddvnguyen/hydra_vortex#368 §3).

AI assistance disclosure

This PR was produced with AI assistance (Claude) under explicit owner direction. The ddvnguyen/llama.cpp fork is a private fork and is exempt from the upstream llama.cpp AI-submission policy (per AGENTS.md, "Private forks are exempt").

…ra_vortex ggml-org#368)

Makes COMBINED zero-copy binding (llama.cpp#20) swap-safe and re-callable
in preparation for dynamic quant swap (ggml-org#263, SWAP_QUANT 0x45). Also fixes
the COMBINED startup race (hydra_vortex ggml-org#357) by binding on demand at
SET_EXPERT_MODE("combined") instead of one-shot at startup.

Forward-compatible slice per docs/dynamic-quant-swap-combined.md: this
lands the protocol + binding + fail-open + tests now, so ggml-org#263 can plug
in the actual model free/reload against a stable contract.

## What

### 1. Registry epoch (ggml-rpc.cpp)
- rpc_msg_resolve_tensor_rsp gets a new uint32_t registry_epoch; the
  server returns its g_hydra_registry_epoch with every resolve.
- g_hydra_registry_epoch (atomic<uint32_t>) is bumped on every
  ggml_backend_rpc_clear_local_tensors() call. A head re-checks
  ggml_backend_rpc_get_remote_registry_epoch() before each use of a
  binding; mismatch = stale = the head must rebind or stay solo.

### 2. ggml_backend_rpc_clear_local_tensors
New public proc. Clears the local-tensor registry and bumps the epoch.
Idempotent. Foreign-buffer clearing is a defense-in-depth follow-up
(the head's epoch check is the primary safety).

### 3. ggml_backend_rpc_get_remote_registry_epoch
New public proc. Probes the peer's current epoch over RPC (reuses
RPC_CMD_RESOLVE_TENSOR with a sentinel name and reads back the epoch
field of the response — single round trip). Returns 0 on any failure;
callers must treat 0 as "couldn't verify" not "epoch is 0".

### 4. Fail-open in ggml_backend_rpc_bind_remote_tensor
Replaced RPC_STATUS_ASSERT(status) (which calls GGML_ABORT) with
"if (!status) { log; return nullptr; }" — peer drop, malformed
response, or transient RPC error must NOT crash the engine. The
bind result is still observable: nullptr means "fall back to solo".

### 5. ne-guard
ggml_backend_rpc_bind_remote_tensor takes a new const uint32_t*
expected_ne parameter (may be NULL to skip). After resolve, the
response's ne[] is compared element-wise; mismatch = refuse the bind
(return nullptr) — only type/nb (the quant layout) may differ between
bindings; ne (the count along each axis) is structural. A head bound
to a different peer's resident model fails loud, not by running on
the wrong weights (which would corrupt the KV cache silently).

### 6. llama_hydra_rebind_combined_experts (new)
Extracted from llama_hydra_load_combined_experts. Reuses the per-peer
backend (which is registered once at startup), frees the previous
binding's meta_ctx, allocates a new one, and re-runs the bind loop
with the new epoch + ne-guard. Per-peer resource map
(s_hydra_combined_bindings) owns the meta_ctx — replacing an entry
drops the previous ctx (the per-rebind leak fix; old code pushed
ctxs into a static vector and never freed).

### 7. llama_hydra_register_local_tensors_for_rpc re-callable
Now calls ggml_backend_rpc_clear_local_tensors before the registration
loop. First call: clear is a no-op (epoch 0 → 1). Subsequent calls
(after a model swap / re-register on the peer) bump the epoch again,
so any outstanding binding becomes observably stale.

### 8. SET_EXPERT_MODE(combined) handler now binds on activation
server-context.cpp:3504. Replaces the implicit reliance on a
startup-only binding. On each SET_EXPERT_MODE("combined") the head
calls llama_hydra_rebind_combined_experts and degrades to solo if
the rebind fails (peer unreachable, ne-guard, RPC error). This is
the ggml-org#357 fix: the head binds on demand when the peer is reachable,
every time. No abort path.

## Tests (test-hydra-rpc-bind.cpp)
- original round-trip (zero-copy read of server's live tensor)
- registry_epoch in resolve response
- ne-guard: rejected bind with mismatched shape (returns null)
- ne-guard: NULL expected_ne skips the check (legacy callers)
- missing name: returns null
- clear → epoch bumps; re-register → epoch unchanged (only clear bumps)
- re-bind after clear+re-register succeeds and observes the new epoch
- fail-open: bind to unreachable endpoint returns null (no abort)
- re-callable register is monotonic (no spurious epoch bump)

Run: `./bin/test-hydra-rpc-bind` → "OK: zero-copy RESOLVE_TENSOR/
bind_remote_tensor round-trips correctly (incl. ggml-org#368 epoch, ne-guard,
fail-open, re-callable register)"

## Build verification
- sm_120 (CUDA 13.2): `cmake --build build_sm120 --target llama-engine
  test-hydra-rpc-bind` — clean. Test runs and passes.
- sm_60 build deferred to P100 host (the AGENTS.md note about the
  gcc-15 / cuda-12.9 toolchain incompatibility is unchanged).

## Out of scope (lands with ggml-org#263)
The actual SWAP_QUANT model free/reload + re-register on the engine
(0x45 handler body) — slots into this contract once built. The Core
side (WorkerSchedulerService SWAPPING state) is in the parent repo
(ddvnguyen/hydra_vortex#368 §3).

## Cross-repo
- ddvnguyen/hydra_vortex#368 (parent issue)
- ddvnguyen/hydra_vortex#366 (merged — the SOLO_BUSY/COMBINED_SERVING
  state machine foundation that ggml-org#368 §3 extends)
- ddvnguyen/hydra_vortex#357 (startup race — fixed here by bind-on-
  activation)
- #22 (fork-side tracker)

AI assistance note: this commit was produced with AI assistance
(Claude) under explicit owner direction. Per AGENTS.md the private
fork ddvnguyen/llama.cpp is exempt from the upstream llama.cpp AI-
submission policy.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

@ddvnguyen ddvnguyen left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Review summary

(Submitting as a comment — GitHub blocks REQUEST_CHANGES on one's own PR. Intended verdict: request changes, chiefly for P1.)

Design is sound and the fail-open / ne-guard / rebind-leak fixes are correct. One real correctness bug undercuts the headline epoch mechanism, plus a wire-compat gap and a few minor notes. Inline comments on the two blocking items.

🔴 P1 — resolve_tensor never sets registry_epoch on the not-found path

ggml_backend_rpc_get_remote_registry_epoch probes with a sentinel name that's deliberately never found, but the not-found return leaves registry_epoch unset (and the dispatch-site response struct is uninitialized). The probe — the PR's stated primary safety for detecting a peer model swap — transmits indeterminate stack bytes. It "passes" only because a fresh stack reads 0 == initial epoch. See inline comment.

🟡 P2 — wire struct changed without an RPC protocol version bump

rpc_msg_resolve_tensor_rsp grew a field but RPC_PROTO_MINOR_VERSION wasn't bumped, so a new head ↔ old (sm_60) peer mismatch isn't caught at the hello handshake → silent permanent solo. See inline comment.

Minor notes

  • Epoch recorded but never re-checked at use time — acknowledged forward-compat, fine; but combined with P1 the staleness machinery is unused and broken, and the green test gives false confidence. A one-line note that the probe isn't yet a live guard would help.
  • get_remote_registry_epoch returns 0 on both failure and genuine epoch 0 — documented, but ambiguous; consider a separate "verified" signal.
  • s_hydra_combined_bindings has no mutex — fine if SET_EXPERT_MODE stays single-threaded; flag for the future.
  • Nit: the shared bound_epoch across the bind loop keeps the last writer's value.

✅ Done well

Fail-open replacing GGML_ABORT; ne-guard with NULL opt-out for legacy callers; per-rebind meta_ctx leak fix via the per-peer map; already_added guard on the non-idempotent scheduler append; bind-on-activation addressing ggml-org#357.


Generated by Claude Code

foreign_buffers.insert(info.buffer);

response.found = 1;
response.registry_epoch = g_hydra_registry_epoch.load(std::memory_order_acquire);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

P1 — registry_epoch is set only on the found path. The not-found early return just above (if (it == g_hydra_local_tensors.end()) { response.found = 0; return true; }) leaves response.registry_epoch unset, and the dispatch-site rpc_msg_resolve_tensor_rsp response; is not zero-initialized.

This breaks ggml_backend_rpc_get_remote_registry_epoch, which intentionally probes with a sentinel name (__hydra_epoch_probe__) that is never found — its comment claims the server "still populates the epoch field," but on the not-found path it does not. The probe therefore returns indeterminate stack bytes. It passes today only because a fresh server-thread stack reads 0 == the initial epoch; once the epoch is non-zero (after any clear) or the stack slot is reused, the probe returns garbage/stale values.

Since this probe is described as the primary safety for detecting a peer model swap, when the use-time check lands (ggml-org#263) it'll be built on a broken primitive → false-stale (permanent solo) or false-fresh (UAF on freed weights — the hazard this PR exists to prevent).

Fix: set response.registry_epoch before the not-found return true; (and/or zero-init the response at the dispatch site). The green test (remote registry epoch matches local after register) is validating UB, not the contract.


Generated by Claude Code


struct rpc_msg_resolve_tensor_rsp {
uint8_t found;
uint32_t registry_epoch; // #368: monotonic version of the local-tensor registry.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

P2 — wire struct changed without an RPC protocol version bump. Adding registry_epoch here changes rpc_msg_resolve_tensor_rsp's size/layout, but neither RPC_PROTO_MAJOR_VERSION nor RPC_PROTO_MINOR_VERSION is bumped. A new RTX head talking to an old P100 server (this PR defers the sm_60 build to the P100 host) passes the hello handshake, then send_rpc_cmd(... &response, sizeof(response)) size-mismatches on RESOLVE_TENSOR → fail-open → silent permanent solo with no version-level diagnostic.

Recommend bumping RPC_PROTO_MINOR_VERSION so a stale peer is rejected loudly at handshake, and/or coordinating the sm_60 rebuild before this path is relied on.


Generated by Claude Code

…VERSION to 1

P1: rpc_server::resolve_tensor left response.registry_epoch uninitialised
on the not-found early return. ggml_backend_rpc_get_remote_registry_epoch
probes with a sentinel name that is never found, so it was reading
indeterminate stack bytes instead of the real epoch. Fix: populate
registry_epoch before the not-found return, inside the lock where
g_hydra_registry_epoch is already loaded for the found path.

P2: rpc_msg_resolve_tensor_rsp gained a uint32_t registry_epoch field but
RPC_PROTO_MINOR_VERSION was not bumped. A new RTX head talking to an old
P100 server (not yet rebuilt) would size-mismatch on RESOLVE_TENSOR and
silently fail-open to permanent solo with no diagnostic. Fix: bump minor
to 1 and tighten negotiate_hello to require an exact minor match (reject
both older AND newer peers) so the mismatch is diagnosed at hello, not
buried in a silent fail-open.

Fixes review findings P1 and P2 on #23.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@ddvnguyen

Copy link
Copy Markdown
Owner Author

Both blocking findings addressed in 94a7dab.

P1 — registry_epoch unset on not-found path (fixed)
rpc_server::resolve_tensor now sets response.registry_epoch before the not-found early return, inside the same lock where it's already read for the found path. ggml_backend_rpc_get_remote_registry_epoch (sentinel probe) now always receives the real epoch. The test that was validating UB (remote registry epoch matches local after register) now validates the correct contract.

P2 — wire struct grew without a version bump (fixed)
RPC_PROTO_MINOR_VERSION bumped from 0 → 1 in ggml-rpc.h. negotiate_hello changed from response.minor > RPC_PROTO_MINOR_VERSION (reject only newer) to response.minor != RPC_PROTO_MINOR_VERSION (reject older and newer). An old P100 server (minor=0) now fails at hello with a clear diagnostic instead of silently size-mismatching at RESOLVE_TENSOR time. The P100 host will need a rebuild before this path is relied on (consistent with the PR's deferred sm_60 build note).

Minor notes


Generated by Claude Code

@ddvnguyen ddvnguyen left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Re-review (commit 94a7dab)

(Submitting as a comment — GitHub blocks APPROVE on one's own PR, same as it blocked REQUEST_CHANGES last round. Intended verdict: approve.)

Follow-up commit fix(rpc): set registry_epoch on not-found path; bump RPC_PROTO_MINOR_VERSION to 1 addresses both findings from the previous review.

🔴 P1 — verified fixed

rpc_server::resolve_tensor's not-found branch now sets response.registry_epoch = g_hydra_registry_epoch.load(std::memory_order_acquire); before returning, correctly placed inside the existing std::lock_guard scope. The sentinel-probe path (ggml_backend_rpc_get_remote_registry_epoch) now reads a real value instead of uninitialized stack bytes.

🟡 P2 — verified fixed

Checked the transport layer (send_rpc_cmd/recv_msg): it already enforces out_size == output_size and fails cleanly on a struct-size mismatch, so the original issue was never memory-unsafe — just a silent, undiagnosed fail-open. The fix bumps RPC_PROTO_MINOR_VERSION to 1 and tightens negotiate_hello to require an exact major.minor match (previously only rejected a server with a greater minor), so an old/not-yet-rebuilt peer is now refused loudly at hello with a clear version-mismatch diagnostic instead of a confusing generic RPC failure later. No regression — this path only affects the COMBINED zero-copy bind feature.

Remaining (non-blocking, unchanged, already acknowledged as follow-up scope)

  • Epoch recorded but not yet re-checked at use time before a decode/prefill (lands with ggml-org#263).
  • get_remote_registry_epoch overloads 0 for both "failure" and "genuine epoch 0".
  • s_hydra_combined_bindings has no mutex — fine under the current single-threaded SET_EXPERT_MODE caller.
  • Nit: shared bound_epoch across the per-tensor bind loop keeps the last writer's value.

Both blocking findings are resolved with no new issues introduced. LGTM.


Generated by Claude Code

@ddvnguyen
ddvnguyen merged commit 77416ee into hydra-fork Jul 1, 2026
@ddvnguyen

Copy link
Copy Markdown
Owner Author

Added two more fix commits on top:

ddvnguyen added a commit that referenced this pull request Jul 1, 2026
* fork: bind-on-activation + registry epoch + ne-guard + fail-open (hydra_vortex ggml-org#368)

Makes COMBINED zero-copy binding (llama.cpp#20) swap-safe and re-callable
in preparation for dynamic quant swap (ggml-org#263, SWAP_QUANT 0x45). Also fixes
the COMBINED startup race (hydra_vortex ggml-org#357) by binding on demand at
SET_EXPERT_MODE("combined") instead of one-shot at startup.

Forward-compatible slice per docs/dynamic-quant-swap-combined.md: this
lands the protocol + binding + fail-open + tests now, so ggml-org#263 can plug
in the actual model free/reload against a stable contract.

## What

### 1. Registry epoch (ggml-rpc.cpp)
- rpc_msg_resolve_tensor_rsp gets a new uint32_t registry_epoch; the
  server returns its g_hydra_registry_epoch with every resolve.
- g_hydra_registry_epoch (atomic<uint32_t>) is bumped on every
  ggml_backend_rpc_clear_local_tensors() call. A head re-checks
  ggml_backend_rpc_get_remote_registry_epoch() before each use of a
  binding; mismatch = stale = the head must rebind or stay solo.

### 2. ggml_backend_rpc_clear_local_tensors
New public proc. Clears the local-tensor registry and bumps the epoch.
Idempotent. Foreign-buffer clearing is a defense-in-depth follow-up
(the head's epoch check is the primary safety).

### 3. ggml_backend_rpc_get_remote_registry_epoch
New public proc. Probes the peer's current epoch over RPC (reuses
RPC_CMD_RESOLVE_TENSOR with a sentinel name and reads back the epoch
field of the response — single round trip). Returns 0 on any failure;
callers must treat 0 as "couldn't verify" not "epoch is 0".

### 4. Fail-open in ggml_backend_rpc_bind_remote_tensor
Replaced RPC_STATUS_ASSERT(status) (which calls GGML_ABORT) with
"if (!status) { log; return nullptr; }" — peer drop, malformed
response, or transient RPC error must NOT crash the engine. The
bind result is still observable: nullptr means "fall back to solo".

### 5. ne-guard
ggml_backend_rpc_bind_remote_tensor takes a new const uint32_t*
expected_ne parameter (may be NULL to skip). After resolve, the
response's ne[] is compared element-wise; mismatch = refuse the bind
(return nullptr) — only type/nb (the quant layout) may differ between
bindings; ne (the count along each axis) is structural. A head bound
to a different peer's resident model fails loud, not by running on
the wrong weights (which would corrupt the KV cache silently).

### 6. llama_hydra_rebind_combined_experts (new)
Extracted from llama_hydra_load_combined_experts. Reuses the per-peer
backend (which is registered once at startup), frees the previous
binding's meta_ctx, allocates a new one, and re-runs the bind loop
with the new epoch + ne-guard. Per-peer resource map
(s_hydra_combined_bindings) owns the meta_ctx — replacing an entry
drops the previous ctx (the per-rebind leak fix; old code pushed
ctxs into a static vector and never freed).

### 7. llama_hydra_register_local_tensors_for_rpc re-callable
Now calls ggml_backend_rpc_clear_local_tensors before the registration
loop. First call: clear is a no-op (epoch 0 → 1). Subsequent calls
(after a model swap / re-register on the peer) bump the epoch again,
so any outstanding binding becomes observably stale.

### 8. SET_EXPERT_MODE(combined) handler now binds on activation
server-context.cpp:3504. Replaces the implicit reliance on a
startup-only binding. On each SET_EXPERT_MODE("combined") the head
calls llama_hydra_rebind_combined_experts and degrades to solo if
the rebind fails (peer unreachable, ne-guard, RPC error). This is
the ggml-org#357 fix: the head binds on demand when the peer is reachable,
every time. No abort path.

## Tests (test-hydra-rpc-bind.cpp)
- original round-trip (zero-copy read of server's live tensor)
- registry_epoch in resolve response
- ne-guard: rejected bind with mismatched shape (returns null)
- ne-guard: NULL expected_ne skips the check (legacy callers)
- missing name: returns null
- clear → epoch bumps; re-register → epoch unchanged (only clear bumps)
- re-bind after clear+re-register succeeds and observes the new epoch
- fail-open: bind to unreachable endpoint returns null (no abort)
- re-callable register is monotonic (no spurious epoch bump)

Run: `./bin/test-hydra-rpc-bind` → "OK: zero-copy RESOLVE_TENSOR/
bind_remote_tensor round-trips correctly (incl. ggml-org#368 epoch, ne-guard,
fail-open, re-callable register)"

## Build verification
- sm_120 (CUDA 13.2): `cmake --build build_sm120 --target llama-engine
  test-hydra-rpc-bind` — clean. Test runs and passes.
- sm_60 build deferred to P100 host (the AGENTS.md note about the
  gcc-15 / cuda-12.9 toolchain incompatibility is unchanged).

## Out of scope (lands with ggml-org#263)
The actual SWAP_QUANT model free/reload + re-register on the engine
(0x45 handler body) — slots into this contract once built. The Core
side (WorkerSchedulerService SWAPPING state) is in the parent repo
(ddvnguyen/hydra_vortex#368 §3).

## Cross-repo
- ddvnguyen/hydra_vortex#368 (parent issue)
- ddvnguyen/hydra_vortex#366 (merged — the SOLO_BUSY/COMBINED_SERVING
  state machine foundation that ggml-org#368 §3 extends)
- ddvnguyen/hydra_vortex#357 (startup race — fixed here by bind-on-
  activation)
- #22 (fork-side tracker)

AI assistance note: this commit was produced with AI assistance
(Claude) under explicit owner direction. Per AGENTS.md the private
fork ddvnguyen/llama.cpp is exempt from the upstream llama.cpp AI-
submission policy.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* fix(rpc): set registry_epoch on not-found path; bump RPC_PROTO_MINOR_VERSION to 1

P1: rpc_server::resolve_tensor left response.registry_epoch uninitialised
on the not-found early return. ggml_backend_rpc_get_remote_registry_epoch
probes with a sentinel name that is never found, so it was reading
indeterminate stack bytes instead of the real epoch. Fix: populate
registry_epoch before the not-found return, inside the lock where
g_hydra_registry_epoch is already loaded for the found path.

P2: rpc_msg_resolve_tensor_rsp gained a uint32_t registry_epoch field but
RPC_PROTO_MINOR_VERSION was not bumped. A new RTX head talking to an old
P100 server (not yet rebuilt) would size-mismatch on RESOLVE_TENSOR and
silently fail-open to permanent solo with no diagnostic. Fix: bump minor
to 1 and tighten negotiate_hello to require an exact minor match (reject
both older AND newer peers) so the mismatch is diagnosed at hello, not
buried in a silent fail-open.

Fixes review findings P1 and P2 on #23.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* fix: fail-open on RPC handshake (HELLO) + advertise combined/pipeline in capabilities (hydra_vortex ggml-org#375 ggml-org#376)

## ggml-org#375 — advertise combined/pipeline in engine_info

server-context.cpp: the engine_info (0x41) handler now lists 'combined' and
'pipeline' in the capabilities set. The build that ships this binary is
always the llama-engine fork, which carries the COMBINED code path; this
is the engine-side mirror of the static workers.json fields.

## ggml-org#376 — fail-open on RPC handshake

ggml-rpc.cpp: RPC_STATUS_ASSERT in negotiate_hello (line 363) replaced
with if(!status){log; return false;}. The TCP connection succeeds before
the RPC server's HELLO handler is ready, especially under compose where
two llama-engine instances start simultaneously. Aborting on a transient
HELLO failure crashes the head for no reason — returning false lets the
caller (get_socket -> get_device_count -> add_server) return 0 devs,
which keeps the head alive and degrades to solo instead.

Refs: ddvnguyen/hydra_vortex#375, ddvnguyen/hydra_vortex#376

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* fix: bind-on-activation and OT-pattern stripping for COMBINED mode

Two bugs prevented SET_EXPERT_MODE("combined") from ever activating when
the peer (RTX 3060) came up after the head (RTX 5060 Ti) had already started:

1. server-context.cpp: want_combined was gated on hydra_combined_head_attached,
   which is only set true when the startup llama_hydra_load_combined_experts
   call succeeds. If the peer was down at boot, the flag stayed false and the
   entire bind-on-activation code path (ggml-org#368) was unreachable. Fixed by gating
   on "is the engine configured as a combined head" (non-empty peer addr +
   OT pattern) instead, which correctly reflects what the engine is configured
   to do rather than an accident of startup ordering. Also latches
   hydra_combined_head_attached=true on first successful rebind so INFO RPC
   advertises combined capability.

2. llama-hydra.cpp: tensor_pattern from --combined-ot-pattern uses the
   override-tensor format "<name_regex>=<backend>" (e.g.
   "blk\.([0-9]+)\.ffn_.*_exps\.weight=CPU"), but the regex was matched
   against raw tensor names which never contain "=CPU". Fixed by stripping
   everything from the last '=' onward before compiling the regex.

With both fixes applied: 120/120 expert tensors across 40 layers are bound
to the peer on the first COMBINED request, and the engine reports
applied='combined' instead of falling back to solo.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

---------

Co-authored-by: Hydra Engineering <hydra-engineering@local>
Co-authored-by: Claude Opus 4.8 <[email protected]>
ddvnguyen pushed a commit to ddvnguyen/hydra_vortex that referenced this pull request Jul 1, 2026
…generation

Wires the Core-side of #368 §3 — the SWAPPING GPU state and the
SWAP_QUANT admission + lifecycle. Builds on the per-GPU exclusive
reservation from #366 (PR #371, MERGEABLE). Pairs with the fork-side
work (ddvnguyen/llama.cpp#23) that lands the actual SWAP_QUANT body
against a stable Core contract.

## Changes

### IWorkerTracker
- TryEnterSwapping(name) / ExitSwapping(name) / IsSwapping(name) /
  GetSwapGeneration(name) — the new SWAPPING state. Mutually exclusive
  with SOLO_BUSY and COMBINED_SERVING: blocks TryAcquireSlot,
  HasFreeSlot, FreeSlotCount, IsFree, FreeWorkers, TryReserveWorkerExclusive.
  Succeeds only when the worker is healthy, all slots free, not
  exclusively reserved, and not already SWAPPING.
- ExitSwapping bumps SwapGeneration only on an actual exit (idempotent
  no-op otherwise) — the head's epoch-check on the C++ side reads this
  generation alongside the registry epoch, so spurious bumps would
  force unnecessary rebinds.

### WorkerSchedulerService
- TryAcquireMultiEngine (WSS:746) refuses if the head OR the peer is
  SWAPPING — a peer about to free+reload its resident tensors must
  not be bound, and a head about to swap must not lend its slot to a
  peer. This is the #368 §3 serialization rule.
- New public TrySwapQuantAsync(worker, quantKey, pattern, trace, ct)
  on IWorkerScheduler: enter SWAPPING, dispatch the engine SWAP_QUANT
  RPC (stubbed on the C++ side until #263), release SWAPPING in a
  finally. On exit, SwapGeneration is bumped so the head can detect
  the change on its next binding-epoch check.

### WorkItem (no change)
The head's "binding is stale" check is a C++-side concern (the engine
records the registry epoch and re-fetches ggml_backend_rpc_get_remote_
registry_epoch before each use). The Core just bumps the generation
counter; the engine does the actual staleness check.

## Tests
- 13 new WorkerTrackerTests for the SWAPPING state (admission, gating,
  generation, status)
- 4 new WorkerSchedulerTests for TrySwapQuantAsync (succeeds on idle,
  refuses when exclusively reserved, refuses when busy, idempotent on
  repeated refusals, defensive on unknown worker)
- 346/347 pass on the full Tests.Core suite (1 pre-existing flake from
  PR #369: LoadWorkers_ProductionConfigFile_LoadsBothWorkersWithCorrectModelFields)

## Out of scope (lands with #263)
The actual SWAP_QUANT model free/reload on the engine. The C++ side
is currently a stub (server-context.cpp:3535) that logs and returns
OK. The Core contract is in place: when the real implementation lands,
it must (1) call TryEnterSwapping before any free/reload, (2) call
ggml_backend_rpc_clear_local_tensors to bump the registry epoch
(replacing resident tensors), and (3) call ggml_backend_rpc_register_
local_tensor to re-register the new tensors. ExitSwapping on the
Core side will then bump the swap generation, completing the loop.

## Cross-repo coordination
- #368 (parent issue)
- #366 (PR #371, foundation: per-GPU exclusive
  peer reservation)
- #357 (startup race — fixed by the fork-side
  bind-on-activation)
- ddvnguyen/llama.cpp#22 (fork-side tracker)
- ddvnguyen/llama.cpp#23 (fork PR — the C++ side: registry epoch +
  bind-on-activation + ne-guard + fail-open)

The parent submodule bump for src/llama-cpp is intentionally deferred
to a follow-up commit on this branch (see docs/workflow/08-llama-fork.md
— must point at the merge commit of #23, which requires user review of
the fork PR first).

Refs: #368
Co-Authored-By: Claude Opus 4.8 <[email protected]>
hydra-z Bot pushed a commit to ddvnguyen/hydra_vortex that referenced this pull request Jul 1, 2026
…generation (#372)

Wires the Core-side of #368 §3 — the SWAPPING GPU state and the
SWAP_QUANT admission + lifecycle. Builds on the per-GPU exclusive
reservation from #366 (PR #371, MERGEABLE). Pairs with the fork-side
work (ddvnguyen/llama.cpp#23) that lands the actual SWAP_QUANT body
against a stable Core contract.

## Changes

### IWorkerTracker
- TryEnterSwapping(name) / ExitSwapping(name) / IsSwapping(name) /
  GetSwapGeneration(name) — the new SWAPPING state. Mutually exclusive
  with SOLO_BUSY and COMBINED_SERVING: blocks TryAcquireSlot,
  HasFreeSlot, FreeSlotCount, IsFree, FreeWorkers, TryReserveWorkerExclusive.
  Succeeds only when the worker is healthy, all slots free, not
  exclusively reserved, and not already SWAPPING.
- ExitSwapping bumps SwapGeneration only on an actual exit (idempotent
  no-op otherwise) — the head's epoch-check on the C++ side reads this
  generation alongside the registry epoch, so spurious bumps would
  force unnecessary rebinds.

### WorkerSchedulerService
- TryAcquireMultiEngine (WSS:746) refuses if the head OR the peer is
  SWAPPING — a peer about to free+reload its resident tensors must
  not be bound, and a head about to swap must not lend its slot to a
  peer. This is the #368 §3 serialization rule.
- New public TrySwapQuantAsync(worker, quantKey, pattern, trace, ct)
  on IWorkerScheduler: enter SWAPPING, dispatch the engine SWAP_QUANT
  RPC (stubbed on the C++ side until #263), release SWAPPING in a
  finally. On exit, SwapGeneration is bumped so the head can detect
  the change on its next binding-epoch check.

### WorkItem (no change)
The head's "binding is stale" check is a C++-side concern (the engine
records the registry epoch and re-fetches ggml_backend_rpc_get_remote_
registry_epoch before each use). The Core just bumps the generation
counter; the engine does the actual staleness check.

## Tests
- 13 new WorkerTrackerTests for the SWAPPING state (admission, gating,
  generation, status)
- 4 new WorkerSchedulerTests for TrySwapQuantAsync (succeeds on idle,
  refuses when exclusively reserved, refuses when busy, idempotent on
  repeated refusals, defensive on unknown worker)
- 346/347 pass on the full Tests.Core suite (1 pre-existing flake from
  PR #369: LoadWorkers_ProductionConfigFile_LoadsBothWorkersWithCorrectModelFields)

## Out of scope (lands with #263)
The actual SWAP_QUANT model free/reload on the engine. The C++ side
is currently a stub (server-context.cpp:3535) that logs and returns
OK. The Core contract is in place: when the real implementation lands,
it must (1) call TryEnterSwapping before any free/reload, (2) call
ggml_backend_rpc_clear_local_tensors to bump the registry epoch
(replacing resident tensors), and (3) call ggml_backend_rpc_register_
local_tensor to re-register the new tensors. ExitSwapping on the
Core side will then bump the swap generation, completing the loop.

## Cross-repo coordination
- #368 (parent issue)
- #366 (PR #371, foundation: per-GPU exclusive
  peer reservation)
- #357 (startup race — fixed by the fork-side
  bind-on-activation)
- ddvnguyen/llama.cpp#22 (fork-side tracker)
- ddvnguyen/llama.cpp#23 (fork PR — the C++ side: registry epoch +
  bind-on-activation + ne-guard + fail-open)

The parent submodule bump for src/llama-cpp is intentionally deferred
to a follow-up commit on this branch (see docs/workflow/08-llama-fork.md
— must point at the merge commit of #23, which requires user review of
the fork PR first).

Refs: #368

Co-authored-by: Hydra Engineering <hydra-engineering@local>
Co-authored-by: Claude Opus 4.8 <[email protected]>
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.

[Llama-Engine-P1] COMBINED mode startup race: peer unreachable at boot degrades to solo permanently

1 participant