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
92 changes: 92 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# AGENTS.md

Guide for coding agents working on core-sdk. Read it before changing anything.

## Project

core-sdk (`@ethersphere/core-sdk`) is a framework-agnostic TypeScript library of [Swarm](https://www.ethswarm.org/) primitives. It builds and parses Swarm's data structures: typed bytes, content-addressed and single-owner chunks, BMT hashing, the chunk splitter and joiner, Mantaray manifests, erasure coding, encryption, postage stamps and BPS (pubsub) framing.

- **No network I/O, ever.** Talking to a Bee node belongs in consumers such as [bee-js](https://github.com/ethersphere/bee-js).
- Output must be byte-identical to Bee (Go). Check new encodings against vectors generated by Bee's own code.
- Runtime dependencies are only `@noble/curves` and `@noble/hashes`. Don't add more without a strong reason.
- Ships ESM and CJS with types, and runs in Node.js 22+ and browsers. Use only `Uint8Array` and web APIs, not Node-only modules such as `Buffer` or `fs`, in `src/`.

## Structure

| Path | Contents |
| --- | --- |
| `src/bytes/` | `Bytes` and typed fixed-length wrappers (`Reference`, `EthAddress`, `Topic`, `PrivateKey`, ...) plus encoding helpers |
| `src/crypto/` | Keccak-256, ECDSA sign/recover/verify, key derivation |
| `src/chunk/` | CAC, SOC, BMT, `ChunkSplitter`, `ChunkJoiner` |
| `src/mantaray/` | `MantarayNode` manifest trie |
| `src/erasure-coding/` | Reed-Solomon, redundancy levels, parity batching |
| `src/encryption/` | Chunk stream cipher and XOR helpers |
| `src/stamper/` | Postage stamp signing and capacity math |
| `src/bps/` | BPS-lite chunks, roster, frames and subscriber position |
| `src/<module>/index.ts` | The barrel file of each module, which is its public surface. `src/index.ts` re-exports every barrel |
| `test/*.test.ts` | vitest tests, one file per area |
| `test/regression/` | One file per fixed bug, named after the bug |
| `test/*.bench.ts`, `test/vectors/*.bench-baseline.json` | Benchmarks and their stored baselines |
| `test/fixtures/` | Large binary fixtures, generated by `test/fixtures/generate.ts` before tests run |
| `test/public-api.test.ts` | Locks the exact export list of every subpath |
| `scripts/` | esbuild build, CJS type copying, bench regression checks |

### Adding a module or export

- A new subpath needs an entry in the `package.json` `exports` map. The build reads its entry points from there (`scripts/entry-points.mjs`).
- Re-export the module from `src/index.ts` and add a row to the README table.
- Update `test/public-api.test.ts`. Every new export is a deliberate API change, and a missing name there is a breaking removal.
- Keep internals out of the barrel files. Import them by path inside `src/` instead.

## Commands

The repo uses pnpm (`pnpm-lock.yaml`).

```sh
pnpm install
pnpm test # generates fixtures, then vitest run
pnpm typecheck # tsc --noEmit
pnpm format:check # prettier
pnpm build # esbuild ESM + CJS, then tsc declarations
pnpm bench # benchmarks against the stored baseline
```

Before finishing, `pnpm test`, `pnpm typecheck` and `pnpm format:check` must pass, and `pnpm build` must succeed. If you touched hot paths (hashing, BMT, splitter, SOC, encryption, Reed-Solomon, mantaray), also run `pnpm bench`. CI compares the branch against `main` and fails on a regression above 25%.

## Coding style

- Prettier: no semicolons, single quotes, trailing commas, 2-space indent, 120 columns, `arrowParens: avoid`.
- TypeScript is strict, including `noUncheckedIndexedAccess`, `exactOptionalPropertyTypes` and no unused locals or parameters. Don't weaken `tsconfig.json`.
- ESM with `NodeNext` resolution: relative imports end in `.js` (`import { Bytes } from '../bytes/bytes.js'`).
- Functions are pure and deterministic. No global state, no I/O, and no timers.
- Accept flexible input (`Bytes | Uint8Array | string`) and normalise it right away with the typed constructors (`new EthAddress(x)`, `new Topic(x)`). Return typed wrappers, not raw arrays.
- Validate lengths and ranges at the boundary and throw `Error` (or `RangeError`) with a message naming the value, e.g. `` `BPS index out of range: ${index}` ``.
- Use `bigint` for 64-bit values such as indexes and spans.
- Prefer small, well-named functions over long ones. Match the naming and idioms of the surrounding code.

## Comments: strict rule

Write code that is easy to read, so it doesn't need comments. Comments must not explain unclear code; rewrite the code instead.

- Default to no comments.
- Before adding one, try better names, a well-named helper function or a named constant.
- If a comment is still needed, for a non-obvious "why" such as a protocol quirk or a compatibility detail with Bee, keep it to one short line.
- No multi-line block comments, no JSDoc paragraphs, no comments that restate the next line, and no commented-out code.
- When you edit code that has verbose comments, shorten or remove them.

## Tests: required

Every functionality must be covered by tests. A change without tests is not done.

- New feature: tests for the normal path, edge cases and every validation error.
- Wire formats and hashes: compare against fixed vectors generated by Bee's Go code, so compatibility is proven and not just round-trip consistency.
- Bug fix: a test that fails without the fix, in `test/regression/`.
- Public API changes: update `test/public-api.test.ts`.
- Performance-sensitive code: add or update a `*.bench.ts`. If a change is meant to make something faster or slower, update the matching baseline in `test/vectors/` with `UPDATE_BENCH_BASELINE=1 pnpm bench`.
- Test names describe the behaviour: `rejects a roster entry that is not 20 bytes`, not `test 3`.

## Git and releases

- Conventional Commits (`feat:`, `fix:`, `chore:` ...).
- `main` is the trunk. release-please opens release PRs from it, and publishing a GitHub release publishes the package to npm.
- Breaking changes must be called out in the commit message (`feat!:` or a `BREAKING CHANGE:` footer), because bee-js and other consumers depend on the exact API.
1 change: 1 addition & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@AGENTS.md
Loading