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
102 changes: 102 additions & 0 deletions .harness/analysis-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<!--
Harness knowledge file.
Update when the corresponding architecture or workflow changes.
Do not add speculative information.
-->

# Harness analysis report

Generated by the `fp-repo-harness` bootstrap. Records evidence, confidence, and
open questions. No application code, dependencies, or infrastructure were changed.

## Repository classification

- **Type:** Go HTTP service **and** CLI generator (single binary, multiple
subcommands). Evidence: `cmd/error-pages/main.go`, `internal/cli/root.go`
(`serve`, `build`, `healthcheck`, `version`), `internal/http/server.go`.
- **Distribution:** compiled binaries (release workflow) + distroless Docker image
(`Dockerfile`, `FROM scratch`). Evidence: `.github/workflows/release.yml`.
- **Confidence:** HIGH.

## Toolchain & commands

| Item | Value | Evidence | Confidence |
| --- | --- | --- | --- |
| Go version | 1.18 | `go.mod`, `Dockerfile` (`golang:1.18.1-alpine`), `tests.yml` | HIGH |
| Lint | `golangci-lint` v1.44, config `.golangci.yml` | `tests.yml`, `docker-compose.yml`, `Makefile` | HIGH |
| Unit tests | `go test -race ./...` | `tests.yml`, `Makefile` (`gotest`) | HIGH |
| Integration | Hurl suite `test/hurl/*.hurl` | `Makefile` (`int-test`), `tests.yml` (`poke-docker-image`) | HIGH |
| Config lint | `ajv` vs `schemas/config/1.0.schema.json` | `tests.yml` (`validate-config-file`) | HIGH |
| l10n lint | `eslint@8` in `l10n/` | `tests.yml` (`lint-l10n`), `l10n/.eslintrc.json` | HIGH |
| golangci job Go version | 1.17 (not 1.18) | `tests.yml` comment: embedded ruleguard load error on 1.18 | HIGH |

## Architecture findings

- **Shared rendering core.** `internal/tpl.TemplateRenderer.Render` is the only
render path; used by `internal/http/core/errorpage.go` and
`internal/cli/build/command.go`. Confidence: HIGH.
- **Single response funnel.** All error output flows through
`core.RespondWithErrorPage` (`internal/http/core/errorpage.go`). It hard-sets
`X-Robots-Tag: noindex`, negotiates `json`/`xml` from `Content-Type` /
`X-Format` (only those two; default HTML), and copies only `--proxy-headers`
request headers to the response. Confidence: HIGH.
- **`/{code}.html` returns HTTP 200.** `errorpage` handler passes
`fasthttp.StatusOK` regardless of `code`. `/` (`index` handler) instead uses
the real code, overridable by the `X-Code` header (validated `0 < code <= 599`,
max 3 chars). Confidence: HIGH.
- **Config relative paths.** `config.FromYamlFile` `os.Chdir`s into the config
file's directory before resolving template `path` entries, then restores.
Confidence: HIGH.
- **`envsubst` on config.** `config.FromYaml` runs `envsubst.Bytes` before YAML
parse, so `error-pages.yml` may reference environment variables. Confidence: HIGH.
- **Template output cache.** In-process map keyed by md5(props)+md5(content),
2s item lifetime, 1s cleanup tick, cleared on `Close()`. Not configurable via
flags. Confidence: HIGH.
- **Picker modes.** `first` (default), `random` (once), `i-said-random` (every
request), `random-daily`, `random-hourly`; explicit template name bypasses the
picker. Confidence: HIGH (`internal/cli/serve/command.go`, `internal/pick`).
- **Docker image bakes static pages.** `Dockerfile` runs
`error-pages ... build ./html --verbose --index` into `/opt/html` for reuse in
other images / the gh-pages demo. Confidence: HIGH.

## Schema vs runtime mismatch (NEEDS HUMAN CONFIRMATION)

`schemas/config/1.0.schema.json` documents a page-code pattern
(alphanumeric/underscore/hyphen), while `config.Validate` only rejects empty
codes and codes containing whitespace. The runtime is more permissive than the
published schema. Not necessarily a bug — flag for maintainer awareness when
changing either. Confidence: MEDIUM (noted in existing wiki page 11).

## Existing documentation reused

