Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
* text=auto eol=lf

*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.webp binary
50 changes: 50 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: CI

on:
push:
branches:
- main
pull_request:

permissions:
contents: read

concurrency:
group: ci-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
validate:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 22
- 24

steps:
- name: Check out repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7

- name: Set up pnpm
uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0

- name: Set up Node.js
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
with:
node-version: ${{ matrix.node-version }}
cache: pnpm

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Check formatting
run: pnpm run format:check

- name: Check types
run: pnpm run check

- name: Run tests
run: pnpm test
46 changes: 46 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: CodeQL

on:
push:
branches:
- main
pull_request:
branches:
- main
schedule:
- cron: "31 7 * * 1"

permissions:
contents: read
security-events: write

concurrency:
group: codeql-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
analyze:
name: Analyze JavaScript and TypeScript
runs-on: ubuntu-latest
timeout-minutes: 15

steps:
- name: Check out repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7

- name: Set up Node.js
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
with:
node-version: 24.18.0
package-manager-cache: false

- name: Initialize CodeQL
uses: github/codeql-action/init@e4fba868fa4b1b91e1fdab776edc8cfbe6e9fb81 # v4
with:
languages: javascript-typescript

- name: Build
uses: github/codeql-action/autobuild@e4fba868fa4b1b91e1fdab776edc8cfbe6e9fb81 # v4

- name: Analyze
uses: github/codeql-action/analyze@e4fba868fa4b1b91e1fdab776edc8cfbe6e9fb81 # v4
126 changes: 126 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Publishes exact tarballs prepared from a signed stable release tag.
# Authentication is npm trusted-publisher OIDC only; no token fallback exists.

name: Publish

on:
release:
types: [published]

permissions:
contents: read

concurrency:
group: npm-publish
cancel-in-progress: false

jobs:
prepare:
runs-on: ubuntu-latest
env:
RELEASE_TAG: ${{ github.event.release.tag_name }}
RELEASE_PRERELEASE: ${{ github.event.release.prerelease }}
RELEASE_COMMIT: ${{ github.sha }}
RELEASE_ALLOWED_SIGNERS: ${{ vars.RELEASE_ALLOWED_SIGNERS }}

steps:
- name: Check out the release tag
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
ref: refs/tags/${{ github.event.release.tag_name }}
fetch-depth: 0

- name: Fetch the protected release branch
run: git fetch --no-tags origin main:refs/remotes/origin/main

- name: Configure approved release signers
shell: bash
run: |
if [[ -z "${RELEASE_ALLOWED_SIGNERS}" ]]; then
echo "::error::RELEASE_ALLOWED_SIGNERS must contain the reviewed SSH allowed-signers entries"
exit 1
fi
allowed_signers="${RUNNER_TEMP}/release-allowed-signers"
umask 077
printf '%s\n' "${RELEASE_ALLOWED_SIGNERS}" > "${allowed_signers}"
git config --local gpg.format ssh
git config --local gpg.ssh.allowedSignersFile "${allowed_signers}"

- name: Set up Node.js
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
with:
node-version: 24.18.0
registry-url: https://registry.npmjs.org
package-manager-cache: false

- name: Enable Corepack
run: corepack enable

- name: Use the reviewed npm release
run: npm install --global npm@11.18.0

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Check formatting
run: pnpm run format:check

- name: Check types
run: pnpm run check

- name: Run tests
run: pnpm test

- name: Prepare and verify release tarballs
run: pnpm run release:pack -- --require-clean --require-main-ancestor --require-release-tag --expected-release-commit "${RELEASE_COMMIT}" --output .release

- name: Upload the prepared release
id: upload
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
with:
name: cache-core-release-${{ github.run_id }}
path: .release/
if-no-files-found: error
include-hidden-files: true
overwrite: true
retention-days: 30

- name: Record the prepared artifact digest
run: echo "Prepared artifact SHA-256 ${{ steps.upload.outputs.artifact-digest }}" >> "${GITHUB_STEP_SUMMARY}"

publish:
needs: prepare
runs-on: ubuntu-latest
environment: npm-publish
permissions:
contents: read
id-token: write
env:
RELEASE_TAG: ${{ github.event.release.tag_name }}
RELEASE_COMMIT: ${{ github.sha }}

steps:
- name: Check out the exact release tag
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
ref: refs/tags/${{ github.event.release.tag_name }}
fetch-depth: 1

- name: Set up Node.js and the npm registry
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
with:
node-version: 24.18.0
registry-url: https://registry.npmjs.org
package-manager-cache: false

- name: Use the reviewed npm release
run: npm install --global npm@11.18.0

- name: Download the prepared release
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: cache-core-release-${{ github.run_id }}
path: .release

- name: Publish verified prepared tarballs
run: node scripts/release-packages.mjs publish --expected-release-commit "${RELEASE_COMMIT}" --manifest .release/package-manifest.json
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
node_modules/
dist/
coverage/
*.tsbuildinfo
.release/

