Initial Commit

This commit is contained in:
2026-05-27 23:29:53 +01:00
commit fbc70f96e6
10000 changed files with 1102152 additions and 0 deletions

View File

@@ -0,0 +1,139 @@
# OpenSpec Change: enterprise-server-health-check
- **Change ID**: enterprise-server-health-check
- **Package**: `@suaveplan/health-check`
- **Category**: server/infra
- **Target path**: `server/infra/health-check`
- **Status**: PROPOSED
- **Date**: 2026-04-24
- **Reference spec**: `/mnt/s/web/suaveplan/enterprise/health-check.md`
---
## Summary
Deliver `@suaveplan/health-check` on the genesis branch from scratch at enterprise quality. The package provides a framework-agnostic health check system that exposes `/health`, `/ready`, and `/live` HTTP endpoints following Kubernetes probe conventions. A `HealthRegistry` holds named `HealthCheck` functions; an `HealthServer` runs a lightweight HTTP server returning structured JSON responses. Individual checks can have configurable timeouts, retry counts, and cache TTLs. Genesis adds Zod schemas, `@suaveplan/error` codes, OTel counters per check result, structured logging, and 100% test coverage via both unit and integration tests.
---
## Current-state audit (genesis)
Does not exist on genesis — fresh build.
---
## Available workspace deps on genesis
| Package | Role |
|---|---|
| `@suaveplan/error` (`workspace:^`) | Typed `HEALTH_CHECK.*` error codes |
| `@suaveplan/types` (`workspace:^`) | `LoggerLike`, `Clock` |
| `@suaveplan/logger` (`workspace:^`) | Structured logs |
| `zod` (`catalog:forms`) | Options validation |
| `@opentelemetry/api` (`catalog:observability`) | Counters + histograms |
| `tslib` (`catalog:build`) | Runtime helpers |
---
## Public API surface
```ts
class HealthRegistry {
register(name: string, check: HealthCheckFn, options?: CheckOptions): void
unregister(name: string): void
runAll(): Promise<HealthReport>
runCheck(name: string): Promise<CheckResult>
}
class HealthServer {
constructor(registry: HealthRegistry, options?: HealthServerOptions)
start(): Promise<void>
stop(): Promise<void>
address(): { host: string; port: number } | undefined
}
function createHealthRegistry(): HealthRegistry
function createHealthServer(registry: HealthRegistry, options?: HealthServerOptions): HealthServer
type HealthCheckFn = (signal: AbortSignal) => Promise<void> // throws = unhealthy
interface CheckOptions {
timeout?: number // ms, default: 5000
critical?: boolean // if true, failure marks overall status 'unhealthy'
cacheTtlMs?: number // reuse last result for this duration
}
interface CheckResult {
name: string
status: 'healthy' | 'unhealthy' | 'degraded'
duration: number // ms
error?: string
cachedAt?: number
}
interface HealthReport {
status: 'healthy' | 'unhealthy' | 'degraded'
checks: CheckResult[]
timestamp: number
}
interface HealthServerOptions {
port?: number // default: 3001
host?: string // default: '0.0.0.0'
path?: {
health?: string // default: '/health'
ready?: string // default: '/ready'
live?: string // default: '/live'
}
logger?: LoggerLike
}
type HealthCheckErrorCode =
| 'HEALTH_CHECK_TIMEOUT'
| 'HEALTH_CHECK_NOT_FOUND'
| 'HEALTH_SERVER_START_FAILED'
function registerHealthCheckErrorCodes(): void
```
---
## File layout
```text
server/infra/health-check/
package.json
tsconfig.json
vitest.config.ts
src/
index.ts
error-codes.ts
constants/index.ts
types/
health-check.types.ts
health-check.types.md
registry/
health-registry/
health-registry.ts
health-registry.md
health-registry.types.ts
health-registry.test.ts
index.ts
server/
health-server/
health-server.ts
health-server.md
health-server.types.ts
health-server.test.ts
index.ts
tests/
integration.test.ts
```
---
## Impact
Downstream: all server packages use `@suaveplan/health-check` to register their own checks. This is a core infrastructure primitive.
**Prerequisites on Verdaccio**: `@suaveplan/error`, `@suaveplan/types`, `@suaveplan/logger`.

View File

