agentleFS
VercelKnown publishervercel/chat/packages/state-redis/AGENTS.md

chat / state-redis

AGENTS.md2.4k starsChanged 59 days agoMITView on GitHub

Guidance for coding agents working inside the Redis state adapter package (built on node-redis). The top-level repository AGENTS.md covers monorepo-wide build, lint, and release rules — read it first. This file documents the adapter-specific surface, conventions, and pitfalls. @chat-adapter/state-redis persists Chat SDK state in Redis using the redis (a.k.a. node-redis) client. It covers: - Subscriptions stored as a per-thread EXISTS flag. - Distributed locks via SET NX PX + a Lua release script for token-checked atomic release. - Key/value caching…

  • Reads credentials

Trending

Quiet since March.thin dataabout the whole repository

Dec 2025: 0 mentionsJan 2026: 0 mentionsFeb 2026: 1 mentionMar 2026: 4 mentionsApr 2026: 0 mentionsMay 2026: 0 mentionsJun 2026: 0 mentionsJul 2026: 0 mentionsAug 2026: 0 mentionsSep 2026: 0 mentionsOct 2026: 0 mentionsNov 2026: 0 mentions
Dec 2025Jun 2026Nov 2026
Mentions a month of the vercel/chat repository that our weekly crawl found: only 5 in all, so this is a hint, not a trend. There is no adoption data for this file yet.

The file

We split this AGENTS.md into the rules it gives an agent

6 rules. About 0 are common, 2 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    When neither url nor client is provided, the adapter falls back to REDIS_URL.

    line 86
  2. Very specific:

    All keys are prefixed with keyPrefix: (default "chat-sdk:").

    line 94
  3. Very specific:

    If you bring your own client:

    line 163
  4. Distinctive:

    If your client has its own retry/timeout logic, it takes precedence.

    line 167
  5. Distinctive:

    Use named exports throughout.

    line 192
  6. Very specific:

    All key construction goes through a single prefixedKey helper — never inline string concatenation.

    line 193

≈ “Common” is an estimate: at least 1 in 5 of the 16,595 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
AGENTS.md

AGENTS.md — @chat-adapter/state-redis

Guidance for coding agents working inside the Redis state adapter package (built on node-redis). The top-level repository AGENTS.md covers monorepo-wide build, lint, and release rules — read it first. This file documents the adapter-specific surface, conventions, and pitfalls.

Overview

@chat-adapter/state-redis persists Chat SDK state in Redis using the redis (a.k.a. node-redis) client. It covers:

  • Subscriptions stored as a per-thread EXISTS flag.
  • Distributed locks via SET NX PX + a Lua release script for token-checked atomic release.
  • Key/value caching with optional TTL via SETEX / EXPIRE.
  • Lists backed by Redis sorted sets (so list entries can have per-entry TTLs).
  • Queues backed by Redis sorted sets keyed on enqueue time.
  • Automatic key prefixing for multi-tenant isolation.

This is the recommended state adapter for production deployments on managed Redis services (Vercel KV, Upstash, AWS ElastiCache, Redis Cloud, etc.).

Directory layout

packages/state-redis/
├── src/
│   ├── index.ts             # RedisStateAdapter + createRedisState
│   └── index.test.ts        # full StateAdapter contract suite
├── package.json
├── tsconfig.json
├── tsup.config.ts
├── vitest.config.ts
└── README.md

The package is intentionally small. Anything generic to Redis adapters lives here; the ioredis variant in state-ioredis re-implements the same surface against a different client.

Build, test, typecheck

pnpm build
pnpm dev
pnpm test
pnpm test:watch
pnpm typecheck
pnpm clean

# from repo root
pnpm --filter @chat-adapter/state-redis build
pnpm --filter @chat-adapter/state-redis test

The unit tests use an in-memory Redis stub backed by vi.fn(). There is no requirement to run a real Redis server for the contract tests.

Public surface

Main exports from src/index.ts:

  • createRedisState(config?) — primary factory. Auto-detects REDIS_URL (a.k.a. KV_URL for Vercel KV).
  • RedisStateAdapter class — implements the Chat SDK StateAdapter interface. Public methods cover subscriptions, locks, cache, lists, queues, plus a disconnect() for graceful shutdown.
  • RedisStateAdapterConfig — configuration type.

Configuration

createRedisState({
  url: process.env.REDIS_URL,    // or KV_URL
  client: existingRedisClient,    // bring your own connected client
  keyPrefix: "chat-sdk",          // namespace prefix
  logger: customLogger,
});

When neither url nor client is provided, the adapter falls back to REDIS_URL. Connection setup is lazy — the first call connects the client and subsequent calls reuse it. The adapter never owns the client lifecycle when the caller passes one in via client; in that case disconnect() is a no-op.

Internal key layout

All keys are prefixed with keyPrefix: (default "chat-sdk:").

{prefix}:subs:{threadId}            # SET-style subscription marker
{prefix}:lock:{lockId}              # STRING with token value, PX TTL
{prefix}:cache:{cacheKey}           # STRING value, optional EXPIRE
{prefix}:list:{listKey}             # ZSET entries, score = epoch ms
{prefix}:queue:{queueKey}           # ZSET entries, score = epoch ms
{prefix}:queue:{queueKey}:dedupe    # SET of dedupe keys (queue items only)

Sorted-set storage is used for lists and queues so per-entry TTLs are achievable without KEYSPACE-NOTIFICATIONS magic. Reads filter expired entries by score range; periodic ZREMRANGEBYSCORE calls keep the index size bounded (the adapter runs them inline on each write).

Locking semantics

acquireLock(lockId, ttlMs):

  • Generates a unique token (UUID v4).
  • Issues SET {prefix}:lock:{lockId} {token} NX PX {ttlMs}.
  • Returns { token } on success, null on contention.

releaseLock(lockId, token):

  • Runs an embedded Lua script that compares the stored token against the supplied one and DELs only on match.
  • Returns true if the lock was held by the caller and released, false otherwise.

forceReleaseLock(lockId) is DEL {prefix}:lock:{lockId} — unconditional.

The Lua script is loaded with SCRIPT LOAD on first use and cached by sha. If a Redis NOSCRIPT error fires, the adapter retries with EVAL.

Capabilities

  • Persistence — yes (Redis durability rules apply).
  • Multi-instance — yes.
  • Subscriptions — yes.
  • Distributed locking — yes, atomic via Lua release script.
  • Key-value cache — yes, with TTL.
  • Lists — yes, with per-entry TTL via ZSET scores.
  • Queues — yes, with per-entry TTL + dedupe set.
  • Automatic reconnect — yes (delegated to node-redis).
  • Cluster — yes, when the supplied client is a Redis Cluster instance. Key prefixes use {} braces around the hash slot to avoid CROSSSLOT errors on multi-key operations.
  • Sentinel — via client — pass a Sentinel-aware node-redis client.
  • Key prefix namespacing — yes.

Vercel KV

Vercel KV is API-compatible with node-redis. The adapter detects KV deployments by reading KV_URL (alias of REDIS_URL) and configuring TLS automatically. No additional config needed.

Connection management

The adapter uses one Redis connection per RedisStateAdapter instance. All commands run on the same client; transactions (MULTI/EXEC) are used for the rare composite operations. Pipelines are not used because the per-call latency is not the bottleneck.

If you bring your own client:

  • The adapter assumes the client is already connected.
  • The adapter does not call disconnect() on it.
  • If your client has its own retry/timeout logic, it takes precedence.

Error handling

AuthenticationError is thrown for WRONGPASS/NOAUTH errors, AdapterRateLimitError for LOADING (Redis is loading the data set), and NetworkError for connection failures. Other errors propagate as plain Error instances since they typically indicate programmer error (bad key, wrong type, etc.).

Testing approach

  • The contract tests in index.test.ts mirror the state-memory suite. Both packages must stay green against the same set of assertions.
  • The Redis client is replaced with a vi.fn()-backed stub that records commands and returns canned responses. Reach for the stub helpers (makeMockRedis) rather than rolling your own.
  • Real-Redis tests live in packages/integration-tests/src/state-*.test.ts and run against a local Redis instance only when REDIS_URL is exported.

Coding conventions

  • Use named exports throughout. No default exports.
  • All key construction goes through a single prefixedKey helper — never inline string concatenation.
  • Wrap multi-step operations in MULTI/EXEC when atomicity matters (e.g. queue dedupe + enqueue).
  • TTLs are stored as absolute epoch milliseconds for ZSET-backed structures and converted to relative ms (PX) for STRING-backed ones.
  • Top-level regex literals only.
  • Errors thrown to chat-sdk callers come from @chat-adapter/shared (AuthenticationError, AdapterRateLimitError, NetworkError).

Releases

Behavioural changes need a changeset (pnpm changeset, choose @chat-adapter/state-redis plus chat if a public StateAdapter type changed). README and AGENTS.md edits don't.

Where to look next

  • User-facing docs: apps/docs/content/adapters/official/redis.mdx
  • README: packages/state-redis/README.md
  • Core StateAdapter contract: packages/chat/src/state.ts
  • Sibling state adapters:
    • packages/state-memory
    • packages/state-ioredis
    • packages/state-pg

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.