diff --git a/.agents/HANDOFF.md b/.agents/HANDOFF.md index f7c1f96..ce058ac 100644 --- a/.agents/HANDOFF.md +++ b/.agents/HANDOFF.md @@ -1,5 +1,13 @@ # HANDOFF +## 2026-11-21 - Env template and ignore rules (Claude) + +- Branch `chore/env-contract-and-ignore-rules-2026-11-21`, cut from the public `main` (`c3541d3`) in a scratch clone. Local only, NOT pushed: Gaffer has not approved a push. +- Added `.env.example` (the four names the site reads, empty values) and `.gitignore` rules for every env file, local databases, dumps, backups and key files; `.env.example` stays trackable. +- New `app/env-contract.test.mjs` (4 tests, RED first) pins both: a variable read by the site but missing from `.env.example` fails, a filled key or token fails, an unignored secret or data path fails. +- Verified: install, typecheck, lint, test 63/63, build, deploy:dry-run all exit 0 (Linux, Node 24.22.3, pnpm 11.21.0). E2E and live Vera NOT RUN. +- Open for Gaffer: the same change must land in the monorepo capsule, or the next publish from the capsule drops it; `.agents/` and `.review-2026-11-12/` are public (keep or untrack); a pre-commit secret scan was not added (needs approval). + ## 2026-11-20 - Published to gaffercode/Sentraverse, MIT (Claude) - Gaffer chose MIT for the public repo `gaffercode/Sentraverse` (its web-made LICENSE); capsule `LICENSE`, `package.json`, README and manifest follow (`bc978726`). diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..f63af5f --- /dev/null +++ b/.env.example @@ -0,0 +1,15 @@ +# Sentraverse Neural: environment template. +# Copy this file to `.env.local` (git-ignored) and fill in only what you need. +# Never commit real values. Every variable is server-only and optional: the site builds and +# runs without any of them. + +# Vera chat (Dify). Without the key, POST /api/vera/chat answers HTTP 500 +# "Vera is not available right now." +DIFY_API_KEY= +# Optional override of the Dify endpoint. Default when empty: https://api.dify.ai/v1 +DIFY_API_URL= + +# Vera message limit (Upstash Redis REST). Set both or neither: without the pair the limit is +# counted in memory per server instance. +KV_REST_API_URL= +KV_REST_API_TOKEN= diff --git a/.gitignore b/.gitignore index 65db46f..35341c0 100644 --- a/.gitignore +++ b/.gitignore @@ -27,9 +27,35 @@ yarn-debug.log* yarn-error.log* pnpm-debug.log* -# Local env +# Local env: every env file stays on the machine; only the example is tracked +.env +.env.* +!.env.example .env*.local +# Local databases, dumps and backups: real data never goes into Git +*.sqlite +*.sqlite3 +*.db +*.dump +*.pgdump +*.bak +*.backup +*.sql.gz +*dump*.sql +*backup*.sql +backups/ +dumps/ + +# Keys and credential files +*.key +*.p12 +*.pfx +id_rsa* +id_ed25519* +service-account*.json +credentials*.json + # Vercel .vercel diff --git a/README.md b/README.md index 7a78647..6ba081c 100644 --- a/README.md +++ b/README.md @@ -373,7 +373,7 @@ node scripts/pnpm.mjs run start ### Configuration -Store server-only values in git-ignored `.env.local` or the host's environment configuration. **Never commit credentials.** +Copy `.env.example` to a git-ignored `.env.local` and fill in what you need, or set the values in the host's environment configuration. **Never commit credentials.** | Variable | Required | Role | Boundary | |---|---|---|---| @@ -695,7 +695,7 @@ The following results distinguish **past recorded tests** from **checks performe | Install | **PASS — 20 Nov 2026 record** | `node scripts/pnpm.mjs install --frozen-lockfile` | Historical evidence in `docs/verification.md`. | | Typecheck | **PASS — 20 Nov 2026 record** | `node scripts/pnpm.mjs run typecheck` | Not rerun for README revision. | | Lint | **PASS — 20 Nov 2026 record** | `node scripts/pnpm.mjs run lint` | Not rerun for README revision. | -| Unit tests | **59/59 — 20 Nov 2026 record** | `node scripts/pnpm.mjs run test` | Does not cover full Vera widget / route. | +| Unit tests | **63/63 — 21 Nov 2026 record** | `node scripts/pnpm.mjs run test` | Does not cover full Vera widget / route. | | Build | **PASS — 20 Nov 2026 record** | `node scripts/pnpm.mjs run build` | Not rerun for README revision. | | Deployment dry-run | **PASS WITH LIMITATION — recorded** | `node scripts/pnpm.mjs run deploy:dry-run` | Secret-name check currently uses an empty list. | | Playwright E2E | **24/24 — 9 Oct 2026 record** | `node scripts/pnpm.mjs run test:e2e` | Predates Vera; not rerun on this revision. | diff --git a/app/env-contract.test.mjs b/app/env-contract.test.mjs new file mode 100644 index 0000000..4e44e2f --- /dev/null +++ b/app/env-contract.test.mjs @@ -0,0 +1,87 @@ +import assert from 'node:assert/strict'; +import { spawnSync } from 'node:child_process'; +import { readdirSync, readFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { test } from 'node:test'; +import { fileURLToPath } from 'node:url'; + +// Repository contract: every environment variable the site reads is listed in `.env.example` +// with a placeholder, and local secrets or data files can never be committed by accident. + +const root = dirname(dirname(fileURLToPath(import.meta.url))); +const RUNTIME_DIRS = ['app', 'components', 'src']; +const SOURCE_FILE = /\.(?:ts|tsx|mjs|js|jsx)$/; +const TEST_FILE = /\.(?:test|spec)\./; +// Set by Node, Next.js or the host, never by a developer's env file. +const PLATFORM_VARIABLES = new Set(['NODE_ENV', 'CI', 'PORT', 'VERCEL', 'VERCEL_ENV', 'VERCEL_URL', 'NEXT_RUNTIME']); + +function sourceFiles(dir) { + const found = []; + for (const entry of readdirSync(dir, { withFileTypes: true })) { + const path = join(dir, entry.name); + if (entry.isDirectory()) found.push(...sourceFiles(path)); + else if (SOURCE_FILE.test(entry.name) && !TEST_FILE.test(entry.name)) found.push(path); + } + return found; +} + +function variablesReadByTheSite() { + const names = new Set(); + for (const dir of RUNTIME_DIRS) { + for (const file of sourceFiles(join(root, dir))) { + for (const [, name] of readFileSync(file, 'utf8').matchAll(/process\.env\.([A-Z][A-Z0-9_]*)/g)) names.add(name); + } + } + return [...names].filter((name) => !PLATFORM_VARIABLES.has(name)).sort(); +} + +function envExample() { + const entries = new Map(); + for (const line of readFileSync(join(root, '.env.example'), 'utf8').split(/\r?\n/)) { + const match = /^([A-Z][A-Z0-9_]*)=(.*)$/.exec(line); + if (match) entries.set(match[1], match[2].trim()); + } + return entries; +} + +function isIgnored(path) { + return spawnSync('git', ['check-ignore', '--quiet', '--no-index', path], { cwd: root }).status === 0; +} + +const insideGit = spawnSync('git', ['rev-parse', '--is-inside-work-tree'], { cwd: root }).status === 0; + +test('.env.example lists every environment variable the site reads', () => { + const documented = envExample(); + const missing = variablesReadByTheSite().filter((name) => !documented.has(name)); + assert.deepEqual(missing, []); +}); + +test('.env.example holds no value for keys, tokens, secrets or passwords', () => { + const filled = [...envExample()].filter(([name, value]) => /KEY|TOKEN|SECRET|PASSWORD/.test(name) && value !== ''); + assert.deepEqual(filled.map(([name]) => name), []); +}); + +test('.gitignore keeps local env files, databases, dumps and keys out of Git', { skip: !insideGit && 'not a Git checkout' }, () => { + const mustBeIgnored = [ + '.env', + '.env.local', + '.env.production', + 'apps/web/.env', + 'data.sqlite', + 'local.sqlite3', + 'dev.db', + 'prod.dump', + 'db.bak', + 'backup.sql.gz', + 'backups/2026-11-21.sql', + 'server.pem', + 'private.key', + 'id_rsa', + 'service-account.json', + ]; + assert.deepEqual(mustBeIgnored.filter((path) => !isIgnored(path)), []); +}); + +test('.gitignore still lets .env.example be committed', { skip: !insideGit && 'not a Git checkout' }, () => { + assert.equal(isIgnored('.env.example'), false); +}); diff --git a/docs/data.md b/docs/data.md index 8e8459c..fbf522b 100644 --- a/docs/data.md +++ b/docs/data.md @@ -17,7 +17,8 @@ truncated SHA-256 hash reaches Upstash (`limit.test.mjs` asserts it). ## Environment variables -Names only; values live in Vercel project settings or a local `.env.local` (git-ignored). +Names only; values live in Vercel project settings or a local `.env.local` (git-ignored). The +tracked `.env.example` lists the same names with empty values. | Name | Used by | Required | | --- | --- | --- | diff --git a/docs/development.md b/docs/development.md index 1326d75..9524e7f 100644 --- a/docs/development.md +++ b/docs/development.md @@ -26,7 +26,8 @@ If 4346 is held by an earlier `next start`, stop it or verify on another port ## Configuration The journey needs no configuration. Vera needs `DIFY_API_KEY` (and optionally `DIFY_API_URL`, -`KV_REST_API_URL`, `KV_REST_API_TOKEN`) in a git-ignored `.env.local`; see [`data.md`](data.md). +`KV_REST_API_URL`, `KV_REST_API_TOKEN`) in a git-ignored `.env.local`, copied from the tracked +[`.env.example`](../.env.example); see [`data.md`](data.md). Never commit values. Without a key the page works and Vera answers "not available". ## Workflow diff --git a/docs/testing.md b/docs/testing.md index e451cea..7e19ce4 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -29,6 +29,7 @@ with no runtime relative imports. | `morphology.test.mjs`, `constellation.test.mjs` | Neuron tree shape, vertex budget, constellation seats | | `signal.test.mjs`, `handoff.test.mjs`, `tactile.test.mjs` | Activity cycle, carrier handoff, pointer placement | | `app/api/vera/chat/limit.test.mjs` | Daily limit with TTL, hashed IP only, fallback when store is down or absent | +| `app/env-contract.test.mjs` | Every variable the site reads is in `.env.example` with no secret value; `.gitignore` blocks env files, databases, dumps and keys | ## End-to-end (Playwright, Chromium) diff --git a/docs/verification.md b/docs/verification.md index e5f6b2d..d42c786 100644 --- a/docs/verification.md +++ b/docs/verification.md @@ -12,6 +12,27 @@ A change is acceptable when, from the capsule root: 3. Vera changes are exercised against a stub or closed upstream (no real Dify calls in tests); 4. the result is recorded here and in `.agents/HANDOFF.md`. +## 2026-11-21 — env template and ignore rules (Claude Fable 5.2) + +Branch `chore/env-contract-and-ignore-rules-2026-11-21`, cut from the public `main` (`c3541d3`) in a +scratch clone of `gaffercode/Sentraverse`. Linux, Node 24.22.3, pnpm 11.21.0. Commands run via +`node scripts/pnpm.mjs` on a clean clone. + +| Command | Finished (UTC) | Exit | Result | +| --- | --- | --- | --- | +| `install --frozen-lockfile` | 07:33 | 0 | Lockfile unchanged | +| `typecheck` | 07:33 | 0 | No errors | +| `lint` | 07:33 | 0 | No findings | +| `test` | 07:33 | 0 | 63/63 (59 existing, 4 new in `app/env-contract.test.mjs`) | +| `build` | 07:33 | 0 | Production build written | +| `deploy:dry-run` | 07:33 | 0 | Artifact present, no server-only names in the client bundle | + +The four new tests were watched failing before the change: no `.env.example`; thirteen probe paths +not ignored (`.env`, `.env.production`, a nested `.env`, SQLite and `.db` files, dumps, backups, key +files); and, with the ignore rule in place but no exception, `.env.example` itself ignored. + +NOT RUN: `test:e2e` (no browser on this machine) and live Vera. + ## 2026-11-20 — documentation pass (Claude Opus 5.5) Branch `docs/sentraverse-neural-srds`, capsule `projects/healthcare/sentraverse-neural`,