@@ -0,0 +1,69 @@
# Tasks: enterprise-server-health-check
**Change ID**: enterprise-server-health-check
**Package**: `@suaveplan/health-check`
**Target path**: `server/infra/health-check`
**Prerequisites on Verdaccio**: `@suaveplan/error`, `@suaveplan/types`, `@suaveplan/logger`
---
## 0. Pre-flight (non-negotiable)
- [ ] 0.1 Confirm clean working tree (`git status` reports nothing) and no in-flight worktrees with uncommitted work (`git worktree list` then `git status` in each)
- [ ] 0.2 If any worktrees exist under `.claude/worktrees/`, account for each one: still-active agent, work-pending-merge, or stale-and-prune. Stale worktrees MUST be removed (`git worktree remove`) before opening a new one — code that has been lost twice on main is the failure mode this guards against.
- [ ] 0.3 Confirm the package set in `MANIFEST.md` matches `find packages -maxdepth 3 -name package.json` count. Drift here means a prior change archived without regenerating the manifest.
## 1. Scaffold
- [ ] Create directory tree per `proposal.md`.
- [ ] `package.json` at `0.1.0`, `"sideEffects": false`, exports `"."` + `registry` + `server` subpaths.
- [ ] Deps: `@suaveplan/error workspace:^`, `@suaveplan/types workspace:^`, `@suaveplan/logger workspace:^`, `zod catalog:forms`, `@opentelemetry/api catalog:observability`, `tslib catalog:build`.
- [ ] DevDeps: `vitest catalog:testing`, `@vitest/coverage-istanbul catalog:testing`, `@suaveplan/testing workspace:^`.
- [ ] `tsconfig.json` extending `library-node.json`; `vitest.config.ts` with `createTestConfig()`.
- [ ] Scripts: `lint`, `typecheck`, `test`, `test:coverage`, `build`, `docs:check`.
## 2. Types and errors
- [ ] Define all interfaces and Zod schemas in `src/types/health-check.types.ts`.
- [ ] Write `health-check.types.md` (≥200 words, both examples, API reference).
- [ ] `src/error-codes.ts`: three codes; `registerHealthCheckErrorCodes()`; eager call.
## 3. Health registry
- [ ] Implement `HealthRegistry` in `src/registry/health-registry/health-registry.ts`:
- `register(name, check, options)`: store in internal Map; throw if name already registered.
- `unregister(name)`: remove; throw `HEALTH_CHECK_NOT_FOUND` if absent.
- `runAll()`: run all registered checks concurrently (via `Promise.allSettled`); each with `AbortSignal` tied to `timeout`; collect `CheckResult`; compute overall `status` (any critical failure → unhealthy; any non-critical failure → degraded; all pass → healthy). Return `HealthReport`.
- `runCheck(name)`: run single check with timeout; update cache; return `CheckResult`.
- Cache: store last `CheckResult` per check; on `runCheck`/`runAll` return cached if within `cacheTtlMs`.
- OTel counter `health_check.runs_total` with `check_name` and `status`; histogram `health_check.duration_ms`.
- [ ] Write `health-registry.types.ts`.
- [ ] Write `health-registry.test.ts` (≤500 lines): all checks healthy; one critical fails; one non-critical fails (degraded); timeout triggers; cache TTL respected with FakeClock; unregister removes.
- [ ] Write `health-registry.md` (≥200 words, simple + advanced, API reference).
## 4. Health server
- [ ] Implement `HealthServer` in `src/server/health-server/health-server.ts`:
- Use `Bun.serve` to create a minimal HTTP server.
- `GET /health`: run all checks; return JSON `HealthReport`; HTTP 200 if healthy/degraded, 503 if unhealthy.
- `GET /ready`: same logic but only return 503 if any critical check fails.
- `GET /live`: always return 200 `{ status: 'alive' }` (liveness = process is running).
- `start()` / `stop()` lifecycle.
- OTel counter `health_server.requests_total` with `endpoint` and `status_code`.
- [ ] Write `health-server.types.ts`.
- [ ] Write `health-server.test.ts` (≤500 lines): HTTP 200 on all healthy; HTTP 503 on critical failure; custom paths; /live always 200; stop closes server; `{ timeout: 10000 }`.
- [ ] Write `health-server.md` (≥200 words, simple + advanced, API reference).
## 5. Integration tests
- [ ] `tests/integration.test.ts`: real server on random port; fetch `/health`, `/ready`, `/live`; register a DB check that simulates failure; verify 503 on `/health`; `{ timeout: 15000 }`.
## 6. Docs + verify + publish
- [ ] Root `README.md`: Kubernetes probe configuration snippet, quick-start, API table.
- [ ] `bunx turbo lint --filter=@suaveplan/health-check` — zero errors.
- [ ] `bunx turbo typecheck --filter=@suaveplan/health-check` — zero errors.
- [ ] `bunx turbo test --filter=@suaveplan/health-check` — all pass.
- [ ] `bunx turbo test:coverage --filter=@suaveplan/health-check` — 100%.
- [ ] `bun scripts/publish.ts --package @suaveplan/health-check`.
- [ ] Update `CLAUDE.md`; archive to `openspec/archive/2026-04-24-enterprise-server-health-check/`.