Skip to content

feat(plugin-cache): add a pluggable codec for the Redis store - #139

Merged
RedStar071 merged 3 commits into
mainfrom
137-plugin-cache-codec
Sep 27, 2026
Merged

RedStar071 merged 3 commits into
mainfrom
137-plugin-cache-codec

Conversation

@RedStar071

Copy link
Copy Markdown
Member

🔗 Linked issue

resolves #137

🧭 Context

createRedisCache always serialized values as JSON before optionally compressing them, with no way to plug in another format. For large bots most Redis memory goes into members, users and messages, 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 pluggable CacheCodec, JSON by default and a msgpack codec available opt-in. Snowflake packing, per-entity project, and a benchmark script are deliberately left to follow-up issues so this PR stays focused on the codec mechanism itself.

📚 Description

  • Added CacheCodec (name, encode, decode) and the default jsonCodec() in packages/plugin-cache/src/lib/codec.ts, re-exported from the package root.
  • RedisEntityCacheOptions/RedisCacheOptions gain a codec option (default jsonCodec()), threaded through createRedisCache.
  • RedisEntityCache#serialize/#deserialize now encode/decode through the configured codec instead of hardcoded JSON.stringify/JSON.parse. Encoded values are tagged with the codec's name (empty for the default jsonCodec, 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 to jsonCodec(), so switching a cache's codec option is always safe for entries written under a previous one.
  • Added msgpackCodec(), backed by msgpackr (handles bigint and binary natively), exposed only from a new @wolfstar/plugin-cache/msgpack subpath — mirroring how plugin-logger isolates its optional transports — so importing the package's main entrypoint never resolves the optional msgpackr peer dependency.
  • Documented the new option and subpath in the package README, and added a changeset (minor).

Key changes

Before After
serialize/deserialize hardcode JSON.stringify/JSON.parse Both go through a pluggable CacheCodec, defaulting to the same JSON behavior
No way to use a denser format for Redis-cached entries msgpackCodec() available via @wolfstar/plugin-cache/msgpack
Only compression is tagged for safe migration Codec name is tagged too, so switching codecs is also migration-safe

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation change
  • Workflow / CI change

Pre-flight Checklist

  • I have read the Contributing Guidelines (if applicable)
  • My code follows the code style of this project
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly
  • I added a changeset with pnpm changeset (or pnpm changeset add --empty if no release is needed). See .changeset/README.md.

@RedStar071 RedStar071 added the packages:plugin-cache Changes in plugin-cache label Sep 27, 2026
@socket-security

socket-security Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedmsgpackr@​2.1.09910010093100

View full report

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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(): new packages/plugin-cache/src/lib/codec.ts, re-exported from the package root; jsonCodec() matches the pre-existing JSON.stringify/JSON.parse behavior byte-for-byte.
  • msgpackCodec(): new packages/plugin-cache/src/msgpack.ts, backed by msgpackr, exposed only from a @wolfstar/plugin-cache/msgpack subpath (optional peer dependency) — mirrors how plugin-logger isolates optional transports.
  • RedisEntityCache/createRedisCache: gain a codec option (default jsonCodec()), threaded through serialize/deserialize; encoded values are tagged with the codec's name ("" for the default, preserving backward compatibility) alongside the existing compression marker, plus a new b64: marker for uncompressed Buffer-returning codecs.
  • Docs/tests/changeset: README documents the new option and subpath, a minor changeset is added, and tests/codec.test.ts, tests/msgpack.test.ts, tests/redis.test.ts cover round-tripping and the tagging scheme.

ℹ️ Nitpicks

  • packages/plugin-cache/src/lib/redis.ts:172 — see inline comment: #codecMarker collides with reserved prefixes for unlucky custom codec names.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using Claude Sonnet | 𝕏

Comment thread packages/plugin-cache/src/lib/redis.ts Resolved
Comment thread packages/plugin-cache/src/lib/redis.ts Resolved
@RedStar071
RedStar071 added this pull request to stack #141 September 27, 2026 12:39
RedStar071 added a commit that referenced this pull request Sep 27, 2026
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).
@pkg-pr-new

pkg-pr-new Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@wolfstar/plugin-api

pnpm add https://pkg.pr.new/@wolfstar/plugin-api@139

@wolfstar/plugin-cache

pnpm add https://pkg.pr.new/@wolfstar/plugin-cache@139

@wolfstar/plugin-gateway

pnpm add https://pkg.pr.new/@wolfstar/plugin-gateway@139

@wolfstar/plugin-i18next

pnpm add https://pkg.pr.new/@wolfstar/plugin-i18next@139

@wolfstar/plugin-logger

pnpm add https://pkg.pr.new/@wolfstar/plugin-logger@139

@wolfstar/plugin-sharder

pnpm add https://pkg.pr.new/@wolfstar/plugin-sharder@139

@wolfstar/plugin-subcommands-advanced

pnpm add https://pkg.pr.new/@wolfstar/plugin-subcommands-advanced@139

commit: 43791fb

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ 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 to RedisEntityCacheOptions/RedisCacheOptions; RedisEntityCache#resolveCodec now tries the current codec, then each legacyCodecs entry in order, then falls back to jsonCodec().
  • Validated reserved codec names: a new codecMarker() helper throws RangeError for codec/legacyCodecs entries 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 legacyCodecs as the mechanism for the reverse direction.
  • Added tests: reverse-direction failure without legacyCodecs, success with legacyCodecs, migrating between two non-default codecs, and reserved-name rejection for both codec and legacyCodecs.
  • Reverted an unrelated .github/workflows/semantic-pull-requests.yml change that the push token couldn't push (missing workflow OAuth scope) — out of scope for plugin-cache; the commit message notes a human still needs to add the plugin-broker scope 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).

Pullfrog  | View workflow run | Using 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.
@RedStar071
RedStar071 force-pushed the 137-plugin-cache-codec branch from c769e90 to 43791fb Compare September 27, 2026 13:27
@RedStar071
RedStar071 merged commit f8a0bc0 into main Sep 27, 2026
16 checks passed
@RedStar071
RedStar071 deleted the 137-plugin-cache-codec branch September 27, 2026 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

packages:plugin-cache Changes in plugin-cache

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RFC: pluggable codec and opt-in projection for @wolfstar/plugin-cache's Redis store

1 participant