- `wiki/pages/01..11` — full generated wiki (ref `6d3ced4`), routed from
`.harness/context.yaml`; not duplicated.
- `README.md` — feature list, usage stories, template previews.
- `templates/readme.md`, `l10n/readme.md`, `schemas/config/readme.md`,
`test/hurl/readme.md`, `CHANGELOG.md`, `.github/workflows/*`.

## Undocumented decisions (LOW confidence — do not treat as rules)

- Why `/{code}.html` is always `200` rather than the numeric status: likely
because it mirrors serving a static file. No ADR or comment states this.
- Cache lifetime constants (2s / 1s) are unexplained magic numbers in
`internal/tpl/render.go`.
- No ADRs / `docs/architecture/` decisions directory exists in the repo.

## Suggested maintainer review

1. Confirm the schema/runtime page-code pattern divergence is intentional.
2. Confirm `/{code}.html` returning `200` is intended contract (Hurl covers the
body but this is worth an explicit note).
3. Decide whether the template cache TTLs should be documented or configurable.

## Context assessment

- Root `AGENTS.md`: ~95 lines / well under 1,500 words.
- Tier-1 always-loaded harness files: 3 (`AGENTS.md`, `service.yaml`,
`context.yaml`).
- Tier-2 routed documents: `ARCHITECTURE.md`, `docs/development.md`, plus 11
pre-existing `wiki/pages/*`.
- Nested `AGENTS.md`: 0 (uniform Go tree; `l10n/` too small to warrant one).
- Label: **MINIMAL** — the harness adds a small navigation layer over an
already-documented codebase and defers depth to the existing wiki.
45 changes: 45 additions & 0 deletions .harness/context.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Harness knowledge file. Update when architecture or workflow changes.
# Task -> documents routing. Tier 1 is always loaded; load Tier 2/3 on demand.

tier1_always_loaded:
- AGENTS.md
- .harness/service.yaml
- .harness/context.yaml

tier2_task_routed:
- when: "understand overall architecture / component boundaries"
read: [ARCHITECTURE.md, wiki/pages/01-repository-overview.md]
- when: "trace an HTTP request / add or change a route / middleware"
read: [wiki/pages/03-http-routing-and-request-flow.md, internal/http/server.go, internal/http/core/errorpage.go]
- when: "change CLI flags, env vars, or config parsing"
read: [wiki/pages/02-cli-and-configuration.md, internal/cli/serve/flags.go, internal/env/env.go, internal/config/config.go]
- when: "response format, status codes, headers, error contract"
read: [wiki/pages/04-response-formats-and-error-contracts.md, internal/http/core/formats.go, internal/http/core/headers.go, schemas/config/1.0.schema.json, test/hurl/]
- when: "template rendering, template functions/tokens, localization"
read: [wiki/pages/05-template-rendering-and-localization.md, internal/tpl/render.go, internal/tpl/properties.go, l10n/readme.md]
- when: "template selection / picker behavior"
read: [wiki/pages/06-template-selection.md, internal/pick/picker.go, internal/cli/serve/command.go]
- when: "static page generation (build command / Docker asset tree)"
read: [wiki/pages/07-static-page-generation.md, internal/cli/build/command.go, Dockerfile]
- when: "healthz / metrics / version endpoints, observability"
read: [wiki/pages/08-operational-endpoints-and-observability.md, internal/http/common/middlewares.go, internal/metrics/metrics.go]
- when: "Docker image, build args, release pipeline"
read: [wiki/pages/09-container-and-release-delivery.md, Dockerfile, .github/workflows/release.yml]
- when: "running tests, CI expectations, integration (Hurl) suite"
read: [docs/development.md, wiki/pages/10-tests-and-ci-verification.md, .github/workflows/tests.yml, Makefile]
- when: "adding a template, format, or endpoint (extension points)"
read: [wiki/pages/11-repository-map-and-extension-points.md]
- when: "local development / build / lint / run commands"
read: [docs/development.md]

tier3_specialized:
- path: wiki/
note: "Full generated wiki (11 pages + manifest.json). Authoritative deep dive; generated at ref 6d3ced4."
- path: CHANGELOG.md
note: "Release history."
- path: .harness/analysis-report.md
note: "Findings, confidence levels, and items needing human confirmation from harness bootstrap."

notes:
- "No nested AGENTS.md files: the Go tree is uniform (same toolchain, test style, lint config)."
- "l10n/ is JavaScript with its own ESLint config but is small and single-purpose; covered by wiki page 05."
84 changes: 84 additions & 0 deletions .harness/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Harness knowledge file. Update when architecture or workflow changes.
# Machine-readable summary of this repository for AI coding agents.

