diff --git a/CLAUDE.md b/CLAUDE.md index e5fa7cf..c0a51bc 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -2,7 +2,7 @@ ## Project -Self-hosted error and log monitoring server that accepts data from the official Sentry SDKs via DSN (envelope protocol). Go, an embedded key-value store (goleveldb, pure Go, no CGO), shipped mainly as a Docker image (`FROM scratch`, size budget 50 MB, currently ~20 MB, ~6 MB to pull). MIT, public DevForth OSS project. Tracklify task: `DevFo-OSS/17`. +Self-hosted error and log monitoring server that accepts data from the official Sentry SDKs via DSN (envelope protocol). Go, an embedded key-value store (goleveldb, pure Go, no CGO), shipped mainly as a Docker image (`FROM scratch`, size budget 50 MB, currently ~25 MB, ~6 MB to pull). MIT, public DevForth OSS project. Tracklify task: `DevFo-OSS/17`. Scope: errors and logs only. Tracing spans, session replay and uptime items are accepted and dropped, not stored; `client_report` items are only counted (dropped data per project). @@ -58,7 +58,7 @@ Where to look first: - `server/`: the HTTP shell: the routes of the whole server and `/health` (`server.go`), the IP allowlist and token (`access.go`), security headers, the CSP builder and the Origin check (`headers.go`), JSON answers (`json.go`), the control socket of backups (`control.go`). - `ingest/`: receiving from Sentry SDKs (handler, pipeline, `StoreSink`, `compact.go`, `scrub.go`) with `grouping`. - `sentry/`: the SDK wire format, read by ingest, notify, services (the issue page) and the agent prompt. -- `storage/`: `store` (the API of the storage: the records, their rules and the `Store` interface, every operation on the records; everything else uses only it, except the inbox (`ingest.Pipeline`) and the periodic jobs and backups (`internal/app`, the database lane built by `database.Store.Lane`), which take `*database.Store`), `database` (the one implementation of that API: every record, the ingest inbox included, each collection declared in `database/schema.go`; the engine is picked in `database/engine_leveldb.go`, the only file naming it), `kv` (the layer under it, unaware of the application: keys built from typed parts, collections with declared indexes, batches with counters and sequence numbers, queries and cursors (`Head` decodes only a record's head when the collection has `DecodeHead`, as events, the inbox and the outbox do; `Raw` filters on the stored bytes before decoding; `Keyed` on the record's key before reading it, through an index too; `Count` without `Where`, `Raw` or `Keyed` counts keys; `WithContext` ends a walk with its context; `Cursor.Step` reads one key, a skipped one too, so merged walks advance together), `Check` (`Schema.Check` runs it on every collection, as `Store.Verify` does), the `Engine` interface (`SizeOf`: the files' bytes of a key range, deleted keys included until compacted); `Definition.Tally`: record counts per key (how many records have a status in a project, say), written in the same batch under `tally/`, apart from the byte counters (`DB.Tally`, `DB.Tallies`, `DB.CheckTallies`; beside a Backfill only of indexes, not of the Group; `Definition.Recount` names a Tally added to records stored without one: `DB.Recount` (called by `database.open` after the format check, so a refused database is never written) counts them in once, in batches of 2000 with their position in `meta/recount//`, before anything writes (`recount.go`), a new database marked done); `Write` neither deletes nor puts again the index keys a rewrite keeps, and they leave nothing stale; `Definition.Group`: a second byte counter per record (the project), written in the same batch under `group/` and kept apart from `Counter`'s (not in `Batch.Counted`, `DB.Written` or the cleanup's total; `DB.Groups`); `Collection.Stale`: per counted collection, the bytes its deletes and rewrites left in the files, counted in the same batch under `stale/` (all ever added under `staled/`: `StaleMark`, a compaction's mark at its start), taken off by a compaction's `TakeStale(mark)` (`addStale` changes `stale` and `staled` together, so a rewrite's refund lowers both) (only what no other compaction took off, so overlapping ones never take bytes deleted later) and never by the engine's own compactions; `Definition.Backfills` (`[]Backfill{Name, Indexes, Group}`): what a collection gained after records were stored, each in turn, counted once in steps (`Collection.BackfillStep`, each its own position in `meta/backfill//` in the same batch, so it survives a restart; an index is counted for a record once its own Backfill reached it, `Collection.Indexed`; a Backfill of the Group must be the collection's only one; a record not reached yet is counted as before, so writes and deletes meanwhile stay exact; a new database is marked done when opened, `DB.Fresh`; a backfilled index cannot be unique); nothing else builds a key), `kv/leveldb` (the goleveldb adapter, the only code importing goleveldb, checked by `kv/adapters_test.go`; goleveldb is pure Go, so the binary stays static and the image `FROM scratch`, and its files are LevelDB's format, so a LevelDB C++ adapter could read them; it is unmaintained since 2022, a risk accepted for that; another store is another adapter package plus `database/engine_.go` under `//go:build `, with `//go:build !` on `engine_leveldb.go`), `kv/enginetest` (the test every adapter must pass), `projects` (registry). Tests open a store with `storetest.Open(t)` (a `database` in a temporary directory) and seed records with `database.Store.SaveEvent`/`SaveLogs` (for tests only, not in `store.Store`: ingest writes through `Write`) and projects with fixed ids and keys (the fixtures' DSNs, `fixtures.Projects()`) with `storetest.AddProjects` (`database.Store.AddProjects`, for tests only), before the registry opens; the measurements open a copy of a 60-day seed with `storetest.OpenSeed`. Ingest goes through `ingest.Pipeline` (inbox, processors, one writer that notifies after each batch). Only `main` and tests open the database. +- `storage/`: `store` (the API of the storage: the records, their rules and the `Store` interface, every operation on the records; everything else uses only it, except the inbox (`ingest.Pipeline`) and the periodic jobs and backups (`internal/app`, the database lane built by `database.Store.Lane`), which take `*database.Store`), `database` (the one implementation of that API: every record, the ingest inbox included (in memory, what does not fit in its collection on disk, `inbox.go`), each collection declared in `database/schema.go`; the engine is picked in `database/engine_leveldb.go`, the only file naming it), `kv` (the layer under it, unaware of the application: keys built from typed parts, collections with declared indexes, batches with counters and sequence numbers, queries and cursors (`Head` decodes only a record's head when the collection has `DecodeHead`, as events, logs, the inbox and the outbox do; `Raw` filters on the stored bytes before decoding; `Keyed` on the record's key before reading it, through an index too; `Count` without `Where`, `Raw` or `Keyed` counts keys; `WithContext` ends a walk with its context; `Cursor.Step` reads one key, a skipped one too, so merged walks advance together), `Check` (`Schema.Check` runs it on every collection, as `Store.Verify` does), the `Engine` interface (`SizeOf`: the files' bytes of a key range, deleted keys included until compacted); `Definition.Tally`: record counts per key (how many records have a status in a project, say), written in the same batch under `tally/`, apart from the byte counters (`DB.Tally`, `DB.Tallies`, `DB.CheckTallies`; beside a Backfill only of indexes, not of the Group; `Definition.Recount` names a Tally added to records stored without one: `DB.Recount` (called by `database.open` after the format check, so a refused database is never written) counts them in once, in batches of 2000 with their position in `meta/recount//`, before anything writes (`recount.go`), a new database marked done); `Write` neither deletes nor puts again the index keys a rewrite keeps, and they leave nothing stale, nor reads a unique key the old record holds already (an issue's fingerprint at every rewrite, `TestRewriteReadsNoUniqueKeyItHolds`); `Definition.Group`: a second byte counter per record (the project), written in the same batch under `group/` and kept apart from `Counter`'s (not in `Batch.Counted`, `DB.Written` or the cleanup's total; `DB.Groups`); `Collection.Stale`: per counted collection, the bytes its deletes and rewrites left in the files, counted in the same batch under `stale/` (all ever added under `staled/`: `StaleMark`, a compaction's mark at its start), taken off by a compaction's `TakeStale(mark)` (`addStale` changes `stale` and `staled` together, so a rewrite's refund lowers both) (only what no other compaction took off, so overlapping ones never take bytes deleted later) and never by the engine's own compactions; `Definition.Backfills` (`[]Backfill{Name, Indexes, Group}`): what a collection gained after records were stored, each in turn, counted once in steps (`Collection.BackfillStep`, each its own position in `meta/backfill//` in the same batch, so it survives a restart; an index is counted for a record once its own Backfill reached it, `Collection.Indexed`; a Backfill of the Group must be the collection's only one; a record not reached yet is counted as before, so writes and deletes meanwhile stay exact; a new database is marked done when opened, `DB.Fresh`; a backfilled index cannot be unique); nothing else builds a key), `kv/leveldb` (the goleveldb adapter, the only code importing goleveldb, checked by `kv/adapters_test.go`; goleveldb is pure Go, so the binary stays static and the image `FROM scratch`, and its files are LevelDB's format, so a LevelDB C++ adapter could read them; it is unmaintained since 2022, a risk accepted for that; another store is another adapter package plus `database/engine_.go` under `//go:build `, with `//go:build !` on `engine_leveldb.go`), `kv/enginetest` (the test every adapter must pass), `projects` (registry). Tests open a store with `storetest.Open(t)` (a `database` in a temporary directory) and seed records with `database.Store.SaveEvent`/`SaveLogs` (for tests only, not in `store.Store`: ingest writes through `Write`) and projects with fixed ids and keys (the fixtures' DSNs, `fixtures.Projects()`) with `storetest.AddProjects` (`database.Store.AddProjects`, for tests only), before the registry opens; the measurements open a copy of a 60-day seed with `storetest.OpenSeed`. Ingest goes through `ingest.Pipeline` (inbox, processors, one writer that notifies after each batch). Only `main` and tests open the database. - `notify/`: notifications. - `api/`: the request catalog and its three transports in one package: the endpoints (`catalog.go`, `endpoint.go`, one file per area), `rest.go`, `mcp.go` with `instructions.md` and `skills/`, `ui.go` (the web app's, with its login session), `plain.go`/`plain_agents.go` (the web app's plain requests). - `oauth/`: how agents sign in to MCP. `agentprompt/`: the prompt for coding agents, used by notify, services and the API. @@ -66,8 +66,8 @@ Where to look first: - `session/`: the login of the web app (session cookies, the login limit, form tokens). - `fixtures/`: test data for tests only (`WebApp`: a stand-in build of the web app for full-server tests). -- `cmd/exceptic/main.go`: loads config, sets a soft memory limit of 200 MiB unless `GOMEMLIMIT` is set (`config.LimitMemory`, `config.DefaultMemoryLimit`), opens the store and runs the server of `internal/app`: `app.New` builds it from the settings, with its ingest pipeline and notifier (the tests call it too, and `App.Start` runs the pipeline of a server they serve themselves; `App.WaitStored` waits until what it accepted is stored), `app.Run` starts its pipeline and periodic jobs (`app/jobs.go`, `lanes`, logged at start as "background jobs"; a lane runs its jobs one at a time, lanes side by side; a pass that panics is logged with its stack as a failed pass ("background job failed", `logging.PanicError`) and the lane goes on (a pass that does not stop at its time limit is left running beside the lane, at most one per job; only a compaction can (the inbox compaction, the cleanup compaction, and those the size check and the project purge run), since goleveldb's `CompactRange` takes no ctx; a long pass may return `background.ErrYielded` between its steps once `background.Waiting` (another job of its lane woken or due): the lane runs those, a woken one first, then the pass again), Wakes add at most one waiting pass per job, stopped before the notifier and the store close (a pass that does not stop is waited for one second); `TestJobsStartApart` checks no two start in the same minute over a day), binds the listener before logging "server started", serves it with `config.MaxConnections` (1000) at once (`server.LimitListener`: more wait in the accept queue, idle keep-alive connections are closed when it is full) and `config.MaxHeaderBytes` (64 KiB, 431 above), graceful shutdown on SIGTERM. The `healthcheck` subcommand is what Docker's HEALTHCHECK calls (no curl in a scratch image); `reset-password [email]` (the only admin by default) prints a temporary password, the way to recover a forgotten one (with the server stopped: `docker compose run --rm exceptic reset-password`); `backup [absolute dir]` (default `DATA_DIR/backups/