Skip to content

Latest commit

 

History

History
129 lines (101 loc) · 15.5 KB

AGENTS.md

File metadata and controls

129 lines (101 loc) · 15.5 KB

Agent Instructions

Project Goal

The goal of this project is to provide a FHIR® Server with an internal CQL Evaluation Engine which is able to answer population wide aggregate queries in a timely manner to enable interactive, online queries over millions of patients.

Project Structure

  • Language: Main language is Clojure with some parts written in Java.
  • Build System: Clojure tools.deps. All build steps are available via Makefiles.
  • Modules: Most code is located in modules/. Each module is structured similarly.
    • Frontend: A special module modules/frontend written in Svelte.
  • Server: The FHIR Server is built via the top-level Makefile.

Code Conventions & Idioms

Rigorous adherence to these patterns is required:

  • Functional Core: Blaze is primarily implemented using pure functions.
    • Avoid Bang Functions: Don't introduce functions with side effects (! suffix) if you can avoid it. Instead of a function wrapping swap! or reset!, extract only the pure state transition and apply it at the call site (e.g. (swap! running-jobs mark-running id) with a pure mark-running), so the effect stays visible where it happens. Likewise, pass the dereferenced value instead of the atom to functions that only read state.
  • Error Handling: Use Cognitect Anomalies for error handling. Do not use Exceptions for control flow.
  • Component System:
    • Use Integrant for wiring components.
    • Implement ig/init-key and ig/halt-key! multimethods.
    • Define m/pre-init-spec for dependency validation.
  • Specs:
    • Every public function must have a spec. Exception: functions in namespaces whose last segment is impl (e.g. blaze.luid.impl, but not blaze.db.impl.index) don't need specs.
    • Return Values: Every s/fdef must have a :ret spec. st/instrument checks only :args, so a wrong or missing :ret never fails a test run — that's the reason to be deliberate about it, not a reason to leave it out. For a function returning either a value or an anomaly, use (s/or :value <pred> :anomaly ::anom/anomaly).
    • Location: Specs must never be defined inline in the implementation namespace. There are two distinct spec namespace conventions:
      • s/def (data/attribute specs) → dot-separated *.spec namespace (e.g., blaze.db.node.spec for blaze.db.node), in a spec.clj file nested under the namespace directory.
      • s/fdef (function specs) → hyphen-separated *-spec namespace (e.g., blaze.db.node-spec for blaze.db.node), as a sibling file to the implementation.
    • Classpath: Public module-level specs go in src, but inner-module public function specs should be in test to keep the production classpath small.
  • Java Interop:
    • Avoid reflection.
    • Mandatory: Add (set! *warn-on-reflection* true) to any namespace performing Java interop.
  • Null Checks in Java: Guard every argument of a public constructor or method of a Java class that must not be null with requireNonNull, statically imported from java.util.Objects (e.g. this.snapshot = requireNonNull(snapshot);). So a null fails at the call passing it, not later where it is used, possibly on another thread. Arguments of non-public constructors and methods don't need a guard, because their callers are in the same package.
  • Async Composition:
    • Prefer the blaze.async.comp/do-sync macro over blaze.async.comp/then-apply where feasible, also for a single step. Binding the result to a name reads better than an anonymous function like #(into [] (take 2) %). If the then-apply is followed by other steps like blaze.async.comp/exceptionally, put the do-sync form first in the threading macro.
    • Always use blaze.async.comp/then-apply, blaze.async.comp/then-compose and similar in conjunction with the threading macro (->), never as a standalone call.
  • Macros:
    • Macros must always be :refered directly so they can be used without a namespace alias prefix (e.g., [blaze.async.comp :refer [do-sync]]).
  • Reuse:
    • Avoid code duplication.
    • Use existing functions if possible.
    • Create a function if code is used more than two times.
  • Forms over Strings: Analyse and transform FHIRPath expressions as forms, never as source strings (no splitting, regex replacement or assembling of strings). blaze.fhir-path/parse turns a string into a form, blaze.fhir-path/compile compiles a form, and blaze.fhir-path/form returns the form of an already compiled expression. Derive new expressions by transforming forms with plain Clojure (e.g. clojure.walk) and compile the result directly. Use blaze.fhir-path/unparse only to show a form as FHIRPath, e.g. in messages or as a lookup key, never to parse it again. Likewise, analyse CQL on its ELM or on the forms of compiled expressions (blaze.elm.compiler/form).
  • Named Functions over Explaining Comments: If a piece of code needs a comment to explain what it does or why, especially an anonymous function whose intent isn't obvious (e.g. #(or % false) in a swap!), extract it into a function with a name stating its intent and put the explanation into its docstring, instead of writing an inline comment.
  • Docstrings Name Visible Arguments: If a docstring refers to an argument that the parameter vector destructures, so that its name doesn't appear in the signature (e.g. {node-id :id} while the docstring talks about node), add :arglists metadata with the names the docstring uses (e.g. {:arglists '([db graph node state resource-handle])}). This applies to public and private functions alike.
  • No no-op nil calls: Don't deliberately call a function with a nil argument just so it does nothing and returns its input unchanged (e.g. a nil-tolerant pass-through wrapper for an optional dependency). This is an antipattern — it hides the optionality at the call site. Make the call conditional instead (e.g. with cond->), so the absence of the dependency is visible where it matters.
  • Testing:
    • Test-Driven Development (mandatory): Never change production code without first writing a failing test that captures the new behaviour. Write the test, see it fail, then make it pass. This applies to bug fixes (regression test first), new features, and contract changes (e.g. allowing an anomaly return value).
    • Spec Instrumentation: Always enable spec instrumentation in tests to catch spec violations early.
      • Require [clojure.spec.test.alpha :as st].
      • Call (st/instrument) at the top level of the test namespace.
    • Java Interop: Enable reflection warnings ((set! *warn-on-reflection* true)) ONLY if the test namespace performs Java interop.
    • Fixtures: Use the standard test fixture in all test namespaces.
      • Require [blaze.test-util :as tu].
      • Call (test/use-fixtures :each tu/fixture).
    • Locale: When a test asserts on locale-sensitive formatting (e.g. the thousands separator in 10,000), call (tu/set-default-locale-english!) at the top level of the test namespace. Do not make the production code locale-independent for this — set the locale in the test instead. For this to work the production code must build such strings at call time, not in a top-level def (a def is evaluated at namespace load, before the test sets the locale).
    • Assertions: Use the given macro from juxt.iota for asserting map values (e.g. anomalies).
      • Require [juxt.iota :refer [given]].
      • Example: (given (my-fn ...) ::anom/category := ::anom/fault ::anom/message := "...")
      • Use is only for simple scalar equality checks that don't involve maps.
    • Async Testing:
      • To assert that a CompletableFuture completes exceptionally with an anomaly, use given-failed-future from blaze.test-util — not the (given (ba/try-anomaly (ac/join ...))) pattern.
      • To obtain the value of a successfully-completed future inside a test, use @future (Clojure's deref).
    • Private Functions: Do not call private functions (via #') from tests. If a function needs to be tested, move it to an impl namespace where it becomes part of the public API of that namespace.

Integration Test Scripts

  • Downloading search results: In integration test scripts (.github/scripts, .github/integration-test-*), use blazectl download to fetch all resources matching a FHIR search — blazectl performs paging. Do not use curl with a large _count value, which silently truncates the result at one page.

Monitoring

  • Dashboard: Every new metric must be charted in the Grafana dashboard. Add a panel to the row of the component exporting the metric in modules/monitoring/dashboard.edn (not the generated JSON), with a description stating what the metric means and which values are expected, and update the panel and id counts in modules/monitoring/test/blaze/monitoring/dashboard_test.clj. A metric with labels exports no samples until its first use. If the integration test doesn't exercise it, add it with a comment to the allowlist in .github/scripts/check-dashboard-metrics.sh, so that the dashboard metrics check doesn't fail.

Documentation

  • Environment Variables: Every new environment variable introduced via #blaze/cfg in resources/blaze.edn must be documented in docs/deployment/environment-variables.md, following the existing format (heading, description, default value, since badge).
  • API Behaviour: Before finishing, state whether the change alters what a client can observe on an existing interaction or operation (accepted header, parameter, status code, response media type). If it does, update the corresponding page under docs/api/, including the sibling pages sharing that behaviour, and badge new sections with the version of the issue's GitHub milestone (gh issue view <issue-number> --json milestone). If it's unclear whether a behaviour change is user-facing, ask rather than guess.

Design Documents

  • Before implementing a feature that involves real design decisions (competing approaches, non-obvious rules, migrations), write a design document and get it approved. Bug fixes and small enhancements need none; the GitHub issue is enough.
  • Design documents follow the structure described in specs/README.md. Start from specs/0000-template.md and put the document in specs/YYYY-MM-DD-<topic>-design.md, never under docs/, which is published.
  • Set its status to Accepted once it's approved, and commit it together with the change. Don't commit implementation plans: they are build instructions and go stale once the code exists.
  • Don't rewrite an accepted design document when a later change alters the design. Write a new one and mark the old one as superseded, as described in specs/README.md.
  • Link the design document from the issues it was written for and from later issues building on it.

Release Notes

When generating a CHANGELOG.md entry for a new version:

  1. Fetch issues from the GitHub milestone matching the version name: gh issue list --milestone <version> --state all --limit 200 --json number,title,labels,state,url
  2. Fetch the milestone number for the "full changelog" link: gh api repos/samply/blaze/milestones --jq '.[] | select(.title == "<version>") | .number'
  3. Categorize issues by their labels into sections — in this order if present: Notes, Survey, Security, Enhancements, Performance, Bugfixes, Documentation, Maintenance. Omit issues labeled ci and dependency updates.
  4. Use issue titles verbatim (1:1) as the changelog entry text. Do not rewrite, title-case, shorten, or normalize them to the imperative mood — even when an issue title does not follow the usual title conventions. Fixing the issue title is out of scope at release time.
  5. Insert the new entry at the top of CHANGELOG.md, directly below the # Changelog heading, following the format of existing entries.
  6. End the entry with: The full changelog can be found [here](https://github.com/samply/blaze/milestone/<number>?closed=1).

Verification & Workflow

When starting to work on an issue, you can use the GitHub CLI to fetch the issue details: gh issue view <issue-number>

When creating an issue, classify it via GitHub's native issue type (e.g. Bug, Feature), not via a bug/feature label. The gh issue create flag for this is --type (e.g. gh issue create --type Bug ...). Only labels that aren't covered by a type (e.g. module:db) should be passed via --label. The deciding question between Bug and Task is whether something is actually broken, not whether production code is touched. Two cases that are therefore Task, not Bug: bugs in test code that can be fixed purely by changing only test code (e.g. a flaky test), because the production code is unaffected; and hardening production code against a failure mode that no current code path can reach (a latent invariant gap, defence in depth), because nothing is broken yet.

Always run make build-ig first on a freshly created git worktree. Some modules (e.g. job-async-interaction and anything depending on it, like interaction) need the generated IG resources, which are absent in a new worktree and would otherwise make prep fail.

Before finishing a task, ensure the following commands pass:

  1. Format: make fmt
  2. Lint: make lint (Uses clj-kondo)
  3. Test: Run tests only for the modules you changed: make -C modules/<module> test (e.g. make -C modules/db test). Use make test only when changes span multiple modules or the root.
    • To run a single test or a single namespace, use the test-focus target with a FOCUS variable holding a kaocha test id — either a whole namespace or a namespace/var: make -C modules/<module> test-focus FOCUS=blaze.db.api-test or make -C modules/db test-focus FOCUS=blaze.db.api-test/pull-fn-test.
    • Changed Java classes: Call make -C modules/<module> clean test (or clean test-focus) whenever Java classes of a module changed. The prep step compiles Java classes only once (guarded by target/prep-done), so without clean the tests silently run against the stale classes.
  4. Coverage: make test-coverage (Checks for adequate test coverage — must be ≥ 95% forms)
    • Reflection warnings printed during make test-coverage are normal: cloverage instruments the code in a way that disables type hints, so reflection warnings appear there even for correctly hinted code. Do not try to fix them based on the coverage run — only reflection warnings from make lint/normal compilation matter.

When adding a new module under modules/, also add it to the module matrix in .github/workflows/build.yml (the test job, sorted alphabetically) so CI picks it up.

Every change requires a GitHub issue. If no issue exists for the work, create one first (see above) before writing any code. All work must be tracked by an issue.

After verification, when working on an issue:

  1. Commit the changes: git add . and git commit
    • The commit title should be the issue title. Both use the imperative mood, are written in title case, and fit within about 50 characters (e.g. Fix Error Combining Composite Token-Token Params).
    • The commit body should just contain: Closes: #<issue-number>
    • Do not add a Co-Authored-By trailer (or any other AI/tool attribution). The changes are authored by the human, who appears as the commit's author and uses AI merely as a tool; the AI assistant may appear only as the commit's committer.
  2. There should be exactly one commit per issue. Multiple changes have to be ammended to the first commit.