fix(vad): restore a per-request threshold override on the lifecycle path (#896)

VA
21h ago
+283-37

Summary

Fixes #896 (follow-up to #813).

On the handle-less lifecycle VAD path (rac_vad_process_lifecycle_proto), a per-request activation_threshold was written straight to the shared, loaded detector and never restored. A later request that omitted the field kept running at the previous caller's override. This breaks the VADOptions.activation_threshold contract ("unset = keep the loaded detector's calibrated value"). Also, lifecycle_threshold_mutex() was only taken by requests that carried an override, so a request without one could run inside another request's override window.

This takes the approach the issue suggests, with no ABI bump:

  • Baseline: the lifecycle layer remembers the threshold rac_vad_configure_lifecycle_proto last applied, keyed by detector impl and model id so a reload doesn't inherit a stale value. A detector that configure never touched falls back to kDefaultModelVadThreshold (0.5). That's what the component path assumes for a freshly loaded model, and it's Sherpa's default.
  • Restore: after process, an overriding request sets the detector back to the baseline while still holding the lock. A restore failure is reported the same way a process failure is.
  • Serialization: every lifecycle process takes the lock, with or without an override, and so does configure. A configure therefore can't land inside an override window and then be undone by that request's restore.
  • The "backend does not implement process" check moved ahead of the override, so that error no longer leaves an override applied.

Limitation: without a vtable get_threshold, the layer can't read a backend's own calibrated value. If a backend loads at something other than 0.5 and configure is never called, the first override is restored to 0.5, which is the component path's assumption. The get_threshold + RAC_PLUGIN_API_VERSION bump the issue mentions would remove this. I left it out to keep the ABI unchanged, but I can follow up with it if you'd prefer.

Type of Change

  • Bug fix

Testing

  • Added to core/tests/test_nonllm_lifecycle_proto_abi.cpp (mock VAD ops, samples averaging 0.2): configure 0.3, then process with an override of 0.1 (speech), then process with no override, asserting no speech, i.e. it runs at 0.3. On main the last assertion fails because the 0.1 override sticks.
  • Not compiled locally: this machine has no CMake/protobuf toolchain for core, so I'm relying on CI's ctest run.
  • I checked that no other lifecycle code writes the threshold: voice_agent_* and model_lifecycle_accessors.cpp don't call set_threshold.

Labels

  • Commons (core)

Review follow-up (75212377f)

  • No events under the lock: failure events are published after the threshold lock is released. Subscribers run synchronously, so a re-entrant one would otherwise deadlock.
  • Failed restore: it's remembered, and the next request without an override (and the voice agent sweep) retries it first. If the retry also fails, the request returns the error instead of running at the stale override.
  • Baseline key: it's now the loaded model itself: a weak_ptr to the LoadedModel, via the new rac::lifecycle::lifecycle_vad_owner. Before, it was impl + model id, which a reload could reuse.
  • Voice agent: the lock is now rac::lifecycle::lifecycle_vad_mutex(), declared in rac_nonllm_lifecycle_bridge.h, and the voice agent's direct ops->process sweep takes it too.

Review follow-up (0ac232229)

  • No restore after each request: on Sherpa, set_threshold rebuilds the detector and drops its speech state, so restoring after every overridden request broke continuity. The baseline now records the threshold the detector is at, and set_threshold is skipped when it already matches. An override stays in place until a request (or the voice agent's sweep) needs the configured value. Consecutive chunks at the same value never rebuild the detector.
  • Test: it counts set_threshold calls. Two requests at the same override call it once, and two plain requests that follow call it once more.
  • Comment: the lifecycle_vad_mutex comment now names only the paths that take it.

Summary by CodeRabbit

  • Bug Fixes
    • Voice activity detection now applies the configured threshold to requests without an override. A request-level threshold no longer carries over to later requests, improving consistency across lifecycle operations.
    • Threshold-dependent processing is synchronized to prevent requests from interfering with one another. If the lifecycle detector cannot restore its configured threshold, voice activity checks can fall back to the existing component-based detection path.