Repository navigation
feat(plugin-cache): add a pluggable codec for the Redis store - #139
Conversation
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
There was a problem hiding this comment.
Important
The PR's documented "switching a cache's codec is always safe" guarantee only holds in one direction — see the inline comment on redis.ts.
Reviewed changes
Reviewed the full diff (14 files, 1 commit) adding a pluggable CacheCodec to packages/plugin-cache's Redis store.
CacheCodec+jsonCodec(): newpackages/plugin-cache/src/lib/codec.ts, re-exported from the package root;jsonCodec()matches the pre-existingJSON.stringify/JSON.parsebehavior byte-for-byte.msgpackCodec(): newpackages/plugin-cache/src/msgpack.ts, backed bymsgpackr, exposed only from a@wolfstar/plugin-cache/msgpacksubpath (optional peer dependency) — mirrors howplugin-loggerisolates optional transports.RedisEntityCache/createRedisCache: gain acodecoption (defaultjsonCodec()), threaded throughserialize/deserialize; encoded values are tagged with the codec's name (""for the default, preserving backward compatibility) alongside the existing compression marker, plus a newb64:marker for uncompressedBuffer-returning codecs.- Docs/tests/changeset: README documents the new option and subpath, a
minorchangeset is added, andtests/codec.test.ts,tests/msgpack.test.ts,tests/redis.test.tscover round-tripping and the tagging scheme.
ℹ️ Nitpicks
packages/plugin-cache/src/lib/redis.ts:172— see inline comment:#codecMarkercollides with reserved prefixes for unlucky custom codec names.
Claude Sonnet | 𝕏
RedisEntityCache#deserialize's fallback only stripped the marker of the
currently configured codec, then decoded with jsonCodec() unconditionally.
A value tagged for a different, non-default codec (e.g. msgpack) kept its
marker prefix and failed to parse as JSON, so reconfiguring a cache away
from a non-default codec broke reading every entry written under it -
the reverse of the documented "switching is always safe" guarantee.
Add a legacyCodecs option: former codec values a cache's entries may
still be tagged with, tried in order before falling back to jsonCodec().
Also reject codec names that collide with the reserved compression/
raw-binary markers ("gz", "br", "b64").
Found by pullfrog's review on #139 (redis.ts:385, redis.ts:172).
@wolfstar/plugin-api
@wolfstar/plugin-cache
@wolfstar/plugin-gateway
@wolfstar/plugin-i18next
@wolfstar/plugin-logger
@wolfstar/plugin-sharder
@wolfstar/plugin-subcommands-advanced
commit: |
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Reviewed the two commits pushed since the prior Pullfrog review — a fix for the codec-migration-safety gap plus an unrelated revert.
- Made codec migration safety actually bidirectional: added a
legacyCodecs?: readonly CacheCodec[]option toRedisEntityCacheOptions/RedisCacheOptions;RedisEntityCache#resolveCodecnow tries the currentcodec, then eachlegacyCodecsentry in order, then falls back tojsonCodec(). - Validated reserved codec names: a new
codecMarker()helper throwsRangeErrorforcodec/legacyCodecsentries named"gz","br", or"b64"(would collide with the compression/raw-binary markers); a"json"-named codec is now explicitly documented as always being treated as the untagged default. - Narrowed the documentation: changeset, README, design spec, and JSDoc now correctly scope the "switching is safe" guarantee to default → any codec, and describe
legacyCodecsas the mechanism for the reverse direction. - Added tests: reverse-direction failure without
legacyCodecs, success withlegacyCodecs, migrating between two non-default codecs, and reserved-name rejection for bothcodecandlegacyCodecs. - Reverted an unrelated
.github/workflows/semantic-pull-requests.ymlchange that the push token couldn't push (missingworkflowOAuth scope) — out of scope forplugin-cache; the commit message notes a human still needs to add theplugin-brokerscope entry separately for PR #140.
Both threads from the prior review (redis.ts:385 bidirectional safety, redis.ts:172 reserved-name collision) are fixed as described and have been replied to and resolved. Ran the full plugin-cache suite (43/43 passing, including the new cases) and oxlint src (clean).
Claude Sonnet | 𝕏
createRedisCache/RedisEntityCache accept a codec option (default jsonCodec(), matching prior behavior), with a msgpackCodec() available from the @wolfstar/plugin-cache/msgpack subpath so the optional msgpackr peer dependency is only resolved when actually imported. Encoded values are tagged with the codec's name, so switching codecs never breaks reading entries written under a previous one. Closes #137.
RedisEntityCache#deserialize's fallback only stripped the marker of the
currently configured codec, then decoded with jsonCodec() unconditionally.
A value tagged for a different, non-default codec (e.g. msgpack) kept its
marker prefix and failed to parse as JSON, so reconfiguring a cache away
from a non-default codec broke reading every entry written under it -
the reverse of the documented "switching is always safe" guarantee.
Add a legacyCodecs option: former codec values a cache's entries may
still be tagged with, tried in order before falling back to jsonCodec().
Also reject codec names that collide with the reserved compression/
raw-binary markers ("gz", "br", "b64").
Found by pullfrog's review on #139 (redis.ts:385, redis.ts:172).
The push token lacks the workflow OAuth scope, which GitHub requires to push any change under .github/workflows/. Reverting this one file from the previous commit so the rest of the fix can land; the "plugin-broker" scope entry needs to be added by someone with that scope (or via the GitHub UI) for PR #140's title check to pass.
c769e90 to
43791fb
Compare

🔗 Linked issue
resolves #137
🧭 Context
createRedisCachealways serialized values as JSON before optionally compressing them, with no way to plug in another format. For large bots most Redis memory goes intomembers,usersandmessages, and raw API JSON is verbose (snowflakes as strings, repeated key names, nested objects). This is the first, MVP slice of the RFC in #137: a pluggableCacheCodec, JSON by default and a msgpack codec available opt-in. Snowflake packing, per-entityproject, and a benchmark script are deliberately left to follow-up issues so this PR stays focused on the codec mechanism itself.📚 Description
CacheCodec(name,encode,decode) and the defaultjsonCodec()inpackages/plugin-cache/src/lib/codec.ts, re-exported from the package root.RedisEntityCacheOptions/RedisCacheOptionsgain acodecoption (defaultjsonCodec()), threaded throughcreateRedisCache.RedisEntityCache#serialize/#deserializenow encode/decode through the configured codec instead of hardcodedJSON.stringify/JSON.parse. Encoded values are tagged with the codec's name (empty for the defaultjsonCodec, so every value already in production stays byte-compatible), the same way compressed values are already tagged today. A value not tagged for the currently configured codec falls back tojsonCodec(), so switching a cache'scodecoption is always safe for entries written under a previous one.msgpackCodec(), backed bymsgpackr(handlesbigintand binary natively), exposed only from a new@wolfstar/plugin-cache/msgpacksubpath — mirroring howplugin-loggerisolates its optional transports — so importing the package's main entrypoint never resolves the optionalmsgpackrpeer dependency.minor).Key changes
serialize/deserializehardcodeJSON.stringify/JSON.parseCacheCodec, defaulting to the same JSON behaviormsgpackCodec()available via@wolfstar/plugin-cache/msgpackType of Change
Pre-flight Checklist
pnpm changeset(orpnpm changeset add --emptyif no release is needed). See .changeset/README.md.