Skip to content

Latest commit

 

History

History
247 lines (198 loc) · 11.3 KB

code-standards.md

File metadata and controls

247 lines (198 loc) · 11.3 KB

Fledge code standards

Short acceptance rules for agents and maintainers. Hard rules live in AGENTS.md; dated lessons live in LESSONS.md and are promoted here after at least three days.

These are Fledge's acceptance rules for people and automation. They are not a license to copy another product's code or docs.

Verifier honesty

When you report status on a change:

  1. Tip SHA — name the exact commit (git rev-parse HEAD).
  2. Ran — list each command you personally ran at that tip, with pass/fail.
  3. Not run — list material checks you did not run (Docker, real SMTP, real GitHub OAuth, a real bridge job). Missing evidence is a gap, not a PASS.
  4. No borrowed credit — the author's claims and CI on another SHA are not your result.
  5. PASS wording — say only what the evidence supports: “unit + lint + build green at abc1234; Docker and SMTP not run”.

A PASS must contain at least one real Ran: command. A report without one is not a conclusion.

Fail without change

  • Add the regression test first and run it red on the unbroken tip.
  • Then fix and run it green at the new tip. Keep both halves of the evidence.
  • Test the class, not one sample. If a token or URL has three shapes, cover the shared helper and the sibling shapes.
  • A test that passes on the unbroken tip is not a regression test; say so.

Fix the class, not the sample

  • If review finds one escape path, fix the shared helper and cover the siblings.
  • Same-class reds tighten this file or LESSONS; do not paper over them with a special case.

Architecture boundaries

  • npm run arch:check is a step in the lint CI job and a test in the suite. It reads the tree, not the diff.
  • A view may import db row types; it may not import the store. The store must not import app, a view or a job. A job must not import a view. A view may import only src/jobs/continuity.ts and src/jobs/automationHealth.ts from the job layer; the job service and the bridge stay out of the view.
  • A new source file is at most 1,200 lines. The five files that predate the limit are recorded in scripts/lib/arch-grandfather.json at their current size; an entry may not grow, and it must be deleted once the file is at or below 1,200 lines.
  • A type-only import is erased at build time and is allowed across a boundary; the gate is about runtime coupling.

Silent catches

  • A catch that swallows an error says why in a comment inside the block, or handles it.
  • A fire-and-forget promise (.catch(() => {}) whose result is dropped) needs a void or a one-line reason.
  • Test helpers are not exempt from honesty, only from the production gates.

Lint warnings

  • npm run lint fails on any warning lint-warnings.baseline.json does not name, and on a baseline entry the tree no longer produces.
  • The baseline carries only warnings that predate the gate. Fix a warning when you are already in the file; run npm run lint:update in the same reviewed change so its entry leaves with it.
  • Never add a rule-level off switch, an inline disable, or an exclude to get to green; the baseline is the only allowance the gate carries.

Security

Secrets

  • Never commit or log a GitHub token, a model key, an SMTP password, a session cookie, an approval token, a flg_ token, or a full .env.
  • GitHub tokens and user model keys are encrypted with AES-256-GCM (TOKEN_ENCRYPTION_KEY). Never store the key in the database.
  • The development mail outbox contains approval links; mode 0600, never commit.

Untrusted strings

  • Escape every value interpolated into HTML (escapeHtml).
  • A GitHub login, repo name, branch, PR URL or model name comes from an external system: validate the shape and cap the length before storing or rendering it.
  • A callback body is untrusted until its bearer secret matches and the fields pass the type checks.

