Skip to content

feat(plugin-gateway,plugin-cache): synchronous reads from in-memory caches - #131

Merged
kodiakhq[bot] merged 3 commits into
mainfrom
claude/issue-121-sync-reads
Sep 26, 2026
Merged

kodiakhq[bot] merged 3 commits into
mainfrom
claude/issue-121-sync-reads

Conversation

@RedStar071

Copy link
Copy Markdown
Member

🔗 Linked issue

Part of #121 (item 8: every cache read is async, even in memory).

🧭 Context

EntityCache types every method as Awaitable<T>, so the in-memory and Redis stores can share one contract, and MemoryEntityCache#get already returns synchronously. But every CachedManager method in plugin-gateway is async. Code ported from discord.js therefore has to await every guild.members.cache.get(id), even in hot paths like message filters. Callers also had no way to know when a synchronous read was possible, because you can't tell an Awaitable apart from a promise without calling the method.

📚 Description

This adds an opt-in synchronous path for synchronous caches. None of the existing async methods change behavior.

  • EntityCache.synchronous?: a new optional readonly flag.
    • It is true on MemoryEntityCache (createInMemoryCache) and false on RedisEntityCache (createRedisCache, with or without compression).
    • A store that leaves it out counts as asynchronous, which is always safe.
  • CachedManager#cached(...ids): the synchronous counterpart of get. It takes the same arguments and builds the same structure, relations included. For example, client.members.cached(guildId, userId) or client.messages.cached(channelId, messageId). client.channels.cached(id) falls back to the threads cache, like get does.
  • When cached can't answer:
    • It returns undefined on a miss, or when the client has no cache, like get.
    • On an asynchronous cache it throws a TypeError. Such a cache can't say whether the entity is there without waiting. Returning undefined would quietly disable whatever the caller guards with it (a message filter, say) after a move to Redis.
    • It also throws when a relation lives in an asynchronous entity cache, for example a custom Cache that mixes both kinds. The dropped promise gets a .catch so it never becomes an unhandled rejection.
    • manager.cache?.synchronous tells callers which path to take.
  • How structures are built without awaiting: building a structure reads its relations (user, guild, channel, member…) from other caches. Rather than duplicating each builder:
    • The builders now return a value or a promise, through an internal whenAll helper. It works like Promise.all(values).then(cb) but stays synchronous when none of the values is a promise.
    • The public async get/hydrate/resolveData now return the internal _get/_hydrate/_resolveData, so get and cached share one builder and can't drift apart.
    • The 17 managers that resolve relations now override _hydrate instead of hydrate.

Worth knowing when reviewing:

  • The protected getByKey and cachedGuild now return a value or a promise instead of always a Promise. Every caller in this package awaits them. An outside subclass calling .then() on them would need to await instead.
  • .oxlintrc.json adds _get/_hydrate/_resolveData to plugin-gateway's no-underscore-dangle allow list, next to _add. Like _add, they are @internal methods that follow discord.js's underscore convention.

Key changes

Package Change
plugin-cache EntityCache.synchronous?; MemoryEntityCache.synchronous = true; RedisEntityCache.synchronous = false; README "Custom stores"
plugin-gateway CachedManager#cached; internal _get/_resolveData/_hydrate; relation-resolving managers override _hydrate; ChannelManager thread fallback moved into _get; whenAll/isPromiseLike in util/cache.ts; README "Synchronous reads"
tests the flag for each factory (memory; Redis with none/gzip/brotli); in memory, cached equals what get resolves, with relations; multi-id reads; thread fallback; no cache; Redis cache (throws); mixed cache (throws)
changeset minor for both packages

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 (42 files, 471 tests after merging main)
  • I have updated the documentation accordingly
  • I added a changeset with pnpm changeset

Also checked: pnpm lint, pnpm typecheck, and both package builds (attw and publint clean).

This PR and #126 (member requests) both touch GuildMemberManager.ts, in separate places. Whichever merges second may need a merge of main.

🤖 Generated with Claude Code

https://claude.ai/code/session_01RBWEHLtNzWNykJMPqeqqbB


Generated by Claude Code

…y caches

EntityCache gains an optional readonly `synchronous` flag, true on MemoryEntityCache and false on
RedisEntityCache (compressed or not); a store leaving it out is treated as asynchronous.

Every CachedManager gains `cached(...ids)`, the synchronous counterpart of `get`: it returns the
same structure, relations included, or `undefined` on a miss or without a cache, and throws a
TypeError when the entity cache, or one of the caches its relations are read from, is
asynchronous.

Structures are now built by `_hydrate`, which returns an Awaitable and stays synchronous when
every cache it reads is, so `get`, `hydrate` and `cached` share a single implementation. The
existing asynchronous methods keep their behavior.

Refs #121

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01RBWEHLtNzWNykJMPqeqqbB
@coldtea-pr-lens

coldtea-pr-lens Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

◈ PR Lens

Note

This drawing shows f0a8ca5, and the branch has new commits since. Tick Redraw to draw the latest one

  • Redraw