name: error-pages
module: github.com/tarampampam/error-pages
kind: [http-service, cli]
description: >
Generates original HTTP error pages from a YAML config and HTML templates,
served live by a fasthttp server (serve) or emitted as a static HTML tree
(build). Shipped as a compiled binary and a distroless Docker image.

language:
primary: go
go_version: "1.18" # go.mod; Docker builder golang:1.18.1-alpine
secondary:
- javascript # l10n/l10n.js (browser-side localization, ESLint)

frameworks:
- spf13/cobra # CLI
- valyala/fasthttp # HTTP server
- fasthttp/router # routing
- uber-go/zap # structured logging (JSON optional)
- prometheus/client_golang # /metrics
- gopkg.in/yaml.v3 + a8m/envsubst # config parsing

entry_points:
process: cmd/error-pages/main.go
cli_tree: internal/cli/root.go # NewCommand -> version | healthcheck | build | serve
http_routes: internal/http/server.go # Server.Register

commands:
build: "go build -trimpath -ldflags \"-s -w -X github.com/tarampampam/error-pages/internal/version.version=$VERSION\" -o ./error-pages ./cmd/error-pages/"
test: "go test -race ./..."
lint: "golangci-lint run" # CI pins golangci-lint v1.44, setup-go 1.17 for lint job
int_test: "make int-test" # Hurl suite against docker-compose 'web' service
config_validate: "ajv validate --all-errors -s ./schemas/config/1.0.schema.json -d ./error-pages.y*ml"
l10n_lint: "cd l10n && eslint ./*.js"
docker_image: "make image" # docker build -f Dockerfile -t error-pages:local .
run_local: "make up" # docker-compose up web -> http://127.0.0.1:8080

runtime:
http_port_default: 8080
listen_default: "0.0.0.0"
endpoints:
- "GET / -> index / default error page (honors X-Code header)"
- "GET /{code}.html -> error page for code, always HTTP 200"
- "GET /version -> build version"
- "ANY /healthz -> liveness (also /health/live, deprecated)"
- "GET /metrics -> Prometheus metrics"
negotiation_headers: ["Content-Type", "X-Format"] # json | xml, else HTML
supported_formats: [html, json, xml]
ingress_detail_headers: # surfaced when --show-details / SHOW_DETAILS=true
[X-Original-URI, X-Namespace, X-Ingress-Name, X-Service-Name, X-Service-Port, X-Request-ID, X-Forwarded-For, Host]

config:
file: error-pages.yml
schema: schemas/config/1.0.schema.json
env_expansion: true # envsubst over the YAML
env_vars: # names only; CLI flag > env var > default
- LISTEN_ADDR
- LISTEN_PORT
- TEMPLATE_NAME
- CONFIG_FILE
- DEFAULT_ERROR_PAGE
- DEFAULT_HTTP_CODE
- SHOW_DETAILS
- PROXY_HTTP_HEADERS
- DISABLE_L10N

templates:
dir: templates/
selection_modes: [first, random, i-said-random, random-daily, random-hourly, "<explicit name>"]

ci:
workflows:
- .github/workflows/tests.yml # gitleaks, golangci-lint, ajv, eslint, go test -race, build matrix, generator check, docker build+trivy+hurl
- .github/workflows/release.yml # cross-build binaries, multi-arch docker push, gh-pages demo
- .github/workflows/documentation.yml
coverage: codecov (.codecov.yml)

persistence: none # stateless; no database, cache is in-process only
messaging: none

confidence: HIGH # derived from go.mod, Makefile, CI workflows, Dockerfile, source
97 changes: 97 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<!--
Harness knowledge file.
Update when the corresponding architecture or workflow changes.
Do not add speculative information.
-->

# AGENTS.md

Guidance for AI coding agents working in this repository. Human-written notes in
this file take precedence; extend, do not silently remove them.

## What this is

`error-pages` (`github.com/tarampampam/error-pages`) is a Go 1.18 project that
turns a YAML config plus HTML templates into either:

- a fast HTTP server (`serve`) that renders error pages on demand, or
- a static HTML asset tree (`build`) written to disk.

Both paths share the renderer in `internal/tpl`. The Docker image runs `build` at
image-build time and ships the static output alongside the server binary.

## Where things live

| Area | Path | Notes |
| --- | --- | --- |
| Entry point | `cmd/error-pages/main.go` | thin; delegates to `internal/cli` |
| CLI commands | `internal/cli/{root,serve,build,healthcheck,version}` | Cobra tree |
| Config load | `internal/config/config.go` | YAML + envsubst → `Config` |
| Flags/env | `internal/cli/serve/flags.go`, `internal/env/env.go` | precedence: CLI flag > env var > default |
| HTTP wiring | `internal/http/server.go` (`Server.Register`) | fasthttp + `fasthttp/router` |
| Response core | `internal/http/core/errorpage.go`, `formats.go`, `headers.go` | negotiation, status, proxied headers |
| Handlers | `internal/http/handlers/*` | one package per route |
| Rendering | `internal/tpl/render.go`, `properties.go` | Go `text/template` + 2s in-memory cache |
| Template selection | `internal/pick/`, `internal/cli/serve/command.go` | first / random / random-per-request / daily / hourly |
| Assets | `templates/*.html`, `error-pages.yml`, `l10n/l10n.js` | page set + browser-side localization |
| Public contract | `schemas/config/1.0.schema.json`, `test/hurl/*.hurl` | keep in sync with runtime |
| Delivery | `Dockerfile`, `.github/workflows/`, `Makefile` | |

## Deeper context

A full generated wiki lives in `wiki/pages/` (11 pages). Route by task:

- Architecture / request trace → `wiki/pages/03-http-routing-and-request-flow.md`, `ARCHITECTURE.md`
- CLI & config inputs → `wiki/pages/02-cli-and-configuration.md`
- Response formats & error contract → `wiki/pages/04-response-formats-and-error-contracts.md`
- Rendering & localization → `wiki/pages/05-template-rendering-and-localization.md`
- Template selection → `wiki/pages/06-template-selection.md`
- Static generation → `wiki/pages/07-static-page-generation.md`
- Ops endpoints (`/healthz`, `/metrics`, `/version`) → `wiki/pages/08-operational-endpoints-and-observability.md`
- Container & release → `wiki/pages/09-container-and-release-delivery.md`
- Tests & CI → `wiki/pages/10-tests-and-ci-verification.md`
- Extension points → `wiki/pages/11-repository-map-and-extension-points.md`

Commands and local dev: `docs/development.md`. Machine-readable summary:
`.harness/service.yaml`, routing table: `.harness/context.yaml`.

## Commands (host toolchain)

```
go build -trimpath -o ./error-pages ./cmd/error-pages/ # build
go test -race ./... # unit tests
golangci-lint run # lint (CI pins v1.44)
```

`make` targets (`build`, `fmt`, `lint`, `gotest`, `int-test`, `test`, `up`) run
through Docker Compose; use them when you don't have the toolchain locally. The
Hurl integration suite (`make int-test`) needs the compose `web` service.

## Invariants (verified in code)

- `internal/tpl` is the only renderer for both `serve` and `build`; keep template
tokens/functions changes there so both paths stay consistent.
- All HTTP error responses go through `core.RespondWithErrorPage`. It sets
`X-Robots-Tag: noindex`, negotiates format from `Content-Type` / `X-Format`
(only `json` and `xml` are special-cased; everything else is HTML), and proxies
only the headers named in `--proxy-headers`.
- `/` honors the `X-Code` request header (1–599) to override the default page and
status; `/{code}.html` always responds `200` with the page body for that code.
- Config template `path` values are resolved relative to the config file's
directory (the loader `os.Chdir`s there and back). `envsubst` runs on the YAML,
so `$VAR` in `error-pages.yml` is expanded from the environment.
- Page codes must not contain whitespace (`config.Validate`); the JSON schema
pattern is stricter than the Go check — update both together.
- `TemplateRenderer` starts a cleanup goroutine; callers must `Close()` it
(server does this in `Server.Stop`).
- Docker runtime image is `scratch`, runs as UID `10001`, healthcheck shells
`/bin/error-pages healthcheck`.

## Guardrails

- Do not change response contracts, status codes, header names, or the config
schema without updating `schemas/` and `test/hurl/` together.
- CI ignores Markdown-only changes for build/test jobs but still lints config and
l10n JS. `error-pages.yml` must validate against `schemas/config/1.0.schema.json`
(`ajv`), and `l10n/*.js` must pass ESLint.
- Keep `go.mod` at Go 1.18 and avoid adding dependencies unless necessary.
Loading
Loading