.env
.env.*
!.env.example

.DS_Store
Thumbs.db
.idea/
.vscode/
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pnpm-lock.yaml
89 changes: 89 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Working in this repository

Read this before changing anything. It is short on purpose.

## What this is part of

Cache Core is the ephemeral-caching component of **Pegma**, a family of
MIT-licensed packages a host application composes. Shared time and logging
contracts live in `@pegma/spine`. Durable state belongs in
`@pegma/storage-core` — this repository refuses that job. One repository per
component, publishing under the `@pegma` scope.

The governing principle, which every rule below follows from:

> **Optimize for a fresh agent context window.** How much must be read to make
> a correct change, and how does the change prove itself correct? Minimize the
> first, mechanize the second.

A cache that lies about freshness, stamps over a neighbor's namespace, or
falls back to unbounded local memory when Redis is down is worse than no
cache. Weigh changes accordingly.

## Hard rules

**The conformance suite is the specification.** A behaviour that is not
asserted in `@pegma/cache-conformance` is not something a host or adapter may
rely on. Add cases with the behaviour, never after.

**Injected Clock and Logger only.** TTL, sliding expiry, and XFetch read
`clock.now()`. Never call `Date.now()` on a production path. Log outcomes
coarsely (namespace, status) — never keys, values, or customer content.

**Namespaces are strict.** `formatCacheKey` is the only legal wire form.
Redis Cluster hash tags are in the contract (`hashTag` becomes `{tag}` in
the formatted key). Do not invent implicit cross-partition multi-key ops.

**Single-flight is process-local honesty.** N parallel misses for one key
must run `compute` exactly once in that process. Do not pretend this is a
distributed lock.

**Fail-open computes; fail-closed refuses.** When the backend errors, a
`fail-open` `getOrCompute` still runs `compute` and returns the value. A
`fail-closed` call returns `error` and does not compute. Never install an
unbounded in-process map as a fallback when the remote cache is down.

**No vendor clients in application code.** Adapters wrap Redis, Azure Cache,
ElastiCache, or Upstash. Hosts depend on `CacheStore`. Do not create an
adapter package until implementation begins and a named consumer exists.

**This is not durable state.** Persistence, optimistic concurrency, and
transactions belong in `@pegma/storage-core`. A cache miss is not data loss.

**Pin `@pegma/*` deps exactly.** A caret would let CI resolve a version
nobody tested against.

**Never write literal control characters into source.** Write them as escape
sequences such as backslash-u-0000 through backslash-u-001F, and verify the
bytes after any tool-assisted edit.

## Packaging traps already paid for

Each published package needs its **own** README and LICENSE inside the package
directory; npm ignores files at the repository root. Each needs `prepack`
running the build. Each package `tsconfig.json` must exclude
`src/**/*.test.ts`, or compiled tests ship to consumers.

`runNpm` / release scripts must invoke a real npm CLI. Ignore `npm_execpath`
when pnpm set it, or pack/publish silently go through pnpm.

## Workflow

Work on a `claude/*` branch and open a pull request. The gate is
`pnpm run format:check`, `pnpm run check`, `pnpm test` — all three, on Node 22
and 24.

Publishing is trusted-publisher only; no tokens exist. A release starts from a
protected signed annotated `vX.Y.Z` tag already on `origin/main`, followed by
`gh release create vX.Y.Z --verify-tag`. See `docs/RELEASING.md`.

## Where things stand

Phase 1: `@pegma/cache-core` (ports, codecs, `formatCacheKey`, single-flight
helper, in-memory store) and `@pegma/cache-conformance` (executable suite).
Redis / Azure Redis / ElastiCache / Upstash adapters are later phases — do
not create those packages here yet.

Siblings: [spine](https://github.com/pegma-dev/spine),
[storage-core](https://github.com/pegma-dev/storage-core),
[health](https://github.com/pegma-dev/health).
42 changes: 42 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Contributing to Cache Core

Thank you for helping improve Cache Core.

## Before opening an issue

- Search existing issues for related work.
- Use GitHub's private vulnerability reporting flow for security concerns.
- Describe the cache behaviour you need and which refusal it must not
violate (durability, cross-partition multi-key ops, unbounded memory
fallback, vendor clients in application code).
- If a proposal needs this package to own durable state or to create an
adapter before a consumer exists, say so explicitly — those are the
changes the design cannot absorb.

## Local development

Cache Core requires Node.js 22 or 24.

```sh
corepack enable
pnpm install --frozen-lockfile
pnpm run check
pnpm test
pnpm run format:check
```

## Pull requests

Keep pull requests focused. Include:

- the problem being solved;
- the intended component behavior;
- conformance cases for any behaviour a host or adapter may rely on;
- documentation for public API changes.

A behaviour that is not in the conformance suite is not part of the contract.

## License

By contributing, you agree that your contributions will be licensed under the
MIT License.
Loading
Loading