SSRF and egress

  • User model base URLs must be https:// for public hosts; http:// only for loopback with ALLOW_PRIVATE_MODEL_URLS=true.
  • Reject IP literals and resolved addresses in private/loopback/link-local ranges, and URLs with credentials.
  • GitHub API calls go to GITHUB_API_BASE (default https://api.github.com); never build a URL from a user-supplied host.

Authentication and authorization

  • Sessions: random 32-byte token, SHA-256 at rest, HttpOnly/SameSite cookie.
  • Every form has a CSRF token; approval links and job endpoints use their own bearer secret instead.
  • Approval tokens are single-use and expire in 7 days; a status change requires pending.
  • DEV_LOGIN_LOGIN is refused in production; startup fails closed.
  • A repo route checks ownership before reading or mutating.

Quota

  • Platform-funded calls are metered per user; the platform key is never returned.
  • Use x-litellm-response-cost when present; otherwise estimate from usage.
  • A provider error deducts nothing. A request already in flight may finish; the next request sees the updated balance.
  • A quota-exhausted notice is deduped per user per day.

Merge only a reviewed tip

  • T0/T1: merge through the queue when ci-ok is green, the PR is mergeable and design-conformant. No PASS needed.
  • T2/T3: a PASS at the same tip from a reviewer in .github/agent/review-policy.json, where the author of the tip may never PASS it, and green ci-ok. The agent that gated it enqueues it itself.
  • A green required check means the tip can merge, not only that the code compiles: review-gate reads the tip's review threads and fails on an unresolved one, naming its path:line, because the branch requires conversation resolution and the merge API refuses such a tip no matter what the checks say (#308).
  • Any new commit voids the PASS. Re-run the Verifier on the new tip.
  • A tip carrying a commit authored by a reviewer identity cannot be independently reviewed, and no verdict at any SHA can turn that green: the reviewer-authored job names the offending commit at the push that makes it true (#598), and the fix is to re-author it rather than to ask for another review round.
  • Never admin-merge, never bypass a required check, never force-push main.
  • One PR per issue. Push to the existing branch instead of opening a duplicate.

Required contexts

A required context is green only when its latest settled conclusion is success. status != completed is pending, never missing: a context whose only job object is unsettled after the run finished is reported as pending and stays red, and the publisher creates no check run at all while any required context is unsettled. A context with no job object at all is missing and stays red — the two words are distinct on purpose, because missing sends a reader to look for a job that did run. When one context carries several conclusions at one commit, the newest settled one decides, and every conclusion seen is recorded in the published summary, so "two conclusions" can be told from "no conclusions" without re-querying the API. ci-ok has one workflow-file producer, ci-ok-publish.yml; ci-ok-refresh.yml posts a red ci-ok through the API on every push to main, and scripts/lib/reserved-contexts.mjs reads job ids and job-level name: values, so that producer sits outside the scanner's view by design and must keep blocking — deleting it would let a stale green stand.

The issue tie in a PR body

pr-lint requires every pull request to name the issue it belongs to, in one of these shapes, each at the start of a line:

  • Fixes #N — the pull request closes that issue. GitHub reads this itself at merge time; the gate only accepts it.
  • Part of #N or Refs #N — the pull request is one slice of an epic and closes nothing. The success line says so, so the reader can tell the two apart: pr-lint: ok (#603, T3, 10 files; tracks #546, closes nothing).
  • No-issue: <reason> or Investigates: <reason> — no issue stands behind the change.

A tracking reference is never read as a close, and Fixes keeps its meaning: nothing may infer a close from Part of #N. A body with none of these fails and the message lists all the accepted shapes. Partner #544 and Part of #544x are not references — the anchor is a whole word, so a reference cannot be found inside another word.

Review report shape

Verdict: PASS at <sha>
Written-by: <GitHub account>
Author: <Bot identity>
Ran:
- `npm run check` — pass
- `node scripts/risk-gate.mjs --base origin/main` — T2
Not run: Docker image, real SMTP

The tier in that Ran: block is computed from the diff, not read off the label: a path that would be T1 is raised to T2 by a deleted assertion, an always-true assertion, or an inline client-side script (#869). scripts/lib/risk.mjs is the one classifier the review gate and pr-lint both call.

  • First line: exactly Verdict: PASS|INC|REQUEST_CHANGES at <sha>.
  • Second line: Written-by: the GitHub account that posted the verdict. It must be in the policy's writers list; that check is unchanged.
  • Third line: Author: the Bot identity. It is a declaration for readers and the gate never reads it: no body line can satisfy — or waive — the gate.
  • Identity is the platform object. A verdict submitted as a review is decided by review.user.login against the policy's required reviewer, so no Posted-by: line is needed or believed. A verdict posted as an issue comment is the legacy shape: it decides only while the pull request has no review object at all and before COMMENT_VERDICT_FALLBACK_UNTIL (2026-12-14) in scripts/lib/review-gate.mjs, and it must carry Posted-by: matching its own author. After that date the gate refuses it and says to post a review.
  • The PR author may never PASS its own tip: the policy carries no self-review waiver.
  • One tip carries one conclusion. A replacement PASS at the same tip is allowed only when the first was rejected for a missing Ran: line, and it must say Replaces:.
  • A hold label plus a Record only — do not merge comment blocks the queue.

Docs ownership

A change to an owned module updates its doc in the same PR:

Area Doc
Auth, approvals, email docs/security.md
Gateway, quota docs/quota.md
Repos, job callback docs/api.md
App, DB, views docs/architecture.md
Bridge bridge/README.md
Deploy docs/deploy.md

Docs-not-needed: in the PR body and the branch's commit message is the only excuse. A reviewer fails an owned-path change without either.

Tests

  • node:test and node:assert/strict; no test framework dependency.
  • Fakes: in-memory SQLite, a local HTTP server for GitHub/model upstreams, a memory mailer. No network in CI.
  • Each test closes its server, store and app.
  • A test that depends on wall-clock timing gets a generous window and no retries-as-pass.

Migrations and data

  • The schema is additive and idempotent (CREATE TABLE IF NOT EXISTS).
  • Bump PRAGMA user_version for a breaking change and write the migration.
  • Never edit a user's live database in a test; use :memory: or a temp file.
  • npm run db:backup is the only supported online backup.

Tiny loops

  • One issue (or one tightly coupled pair) per branch and worktree.
  • Security fixes before large features.
  • After a PASS and merge, stop or take the next issue; do not pile unrelated diffs into the same PR.