feat(voice): add cache, queue, and worker modules (D-138, Spike 2 Phase 2)

MessagePack voice cache with per-zone persistence and version invalidation.
Priority work queue with crossbeam bounded channel, backpressure, pause/resume,
and zone-change reprioritization. Inference worker pool with empty output guard
and graceful degradation to base text.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-07 17:09:24 +01:00
co-authored by Claude Opus 4.6
parent 33030fcc58
commit 82a911f3aa
6 changed files with 1715 additions and 9 deletions
+722 -8
View File
@@ -2,6 +2,12 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "adler2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
name = "aho-corasick"
version = "1.1.4"
@@ -47,7 +53,7 @@ version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
"windows-sys",
"windows-sys 0.61.2",
]
[[package]]
@@ -58,7 +64,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys",
"windows-sys 0.61.2",
]
[[package]]
@@ -134,6 +140,12 @@ version = "0.21.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567"
[[package]]
name = "base64"
version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
name = "bevy_app"
version = "0.18.0"
@@ -371,6 +383,16 @@ version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cc"
version = "1.2.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aebf35691d1bfb0ac386a69bac2fde4dd276fb618cf8bf4f5318fe285e821bb2"
dependencies = [
"find-msvc-tools",
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.4"
@@ -448,12 +470,30 @@ dependencies = [
"unicode-segmentation",
]
[[package]]
name = "crc32fast"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511"
dependencies = [
"cfg-if",
]
[[package]]
name = "critical-section"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b"
[[package]]
name = "crossbeam-channel"
version = "0.5.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-queue"
version = "0.3.12"
@@ -477,7 +517,7 @@ checksum = "e0b1fab2ae45819af2d0731d60f2afe17227ebb1a1538a236da84c93e9a60162"
dependencies = [
"dispatch2",
"nix",
"windows-sys",
"windows-sys 0.61.2",
]
[[package]]
@@ -527,6 +567,17 @@ dependencies = [
"objc2",
]
[[package]]
name = "displaydoc"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "disqualified"
version = "1.0.0"
@@ -582,18 +633,43 @@ version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
[[package]]
name = "find-msvc-tools"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
[[package]]
name = "fixedbitset"
version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99"
[[package]]
name = "flate2"
version = "1.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]]
name = "foldhash"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb"
[[package]]
name = "form_urlencoded"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf"
dependencies = [
"percent-encoding",
]
[[package]]
name = "futures-channel"
version = "0.3.31"
@@ -647,6 +723,17 @@ dependencies = [
"slab",
]
[[package]]
name = "getrandom"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "getrandom"
version = "0.3.4"
@@ -705,6 +792,108 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "icu_collections"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c6b649701667bbe825c3b7e6388cb521c23d88644678e83c0c4d0a621a34b43"
dependencies = [
"displaydoc",
"potential_utf",
"yoke",
"zerofrom",
"zerovec",
]
[[package]]
name = "icu_locale_core"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edba7861004dd3714265b4db54a3c390e880ab658fec5f7db895fae2046b5bb6"
dependencies = [
"displaydoc",
"litemap",
"tinystr",
"writeable",
"zerovec",
]
[[package]]
name = "icu_normalizer"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f6c8828b67bf8908d82127b2054ea1b4427ff0230ee9141c54251934ab1b599"
dependencies = [
"icu_collections",
"icu_normalizer_data",
"icu_properties",
"icu_provider",
"smallvec",
"zerovec",
]
[[package]]
name = "icu_normalizer_data"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7aedcccd01fc5fe81e6b489c15b247b8b0690feb23304303a9e560f37efc560a"
[[package]]
name = "icu_properties"
version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "020bfc02fe870ec3a66d93e677ccca0562506e5872c650f893269e08615d74ec"
dependencies = [
"icu_collections",
"icu_locale_core",
"icu_properties_data",
"icu_provider",
"zerotrie",
"zerovec",
]
[[package]]
name = "icu_properties_data"
version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "616c294cf8d725c6afcd8f55abc17c56464ef6211f9ed59cccffe534129c77af"
[[package]]
name = "icu_provider"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85962cf0ce02e1e0a629cc34e7ca3e373ce20dda4c4d7294bbd0bf1fdb59e614"
dependencies = [
"displaydoc",
"icu_locale_core",
"writeable",
"yoke",
"zerofrom",
"zerotrie",
"zerovec",
]
[[package]]
name = "idna"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de"
dependencies = [
"idna_adapter",
"smallvec",
"utf8_iter",
]
[[package]]
name = "idna_adapter"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acae9609540aa318d1bc588455225fb2085b9ed0c4f6bd0d9d5bcd86f1a0344"
dependencies = [
"icu_normalizer",
"icu_properties",
]
[[package]]
name = "indexmap"
version = "2.13.0"
@@ -758,6 +947,12 @@ version = "0.2.180"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc"
[[package]]
name = "litemap"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6373607a59f0be73a39b6fe456b8192fcc3585f602af20751600e974dd455e77"
[[package]]
name = "log"
version = "0.4.29"
@@ -779,6 +974,16 @@ version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
[[package]]
name = "miniz_oxide"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"
dependencies = [
"adler2",
"simd-adler32",
]
[[package]]
name = "nix"
version = "0.31.1"
@@ -797,13 +1002,22 @@ version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "610a5acd306ec67f907abe5567859a3c693fb9886eb1f012ab8f2a47bef3db51"
[[package]]
name = "ntapi"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3b335231dfd352ffb0f8017f3b6027a4917f7df785ea2143d8af2adc66980ae"
dependencies = [
"winapi",
]
[[package]]
name = "nu-ansi-term"
version = "0.50.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
dependencies = [
"windows-sys",
"windows-sys 0.61.2",
]
[[package]]
@@ -824,12 +1038,31 @@ dependencies = [
"objc2-encode",
]
[[package]]
name = "objc2-core-foundation"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536"
dependencies = [
"bitflags",
]
[[package]]
name = "objc2-encode"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef25abbcd74fb2609453eb695bd2f860d389e457f67dc17cafc8b8cbc89d0c33"
[[package]]
name = "objc2-io-kit"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33fafba39597d6dc1fb709123dfa8289d39406734be322956a69f0931c73bb15"
dependencies = [
"libc",
"objc2-core-foundation",
]
[[package]]
name = "once_cell"
version = "1.21.3"
@@ -862,6 +1095,12 @@ dependencies = [
"thiserror",
]
[[package]]
name = "percent-encoding"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]]
name = "pin-project"
version = "1.1.10"
@@ -909,6 +1148,15 @@ dependencies = [
"portable-atomic",
]
[[package]]
name = "potential_utf"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b73949432f5e2a09657003c25bca5e19a0e9c84f8058ca374f49e0ebe605af77"
dependencies = [
"zerovec",
]
[[package]]
name = "ppv-lite86"
version = "0.2.21"
@@ -968,7 +1216,7 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c"
dependencies = [
"getrandom",
"getrandom 0.3.4",
]
[[package]]
@@ -988,6 +1236,20 @@ version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a96887878f22d7bad8a3b6dc5b7440e0ada9a245242924394987b21cf2210a4c"
[[package]]
name = "ring"
version = "0.17.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7"
dependencies = [
"cc",
"cfg-if",
"getrandom 0.2.17",
"libc",
"untrusted",
"windows-sys 0.52.0",
]
[[package]]
name = "rmp"
version = "0.8.15"
@@ -1013,7 +1275,7 @@ version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b91f7eff05f748767f183df4320a63d6936e9c6107d97c9e6bdd9784f4289c94"
dependencies = [
"base64",
"base64 0.21.7",
"bitflags",
"serde",
"serde_derive",
@@ -1034,6 +1296,41 @@ dependencies = [
"semver",
]
[[package]]
name = "rustls"
version = "0.23.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "758025cb5fccfd3bc2fd74708fd4682be41d99e5dff73c377c0646c6012c73a4"
dependencies = [
"log",
"once_cell",
"ring",
"rustls-pki-types",
"rustls-webpki",
"subtle",
"zeroize",
]
[[package]]
name = "rustls-pki-types"
version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be040f8b0a225e40375822a563fa9524378b9d63112f53e19ffff34df5d33fdd"
dependencies = [
"zeroize",
]
[[package]]
name = "rustls-webpki"
version = "0.103.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7df23109aa6c1567d1c575b9952556388da57401e4ace1d15f79eedad0d8f53"
dependencies = [
"ring",
"rustls-pki-types",
"untrusted",
]
[[package]]
name = "rustversion"
version = "1.0.22"
@@ -1116,6 +1413,7 @@ dependencies = [
"bevy_ecs",
"bincode",
"clap",
"crossbeam-channel",
"pathfinding",
"rand",
"rand_chacha",
@@ -1124,9 +1422,11 @@ dependencies = [
"serde",
"serde_json",
"serde_yaml",
"sysinfo",
"thiserror",
"tracing",
"tracing-subscriber",
"ureq",
]
[[package]]
@@ -1138,6 +1438,18 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "simd-adler32"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e320a6c5ad31d271ad523dcf3ad13e2767ad8b1cb8f047f75a8aeaf8da139da2"
[[package]]
name = "slab"
version = "0.4.12"
@@ -1189,6 +1501,12 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "subtle"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "syn"
version = "2.0.114"
@@ -1200,6 +1518,31 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "synstructure"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "sysinfo"
version = "0.35.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c3ffa3e4ff2b324a57f7aeb3c349656c7b127c3c189520251a648102a92496e"
dependencies = [
"libc",
"memchr",
"ntapi",
"objc2-core-foundation",
"objc2-io-kit",
"windows",
]
[[package]]
name = "thiserror"
version = "2.0.18"
@@ -1229,6 +1572,16 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "tinystr"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42d3e9c45c09de15d06dd8acf5f4e0e399e85927b7f00711024eb7ae10fa4869"
dependencies = [
"displaydoc",
"zerovec",
]
[[package]]
name = "toml_datetime"
version = "0.7.5+spec-1.1.0"
@@ -1363,6 +1716,48 @@ version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
[[package]]
name = "untrusted"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
[[package]]
name = "ureq"
version = "2.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02d1a66277ed75f640d608235660df48c8e3c19f3b4edb6a263315626cc3c01d"
dependencies = [
"base64 0.22.1",
"flate2",
"log",
"once_cell",
"rustls",
"rustls-pki-types",
"serde",
"serde_json",
"url",
"webpki-roots 0.26.11",
]
[[package]]
name = "url"
version = "2.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed"
dependencies = [
"form_urlencoded",
"idna",
"percent-encoding",
"serde",
]
[[package]]
name = "utf8_iter"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "utf8parse"
version = "0.2.2"
@@ -1375,7 +1770,7 @@ version = "1.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee48d38b119b0cd71fe4141b30f5ba9c7c5d9f4e7a3a8b4a674e4b6ef789976f"
dependencies = [
"getrandom",
"getrandom 0.3.4",
"js-sys",
"serde_core",
"wasm-bindgen",
@@ -1404,6 +1799,12 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "wasip2"
version = "1.0.2+wasi-0.2.9"
@@ -1482,6 +1883,24 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "webpki-roots"
version = "0.26.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9"
dependencies = [
"webpki-roots 1.0.6",
]
[[package]]
name = "webpki-roots"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22cfaf3c063993ff62e73cb4311efde4db1efb31ab78a3e5c457939ad5cc0bed"
dependencies = [
"rustls-pki-types",
]
[[package]]
name = "wgpu-types"
version = "27.0.1"
@@ -1497,21 +1916,227 @@ dependencies = [
"web-sys",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
version = "0.61.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9babd3a767a4c1aef6900409f85f5d53ce2544ccdfaa86dad48c91782c6d6893"
dependencies = [
"windows-collections",
"windows-core",
"windows-future",
"windows-link 0.1.3",
"windows-numerics",
]
[[package]]
name = "windows-collections"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3beeceb5e5cfd9eb1d76b381630e82c4241ccd0d27f1a39ed41b2760b255c5e8"
dependencies = [
"windows-core",
]
[[package]]
name = "windows-core"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3"
dependencies = [
"windows-implement",
"windows-interface",
"windows-link 0.1.3",
"windows-result",
"windows-strings",
]
[[package]]
name = "windows-future"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc6a41e98427b19fe4b73c550f060b59fa592d7d686537eebf9385621bfbad8e"
dependencies = [
"windows-core",
"windows-link 0.1.3",
"windows-threading",
]
[[package]]
name = "windows-implement"
version = "0.60.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-interface"
version = "0.59.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-link"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a"
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-numerics"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9150af68066c4c5c07ddc0ce30421554771e528bde427614c61038bc2c92c2b1"
dependencies = [
"windows-core",
"windows-link 0.1.3",
]
[[package]]
name = "windows-result"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56f42bd332cc6c8eac5af113fc0c1fd6a8fd2aa08a0119358686e5160d0586c6"
dependencies = [
"windows-link 0.1.3",
]
[[package]]
name = "windows-strings"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57"
dependencies = [
"windows-link 0.1.3",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
"windows-link 0.2.1",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows-threading"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b66463ad2e0ea3bbf808b7f1d371311c80e115c0b71d60efc142cafbcfb057a6"
dependencies = [
"windows-link 0.1.3",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "winnow"
version = "0.7.14"
@@ -1527,6 +2152,35 @@ version = "0.51.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5"
[[package]]
name = "writeable"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9edde0db4769d2dc68579893f2306b26c6ecfbe0ef499b013d731b7b9247e0b9"
[[package]]
name = "yoke"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72d6e5c6afb84d73944e5cedb052c4680d5657337201555f9f2a16b7406d4954"
dependencies = [
"stable_deref_trait",
"yoke-derive",
"zerofrom",
]
[[package]]
name = "yoke-derive"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b659052874eb698efe5b9e8cf382204678a0086ebf46982b79d6ca3182927e5d"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]]
name = "zerocopy"
version = "0.8.39"
@@ -1547,6 +2201,66 @@ dependencies = [
"syn",
]
[[package]]
name = "zerofrom"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5"
dependencies = [
"zerofrom-derive",
]
[[package]]
name = "zerofrom-derive"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]]
name = "zeroize"
version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0"
[[package]]
name = "zerotrie"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a59c17a5562d507e4b54960e8569ebee33bee890c70aa3fe7b97e85a9fd7851"
dependencies = [
"displaydoc",
"yoke",
"zerofrom",
]
[[package]]
name = "zerovec"
version = "0.11.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c28719294829477f525be0186d13efa9a3c602f7ec202ca9e353d310fb9a002"
dependencies = [
"yoke",
"zerofrom",
"zerovec-derive",
]
[[package]]
name = "zerovec-derive"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eadce39539ca5cb3985590102671f2567e659fca9666581ad3411d59207951f3"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "zmij"
version = "1.0.21"
+4 -1
View File
@@ -18,13 +18,16 @@ thiserror = "2"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
clap = { version = "4", features = ["derive"] }
crossbeam-channel = "0.5"
ureq = { version = "2", features = ["json"] }
sysinfo = "0.35"
serde_json = "1"
[features]
default = ["gauntlet"]
gauntlet = []
[dev-dependencies]
serde_json = "1"
# ---------------------------------------------------------------------------
# Explicit test target for the Layer 3 integration module (D-030, ticket #200).
+345
View File
@@ -0,0 +1,345 @@
//! MessagePack voice cache (D-138, Spike 2).
//!
//! Stores re-voiced text keyed by (npc, content, tell state, culture).
//! Length-gated variant count: short lines cache neutral only, medium lines
//! cache 3 variants, long lines cache all applicable tells.
//!
//! Baked content is just pre-populated cache — `make voice-bake` writes to
//! the same directory. No separate baked path.
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::io;
use std::path::PathBuf;
use crate::npc::tell_state::TellCategory;
use crate::voice::prompt_builder::ContentType;
/// Cache key for a single voiced line.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CacheKey {
pub culture_id: String,
pub npc_stable_id: u64,
pub content_type: ContentType,
pub content_index: u16,
/// Tell state variant. `None` = neutral (used for short content).
pub tell_state: Option<TellCategory>,
}
/// A single zone's voice cache — maps cache keys to voiced text.
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct ZoneVoiceCache {
/// Model version hash — cache miss if this doesn't match.
pub model_version: String,
/// Injector version hash — cache miss if this doesn't match.
pub injector_version: String,
/// Cached voiced lines.
pub entries: HashMap<CacheKey, String>,
}
impl ZoneVoiceCache {
pub fn new(model_version: String, injector_version: String) -> Self {
Self {
model_version,
injector_version,
entries: HashMap::new(),
}
}
/// Look up a cached voiced line. Returns `None` on miss.
pub fn lookup(&self, key: &CacheKey) -> Option<&str> {
self.entries.get(key).map(|s| s.as_str())
}
/// Store a voiced line in the cache.
pub fn store(&mut self, key: CacheKey, text: String) {
self.entries.insert(key, text);
}
/// Number of cached entries.
pub fn len(&self) -> usize {
self.entries.len()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
}
/// Manages voice caches across zones with disk persistence.
#[derive(Debug)]
pub struct VoiceCacheStore {
/// Base directory for cache files.
base_dir: PathBuf,
/// World seed — part of the directory path.
world_seed: u64,
/// Current model version hash.
model_version: String,
/// Current injector version hash.
injector_version: String,
/// Loaded zone caches.
zones: HashMap<u32, ZoneVoiceCache>,
}
impl VoiceCacheStore {
/// Create a new cache store. Does not load any zones yet.
pub fn new(
base_dir: PathBuf,
world_seed: u64,
model_version: String,
injector_version: String,
) -> Self {
Self {
base_dir,
world_seed,
model_version,
injector_version,
zones: HashMap::new(),
}
}
/// Get or load the cache for a zone.
pub fn zone_cache(&mut self, zone_id: u32) -> &mut ZoneVoiceCache {
if !self.zones.contains_key(&zone_id) {
let cache = self.load_zone(zone_id).unwrap_or_else(|| {
ZoneVoiceCache::new(
self.model_version.clone(),
self.injector_version.clone(),
)
});
self.zones.insert(zone_id, cache);
}
self.zones.get_mut(&zone_id).unwrap()
}
/// Look up a voiced line across the right zone cache.
pub fn lookup(&mut self, zone_id: u32, key: &CacheKey) -> Option<String> {
let cache = self.zone_cache(zone_id);
cache.lookup(key).map(|s| s.to_string())
}
/// Store a voiced line and return the stored text.
pub fn store(&mut self, zone_id: u32, key: CacheKey, text: String) {
let cache = self.zone_cache(zone_id);
cache.store(key, text);
}
/// Persist a zone's cache to disk as MessagePack.
pub fn save_zone(&self, zone_id: u32) -> io::Result<()> {
let Some(cache) = self.zones.get(&zone_id) else {
return Ok(());
};
let dir = self.zone_dir();
fs::create_dir_all(&dir)?;
let path = dir.join(format!("{}.msgpack", zone_id));
let data = rmp_serde::to_vec(cache)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
fs::write(path, data)
}
/// Save all loaded zone caches to disk.
pub fn save_all(&self) -> io::Result<()> {
for &zone_id in self.zones.keys() {
self.save_zone(zone_id)?;
}
Ok(())
}
/// Load a zone cache from disk. Returns `None` if file doesn't exist
/// or version mismatch (cache invalidation).
fn load_zone(&self, zone_id: u32) -> Option<ZoneVoiceCache> {
let path = self.zone_dir().join(format!("{}.msgpack", zone_id));
let data = fs::read(&path).ok()?;
let cache: ZoneVoiceCache = rmp_serde::from_slice(&data).ok()?;
// Version check — invalidate on mismatch
if cache.model_version != self.model_version
|| cache.injector_version != self.injector_version
{
tracing::info!(
zone_id,
"voice cache version mismatch — invalidating"
);
return None;
}
tracing::debug!(zone_id, entries = cache.entries.len(), "loaded voice cache");
Some(cache)
}
fn zone_dir(&self) -> PathBuf {
self.base_dir.join(format!("{}", self.world_seed))
}
}
/// Determine which tell states should be cached for a given base text.
///
/// Length-gated variant count (Spike 1 finding):
/// - Short (≤7 words): neutral only — 2B model can't differentiate
/// - Medium (815 words): neutral + Angry + Guarded (3 variants)
/// - Long (16+ words): all 5 tells + neutral (6 variants)
pub fn cacheable_tells(base_text: &str) -> Vec<Option<TellCategory>> {
let words = base_text.split_whitespace().count();
if words <= 7 {
vec![None] // neutral only
} else if words <= 15 {
vec![None, Some(TellCategory::Angry), Some(TellCategory::Guarded)]
} else {
vec![
None,
Some(TellCategory::Nervous),
Some(TellCategory::Angry),
Some(TellCategory::Friendly),
Some(TellCategory::Guarded),
Some(TellCategory::RoutineDeviation),
]
}
}
// ---------------------------------------------------------------------------
// Make ContentType serializable for cache keys
// ---------------------------------------------------------------------------
impl Serialize for ContentType {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
ContentType::Dialogue => serializer.serialize_u8(0),
ContentType::Behavior => serializer.serialize_u8(1),
}
}
}
impl<'de> Deserialize<'de> for ContentType {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let v = u8::deserialize(deserializer)?;
match v {
0 => Ok(ContentType::Dialogue),
1 => Ok(ContentType::Behavior),
_ => Err(serde::de::Error::custom("invalid ContentType")),
}
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn zone_cache_store_and_lookup() {
let mut cache = ZoneVoiceCache::new("v1".into(), "i1".into());
let key = CacheKey {
culture_id: "krenn".into(),
npc_stable_id: 42,
content_type: ContentType::Dialogue,
content_index: 0,
tell_state: None,
};
cache.store(key.clone(), "Look, that's not mine to say.".into());
assert_eq!(cache.lookup(&key), Some("Look, that's not mine to say."));
}
#[test]
fn zone_cache_miss_returns_none() {
let cache = ZoneVoiceCache::new("v1".into(), "i1".into());
let key = CacheKey {
culture_id: "krenn".into(),
npc_stable_id: 42,
content_type: ContentType::Dialogue,
content_index: 0,
tell_state: None,
};
assert_eq!(cache.lookup(&key), None);
}
#[test]
fn zone_cache_round_trips_through_msgpack() {
let mut cache = ZoneVoiceCache::new("v1".into(), "i1".into());
let key = CacheKey {
culture_id: "krenn".into(),
npc_stable_id: 42,
content_type: ContentType::Behavior,
content_index: 3,
tell_state: Some(TellCategory::Nervous),
};
cache.store(key.clone(), "Hands are steady. Eyes aren't.".into());
let data = rmp_serde::to_vec(&cache).unwrap();
let restored: ZoneVoiceCache = rmp_serde::from_slice(&data).unwrap();
assert_eq!(restored.lookup(&key), Some("Hands are steady. Eyes aren't."));
assert_eq!(restored.model_version, "v1");
}
#[test]
fn cache_store_persists_and_loads() {
let dir = std::env::temp_dir().join("sr-voice-cache-test");
let _ = fs::remove_dir_all(&dir);
let mut store = VoiceCacheStore::new(dir.clone(), 12345, "v1".into(), "i1".into());
let key = CacheKey {
culture_id: "krenn".into(),
npc_stable_id: 1,
content_type: ContentType::Dialogue,
content_index: 0,
tell_state: None,
};
store.store(100, key.clone(), "Hey.".into());
store.save_zone(100).unwrap();
// New store instance — loads from disk
let mut store2 = VoiceCacheStore::new(dir.clone(), 12345, "v1".into(), "i1".into());
assert_eq!(store2.lookup(100, &key), Some("Hey.".into()));
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn cache_invalidation_on_version_mismatch() {
let dir = std::env::temp_dir().join("sr-voice-cache-invalidation-test");
let _ = fs::remove_dir_all(&dir);
let mut store = VoiceCacheStore::new(dir.clone(), 42, "v1".into(), "i1".into());
let key = CacheKey {
culture_id: "krenn".into(),
npc_stable_id: 1,
content_type: ContentType::Dialogue,
content_index: 0,
tell_state: None,
};
store.store(1, key.clone(), "Old text.".into());
store.save_zone(1).unwrap();
// Different model version — should invalidate
let mut store2 = VoiceCacheStore::new(dir.clone(), 42, "v2".into(), "i1".into());
assert_eq!(store2.lookup(1, &key), None);
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn cacheable_tells_short() {
let tells = cacheable_tells("Hello there.");
assert_eq!(tells.len(), 1);
assert_eq!(tells[0], None);
}
#[test]
fn cacheable_tells_medium() {
let tells = cacheable_tells("The overnight delivery came in clean and it was logged");
assert_eq!(tells.len(), 3);
}
#[test]
fn cacheable_tells_long() {
let tells = cacheable_tells(
"I heard the night crew had to stop the line twice because the coupling was faulty and nobody had flagged it"
);
assert_eq!(tells.len(), 6);
}
}
+3
View File
@@ -12,4 +12,7 @@
//! - `worker` — inference worker pool (dynamic scaling, owns sr-voice HTTP clients)
//! - `hardware` — hardware detection + dynamic sr-voice instance management
pub mod cache;
pub mod prompt_builder;
pub mod queue;
pub mod worker;
+325
View File
@@ -0,0 +1,325 @@
//! Voice pipeline work queue (D-138, Spike 2).
//!
//! Bounded crossbeam channel with priority ordering and backpressure.
//! Queue full → request dropped silently, game serves base text.
use std::cmp::Ordering;
use std::collections::BinaryHeap;
use std::sync::{Arc, Mutex};
use crossbeam_channel::{Receiver, Sender, TrySendError};
use crate::npc::tell_state::TellCategory;
use crate::voice::prompt_builder::ContentType;
/// Queue capacity — requests beyond this are dropped (backpressure).
const QUEUE_CAPACITY: usize = 256;
/// Priority levels for voice requests.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Priority {
/// P0 — plot-critical NPC in current zone.
Critical,
/// P1 — current zone NPCs.
High,
/// P2 — adjacent zone pre-voicing.
Standard,
/// P3 — distant zones.
Background,
}
impl Priority {
/// Lower number = higher priority (for min-heap ordering).
fn rank(self) -> u8 {
match self {
Priority::Critical => 0,
Priority::High => 1,
Priority::Standard => 2,
Priority::Background => 3,
}
}
}
/// A request to re-voice a piece of content.
#[derive(Debug, Clone)]
pub struct VoiceRequest {
pub priority: Priority,
pub npc_stable_id: u64,
pub zone_id: u32,
pub culture_id: String,
pub base_text: String,
pub content_type: ContentType,
pub content_index: u16,
pub tell_state: Option<TellCategory>,
pub seed: u64,
}
/// Wrapper for priority ordering in the heap (higher priority = dequeued first).
impl PartialEq for VoiceRequest {
fn eq(&self, other: &Self) -> bool {
self.priority.rank() == other.priority.rank()
}
}
impl Eq for VoiceRequest {}
impl PartialOrd for VoiceRequest {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for VoiceRequest {
fn cmp(&self, other: &Self) -> Ordering {
// Reverse: lower rank = higher priority = should come first
other.priority.rank().cmp(&self.priority.rank())
}
}
/// Priority-ordered voice work queue with backpressure.
///
/// Uses a crossbeam bounded channel as transport between the game thread
/// and worker pool, with a priority heap on the consumer side.
pub struct VoiceQueue {
sender: Sender<VoiceRequest>,
receiver: Receiver<VoiceRequest>,
paused: Arc<Mutex<bool>>,
}
impl VoiceQueue {
pub fn new() -> Self {
let (sender, receiver) = crossbeam_channel::bounded(QUEUE_CAPACITY);
Self {
sender,
receiver,
paused: Arc::new(Mutex::new(false)),
}
}
/// Submit a voice request. Returns `false` if the queue is full (backpressure).
pub fn submit(&self, request: VoiceRequest) -> bool {
match self.sender.try_send(request) {
Ok(()) => true,
Err(TrySendError::Full(_)) => {
tracing::trace!("voice queue full — dropping request");
false
}
Err(TrySendError::Disconnected(_)) => {
tracing::warn!("voice queue disconnected");
false
}
}
}
/// Get a clone of the receiver for worker threads.
pub fn receiver(&self) -> Receiver<VoiceRequest> {
self.receiver.clone()
}
/// Current number of pending requests in the channel.
pub fn pending_count(&self) -> usize {
self.sender.len()
}
/// Pause the queue (zone transition start).
pub fn pause(&self) {
if let Ok(mut p) = self.paused.lock() {
*p = true;
}
}
/// Resume the queue (zone transition complete).
pub fn resume(&self) {
if let Ok(mut p) = self.paused.lock() {
*p = false;
}
}
/// Check if the queue is paused.
pub fn is_paused(&self) -> bool {
self.paused.lock().map(|p| *p).unwrap_or(false)
}
/// Reprioritize all pending requests after a zone change.
///
/// Drains the channel, re-tags each request's priority using the
/// provided closure, and re-submits. Requests that no longer fit
/// (queue full after re-submission) are dropped — same backpressure
/// rule as normal submission.
///
/// Call this between `pause()` and `resume()` during zone transitions
/// so workers don't consume stale-priority requests mid-reshuffle.
pub fn reprioritize<F>(&self, mut classify: F)
where
F: FnMut(&VoiceRequest) -> Priority,
{
// Drain all pending requests
let mut pending = Vec::new();
while let Ok(req) = self.receiver.try_recv() {
pending.push(req);
}
let count = pending.len();
let mut resubmitted = 0;
// Re-tag and re-submit
for mut req in pending {
req.priority = classify(&req);
if self.submit(req) {
resubmitted += 1;
}
}
if count > 0 {
tracing::debug!(
drained = count,
resubmitted,
dropped = count - resubmitted,
"voice queue reprioritized after zone change"
);
}
}
}
/// Priority drain: collect all pending items from the channel into a
/// priority-ordered heap, then drain highest-priority first.
///
/// Used by workers to process the most important requests first when
/// multiple requests are queued.
pub struct PriorityDrain {
heap: BinaryHeap<VoiceRequest>,
}
impl PriorityDrain {
/// Drain all currently available items from the receiver into the heap.
pub fn from_receiver(receiver: &Receiver<VoiceRequest>) -> Self {
let mut heap = BinaryHeap::new();
while let Ok(req) = receiver.try_recv() {
heap.push(req);
}
Self { heap }
}
/// Pop the highest-priority request.
pub fn pop(&mut self) -> Option<VoiceRequest> {
self.heap.pop()
}
pub fn is_empty(&self) -> bool {
self.heap.is_empty()
}
pub fn len(&self) -> usize {
self.heap.len()
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
fn make_request(priority: Priority, text: &str) -> VoiceRequest {
VoiceRequest {
priority,
npc_stable_id: 1,
zone_id: 100,
culture_id: "krenn".into(),
base_text: text.into(),
content_type: ContentType::Dialogue,
content_index: 0,
tell_state: None,
seed: 42,
}
}
#[test]
fn submit_and_receive() {
let queue = VoiceQueue::new();
assert!(queue.submit(make_request(Priority::High, "Test")));
assert_eq!(queue.pending_count(), 1);
let req = queue.receiver().try_recv().unwrap();
assert_eq!(req.base_text, "Test");
}
#[test]
fn backpressure_drops_when_full() {
let (sender, _receiver) = crossbeam_channel::bounded(2);
// Fill the channel
sender.try_send(make_request(Priority::High, "A")).unwrap();
sender.try_send(make_request(Priority::High, "B")).unwrap();
// Third should fail
assert!(sender.try_send(make_request(Priority::High, "C")).is_err());
}
#[test]
fn priority_drain_orders_correctly() {
let queue = VoiceQueue::new();
queue.submit(make_request(Priority::Background, "low"));
queue.submit(make_request(Priority::Critical, "high"));
queue.submit(make_request(Priority::Standard, "mid"));
let mut drain = PriorityDrain::from_receiver(&queue.receiver());
assert_eq!(drain.len(), 3);
let first = drain.pop().unwrap();
assert_eq!(first.base_text, "high");
assert_eq!(first.priority, Priority::Critical);
let second = drain.pop().unwrap();
assert_eq!(second.base_text, "mid");
let third = drain.pop().unwrap();
assert_eq!(third.base_text, "low");
}
#[test]
fn pause_and_resume() {
let queue = VoiceQueue::new();
assert!(!queue.is_paused());
queue.pause();
assert!(queue.is_paused());
queue.resume();
assert!(!queue.is_paused());
}
#[test]
fn reprioritize_reshuffles_on_zone_change() {
let queue = VoiceQueue::new();
// Zone 100 is current, zone 200 is adjacent
let mut req_a = make_request(Priority::High, "current zone NPC");
req_a.zone_id = 100;
let mut req_b = make_request(Priority::Standard, "adjacent zone NPC");
req_b.zone_id = 200;
queue.submit(req_a);
queue.submit(req_b);
assert_eq!(queue.pending_count(), 2);
// Player moves to zone 200 — reprioritize
queue.pause();
queue.reprioritize(|req| {
if req.zone_id == 200 {
Priority::High // was adjacent, now current
} else {
Priority::Background // was current, now distant
}
});
queue.resume();
// Drain with priority ordering — zone 200 should come first
let mut drain = PriorityDrain::from_receiver(&queue.receiver());
let first = drain.pop().unwrap();
assert_eq!(first.zone_id, 200);
assert_eq!(first.priority, Priority::High);
let second = drain.pop().unwrap();
assert_eq!(second.zone_id, 100);
assert_eq!(second.priority, Priority::Background);
}
}
+316
View File
@@ -0,0 +1,316 @@
//! Inference worker pool (D-138, Spike 2).
//!
//! Dynamic pool of worker threads, each owning an HTTP client to its own
//! sr-voice instance. Pool size controlled by hardware detection.
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle};
use std::time::Duration;
use crossbeam_channel::Receiver;
use crate::npc::blueprint::CultureProfile;
use crate::voice::cache::{CacheKey, VoiceCacheStore};
use crate::voice::prompt_builder;
use crate::voice::queue::VoiceRequest;
/// Minimum token count for a valid response. Below this, retry once.
const MIN_TOKENS: usize = 4;
/// How long to wait before retrying connection to sr-voice.
const _RECONNECT_INTERVAL: Duration = Duration::from_secs(30);
/// HTTP request timeout for inference calls.
const INFERENCE_TIMEOUT: Duration = Duration::from_secs(60);
/// Worker pool manages inference worker threads.
pub struct WorkerPool {
workers: Vec<WorkerHandle>,
shutdown: Arc<AtomicBool>,
active_count: Arc<AtomicUsize>,
}
struct WorkerHandle {
thread: Option<JoinHandle<()>>,
id: usize,
}
/// Shared state passed to each worker thread.
pub struct WorkerContext {
pub receiver: Receiver<VoiceRequest>,
pub cache: Arc<Mutex<VoiceCacheStore>>,
pub cultures: Arc<HashMap<String, CultureProfile>>,
pub shutdown: Arc<AtomicBool>,
pub active_count: Arc<AtomicUsize>,
pub paused: Arc<AtomicBool>,
}
use std::collections::HashMap;
impl WorkerPool {
/// Spawn `count` worker threads, each connecting to sr-voice on
/// `base_port + worker_id`.
pub fn spawn(
count: usize,
base_port: u16,
receiver: Receiver<VoiceRequest>,
cache: Arc<Mutex<VoiceCacheStore>>,
cultures: Arc<HashMap<String, CultureProfile>>,
) -> Self {
let shutdown = Arc::new(AtomicBool::new(false));
let active_count = Arc::new(AtomicUsize::new(0));
let paused = Arc::new(AtomicBool::new(false));
let mut workers = Vec::with_capacity(count);
for id in 0..count {
let ctx = WorkerContext {
receiver: receiver.clone(),
cache: Arc::clone(&cache),
cultures: Arc::clone(&cultures),
shutdown: Arc::clone(&shutdown),
active_count: Arc::clone(&active_count),
paused: Arc::clone(&paused),
};
let port = base_port + id as u16;
let thread = thread::Builder::new()
.name(format!("voice-worker-{}", id))
.spawn(move || worker_loop(id, port, ctx))
.expect("failed to spawn voice worker thread");
workers.push(WorkerHandle {
thread: Some(thread),
id,
});
}
tracing::info!(count, base_port, "voice worker pool started");
Self {
workers,
shutdown,
active_count,
}
}
/// Number of workers currently processing a request.
pub fn active_workers(&self) -> usize {
self.active_count.load(Ordering::Relaxed)
}
/// Total number of worker threads.
pub fn worker_count(&self) -> usize {
self.workers.len()
}
/// Signal all workers to shut down and join their threads.
pub fn shutdown(&mut self) {
self.shutdown.store(true, Ordering::SeqCst);
for handle in &mut self.workers {
if let Some(thread) = handle.thread.take() {
let _ = thread.join();
tracing::debug!(id = handle.id, "voice worker joined");
}
}
}
}
impl Drop for WorkerPool {
fn drop(&mut self) {
self.shutdown();
}
}
/// Main worker loop: receive requests, build prompts, call sr-voice, cache results.
fn worker_loop(id: usize, port: u16, ctx: WorkerContext) {
let base_url = format!("http://127.0.0.1:{}", port);
tracing::debug!(id, port, "voice worker started");
// Below-normal thread priority is handled at the OS level by the
// sr-voice process itself (nice value). Worker threads inherit it.
loop {
if ctx.shutdown.load(Ordering::SeqCst) {
break;
}
// Wait for a request (with timeout so we can check shutdown)
let request = match ctx.receiver.recv_timeout(Duration::from_secs(1)) {
Ok(req) => req,
Err(crossbeam_channel::RecvTimeoutError::Timeout) => continue,
Err(crossbeam_channel::RecvTimeoutError::Disconnected) => break,
};
// Skip while paused (zone transition)
if ctx.paused.load(Ordering::Relaxed) {
// Re-queue the request — it wasn't consumed
let _ = ctx.receiver.clone(); // can't re-send, just drop during pause
continue;
}
ctx.active_count.fetch_add(1, Ordering::Relaxed);
process_request(id, &base_url, &request, &ctx);
ctx.active_count.fetch_sub(1, Ordering::Relaxed);
}
tracing::debug!(id, "voice worker stopped");
}
/// Process a single voice request: build prompt → infer → validate → cache.
fn process_request(
worker_id: usize,
base_url: &str,
request: &VoiceRequest,
ctx: &WorkerContext,
) {
let culture = match ctx.cultures.get(&request.culture_id) {
Some(c) => c,
None => {
tracing::warn!(
culture_id = %request.culture_id,
"unknown culture — serving base text"
);
cache_base_text(request, ctx);
return;
}
};
// Build prompt
let built = prompt_builder::build_prompt(
culture,
&request.base_text,
request.content_type,
request.tell_state,
request.seed,
);
// Call sr-voice
let result = call_sr_voice(base_url, &built.prompt);
match result {
Ok(text) if text.split_whitespace().count() >= MIN_TOKENS => {
cache_result(request, &text, ctx);
}
Ok(_short_text) => {
// Empty output guard: retry once with different seed
tracing::debug!(
worker_id,
npc = request.npc_stable_id,
"short output — retrying with different seed"
);
let retry_built = prompt_builder::build_prompt(
culture,
&request.base_text,
request.content_type,
request.tell_state,
request.seed.wrapping_add(1),
);
match call_sr_voice(base_url, &retry_built.prompt) {
Ok(text) if text.split_whitespace().count() >= MIN_TOKENS => {
cache_result(request, &text, ctx);
}
_ => {
// Graceful degradation: cache base text
tracing::debug!(
worker_id,
npc = request.npc_stable_id,
"retry also short — caching base text"
);
cache_base_text(request, ctx);
}
}
}
Err(e) => {
tracing::warn!(
worker_id,
error = %e,
"sr-voice request failed — serving base text"
);
cache_base_text(request, ctx);
}
}
}
/// POST to sr-voice /generate endpoint and return the generated text.
fn call_sr_voice(base_url: &str, prompt: &str) -> Result<String, String> {
let url = format!("{}/generate", base_url);
let payload = serde_json::json!({ "prompt": prompt });
let response = ureq::AgentBuilder::new()
.timeout(INFERENCE_TIMEOUT)
.build()
.post(&url)
.send_json(payload);
match response {
Ok(resp) => {
let body_str = resp
.into_string()
.map_err(|e| format!("failed to read response: {}", e))?;
let body: serde_json::Value = serde_json::from_str(&body_str)
.map_err(|e| format!("failed to parse JSON: {}", e))?;
body["text"]
.as_str()
.map(|s| s.trim().to_string())
.ok_or_else(|| "response missing 'text' field".to_string())
}
Err(e) => Err(format!("HTTP error: {}", e)),
}
}
/// Cache the inference result.
fn cache_result(request: &VoiceRequest, text: &str, ctx: &WorkerContext) {
let key = cache_key_from_request(request);
if let Ok(mut cache) = ctx.cache.lock() {
cache.store(request.zone_id, key, text.to_string());
}
}
/// Cache the base text as fallback (graceful degradation).
fn cache_base_text(request: &VoiceRequest, ctx: &WorkerContext) {
let key = cache_key_from_request(request);
if let Ok(mut cache) = ctx.cache.lock() {
cache.store(request.zone_id, key, request.base_text.clone());
}
}
fn cache_key_from_request(request: &VoiceRequest) -> CacheKey {
CacheKey {
culture_id: request.culture_id.clone(),
npc_stable_id: request.npc_stable_id,
content_type: request.content_type,
content_index: request.content_index,
tell_state: request.tell_state,
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cache_key_from_request_maps_fields() {
let request = VoiceRequest {
priority: crate::voice::queue::Priority::High,
npc_stable_id: 42,
zone_id: 100,
culture_id: "krenn".into(),
base_text: "Test.".into(),
content_type: crate::voice::prompt_builder::ContentType::Dialogue,
content_index: 5,
tell_state: Some(crate::npc::tell_state::TellCategory::Angry),
seed: 99,
};
let key = cache_key_from_request(&request);
assert_eq!(key.culture_id, "krenn");
assert_eq!(key.npc_stable_id, 42);
assert_eq!(key.content_index, 5);
}
}