🟢 +0 new · 🟠 ~8 changed · 🔴 -0 removed · 1 flow · 34 files · commit f0a8ca5


Architecture

Architecture diagram for wolfstar-project/plugins at f0a8ca5

8 components touched across 5 lanes.

Play the interactive walkthrough


Inside the changed components — 2 views

Component view — Gateway Manager Hydration

Synchronous cached entity retrieval and relation hydration across gateway managers.

Architecture view of Component view — Gateway Manager Hydration in wolfstar-project/plugins

Component view — Cache Store Synchronization

Synchronous flag contracts across in-memory and Redis cache backends.

Architecture view of Component view — Cache Store Synchronization in wolfstar-project/plugins

Data flow

Data flow diagram for wolfstar-project/plugins at f0a8ca5

Synchronous cached entity retrieval

Follow each request, response and payload


View

  • Architecture lens
  • Data flow lens
  • Expand every detail

Tip

The CLI's render reads .github/pr-lens.yml and applies your renames, exclusions and lane pins at draw time

🪧 More tips
  • Run npx skills add coldteadotai/pr-lens, then tell your coding agent: "Diagram the change you just made with PR Lens and attach it to the pull request."
  • Run npx @coldtea/pr-lens-cli analyze --base origin/main on a branch, then npx @coldtea/pr-lens-cli render .pr-lens/graph.json. Same lenses, your own model key, before the pull request exists
  • Untick Architecture lens or Data flow lens under View to hide a diagram, or tick Expand every detail to open every section. The comment redraws in a few seconds
  • Click the link under each diagram to open it on a canvas you can zoom, pan and step through
  • The diagrams are links. Click one to open it on the canvas, then press W or click play to walk through the change
  • Open a diagram on the canvas, then press W or click play to walk through the change one step at a time
  • Set github.comment.collapsed: true in .github/pr-lens.yml to fold the comment behind one View architecture and data flow row. Drawing still runs as before
  • Set github.draw: on-demand in .github/pr-lens.yml and PR Lens stops drawing pull requests on its own. Comment @pr-lens draw on a pull request when you want that one drawn
  • Add .github/workflows/pr-lens.yml with coldteadotai/pr-lens/packages/action@v0 and your model provider's key as its api-key to run PR Lens from your own CI. Any /chat/completions endpoint works
  • Push a commit and the drawing stays, with a note that it is out of date. Tick Redraw in the note to draw the new head
  • Switch GitHub to dark mode and the diagrams follow. The moving dots are this pull request's data in motion

Thanks for using PR Lens! It's built by Coldtea, free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

@wolfstarbot wolfstarbot added packages:plugin-cache Changes in plugin-cache packages:plugin-gateway Changes in plugin-gateway labels Sep 26, 2026
@pkg-pr-new

pkg-pr-new Bot commented Sep 26, 2026

Copy link
Copy Markdown

Open in StackBlitz

@wolfstar/plugin-api

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

@wolfstar/plugin-cache

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

@wolfstar/plugin-gateway

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

@wolfstar/plugin-i18next

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

@wolfstar/plugin-logger

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

@wolfstar/plugin-sharder

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

@wolfstar/plugin-subcommands-advanced

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

commit: f402f2e

@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 full diff (29 files) end to end, plus surrounding call sites not touched by the PR.

  • EntityCache.synchronous?: new optional flag in plugin-cache, true on MemoryEntityCache, false on RedisEntityCache (all compression modes); absent on a custom store means asynchronous.
  • CachedManager#cached(...args): new synchronous counterpart of get, returning undefined on a miss/no-cache and throwing TypeError when the entity cache — or any cache a relation is resolved from — turns out to be asynchronous.
  • Internal _get/_resolveData/_hydrate: get/resolveData/hydrate become thin async wrappers around these, which return Awaitable<T> via the new whenAll/isPromiseLike helpers (util/cache.ts) — Promise.all-shaped but stays synchronous when nothing passed to it is a promise.
  • 17 relation-resolving managers converted from override hydrate to override _hydrate, calling other managers' _get/_resolveData instead of the public async methods, so "is this synchronous" propagates correctly through nested relation lookups (author, member, guild, channel, ...).
  • ChannelManager's thread-cache fallback moved from an override get into override _get, preserving behavior while staying sync-capable.
  • Tests added for the happy path (relations included), multi-ID reads, thread fallback, no-cache, a Redis cache (throws), and a mixed sync/async cache (throws) — all with exact assertions, not loose checks.
  • READMEs (plugin-cache, plugin-gateway) and a minor changeset for both packages document the new surface.

I traced the whenAll/isPromiseLike propagation by hand against nested and mixed-cache cases, and grepped all of packages/plugin-gateway/src/managers for any leftover override hydrate/override async get — none remain, confirming all 17 managers (matching the PR's own count) were converted consistently. The few surviving .get(/.resolveData(/.hydrate( call sites outside the diff are unrelated one-off fetch helpers, not part of any relation-resolution chain, and correctly untouched.

Pullfrog  | View workflow run | Using Claude Sonnet | 𝕏

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 packages:plugin-gateway Changes in plugin-gateway

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants