Skip to main content

platform-state — shared-state seam (memory reference + Redis)

@demystify/platform-state (TS) · demystify-platform-state (Py) · v0.2.0 · MIT

What it is / when to use it

The cross-instance shared-state seam for the platform. Five ports, each with a semantically faithful in-memory reference adapter (the keyless/offline default) and a Redis adapter (opt-in, integration-marked). Backs gateway multi-instance correctness (KV, atomic budget counters, priority queue) and the voice session registry across instances (leased registry), plus event/health fan-out (pub/sub).

Leaf package: no module dependency; ioredis (TS) / redis (Py) are optional integration-only deps, imported lazily.

Ports

port methods backs
KvStore get · set(key,val,ttlMs?) · del/delete · ttl caches, flags
AtomicCounter get · incrby · reserve(key,amount,limit) · reconcile(res,actual) · release(res) budgets / rate limits
PriorityQueueStore enqueue(q,item,priority,ts?) · drain(q,max) · peek · depth gateway request queue
LeasedRegistry register(id,val,ttlMs) · renew · list · get · evict voice session registry
PubSub publish(ch,msg) · subscribe(ch,handler) → unsubscribe events + provider-health

reserve → reconcile (the budget protocol gateway needs)

res = counter.reserve("budget:tenant", estimatedMicroUsd, ceilingMicroUsd)
if (res === null)  → over budget, reject the request
... make the provider call ...
counter.reconcile(res, actualMicroUsd)   // adjusts by (actual - estimate)
// or counter.release(res)               // cancel (actual = 0)

reserve atomically books the estimate only if current + amount <= limit; reconcile/release are idempotent per reservation. The Redis adapter does the check-and-add in a Lua script so it is atomic across instances.

PriorityQueueStore ordering

drain returns items priority desc, then ts asc, then insertion order. The Redis adapter encodes score = -priority*1e13 + ts and uses ZPOPMIN.

LeasedRegistry (voice sessions across instances)

Entries self-expire; list returns only unexpired leases. An instance registers a session lease and renews it on a heartbeat; a crashed instance's sessions disappear after the TTL. Memory expiry is driven by an injectable clock (ManualClock) so tests are deterministic.

SDK usage

import { createMemoryState, ManualClock } from "@demystify/platform-state";
const state = createMemoryState();                       // keyless, offline default
await state.queue.enqueue("gateway", { id, payload }, priority);
// opt-in Redis (integration): const state = await createRedisState(process.env.REDIS_URL!);
from demystify_platform_state import create_memory_state
state = create_memory_state()
await state.queue.enqueue("gateway", QueueItemInput(id=id, payload=payload), priority)
# opt-in Redis: state = await create_redis_state(os.environ["REDIS_URL"])

Note: the KV delete method is del in TS and delete in Python (del is a Python keyword); every other method name is identical across the two ports.

Architecture & ports/adapters

port memory adapter (default) redis adapter (integration) selection
all five Memory* / create_memory_state Redis* / create_redis_state(url) memory unless a REDIS_URL is wired

Testing

make -C packages/platform-state setup lint test build. Unit tests exercise every memory port (TTL expiry, reserve/reconcile, priority order, lease expiry/eviction, pubsub delivery). Redis adapter tests are integration-marked and skip without REDIS_URL (TS: INTEGRATION=1 + REDIS_URL; Py: pytest -m integration) — never in CI. Parity: TS and Py replay fixtures/priority-order.json and fixtures/counter-reserve.json and must produce identical results.

v1 scope

Five ports, memory reference + Redis. No cluster/streams/consumer-groups (events owns durable queues via pgmq); this is the lightweight coordination seam. Redis adapter is opt-in and never the default.

Metadata

Release files for demystify-platform-state 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for demystify-platform-state 0.3.0
File Size Uploaded Provenance Actions
demystify_platform_state-0.3.0.tar.gz 66.4 kB With provenance Details for demystify_platform_state-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for demystify-platform-state 0.3.0
File Interpreter ABI Platform Actions
demystify_platform_state-0.3.0-py3-none-any.whl Python 3 none any Details for demystify_platform_state-0.3.0-py3-none-any.whl

Total release size: 77.6 kB

Release files / demystify_platform_state-0.3.0.tar.gz

Download URL demystify_platform_state-0.3.0.tar.gz
Size 66.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7d762f4c90be16149955266f1bba804b792627eb710915465bbcddc5bdb1b378
BLAKE2b-256 checksum
How to use checksums
d60e1e30c28021e0d2df523a3b2438e3545b61453330f635407fcb280082aaf5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 17, 2026.

Transparency log

Release files / demystify_platform_state-0.3.0-py3-none-any.whl

Download URL demystify_platform_state-0.3.0-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6f26036233ba89f2cbb5e54af71cd610949d71378c10a6d8ef7aeaa992dfc692
BLAKE2b-256 checksum
How to use checksums
6a3ffd6ecea30c7ee430ec74ca4c3cbe2e5aa7d6de68522dfd872539ecab1b10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 17, 2026.

Transparency log

Security for demystify-platform-state 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for demystify_platform_state-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 17, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page