Skip to content

Add AGENTS.md agent instructions - #72

Merged
roninsightrx merged 4 commits into
masterfrom
add-agents-md
Nov 24, 2026
Merged

roninsightrx merged 4 commits into
masterfrom
add-agents-md

Conversation

@roninsightrx

Copy link
Copy Markdown
Contributor

Adds an AGENTS.md at the repo root with instructions for AI coding agents working on clinPK.

What it contains

  • Commands — devtools::test(), running a single test file, devtools::document(), devtools::check(), and the R CMD build/check --as-cran equivalents, plus what CI actually runs.
  • Generated files — man/*.Rd and NAMESPACE are roxygen2 output and must not be hand-edited, with a note about the RoxygenNote: 7.3.2 pin causing .Rd churn under newer roxygen2.
  • Architecture — the R/ naming conventions (calc_*, pk_1cmt_*/pk_2cmt_* suffix grammar, pct_*_for_*, nca*), and the method-dispatch pattern: a validating wrapper plus small pure <quantity>_<method>() equation functions, backed by check_input_lengths(), prepare_method_inputs() and normalize_sex() in R/utils.R.
  • Conventions — vectorization expectations (ifelse() over if, vapply() over sapply()), the list(value =, unit =) return shape, centralizing unit strings in valid_units(), relative vs. absolute eGFR, testthat practices, commit/branch style, and CRAN backwards-compatibility constraints.
  • Adding a new equation — the step-by-step checklist, including citing the publication and updating NEWS.md.

Why AGENTS.md and not CLAUDE.md

AGENTS.md is the emerging cross-harness convention, so the same file is picked up by other agent tooling rather than only one vendor's. The content is written harness-agnostically and refers to "agents" rather than any specific product.

AGENTS.md is added to .Rbuildignore so R CMD check --as-cran does not flag it as a non-standard top-level file.

Verified by running devtools::test() on this branch: 678 passing, 0 failures.

🤖 Generated with Claude Code

Documents build/test/document commands, the method-dispatch architecture
used by the multi-method calc_* functions, vectorization and return-value
conventions, and the workflow for adding a new published equation.

Also excludes AGENTS.md from the built package via .Rbuildignore so
R CMD check --as-cran does not flag a non-standard top-level file.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

@roninsightrx roninsightrx left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested: AGENTS.md currently gives agents commands and conventions that are factually incompatible with this branch.

  • AGENTS.md:28 — The standalone testthat::test_file("tests/testthat/test_calc_egfr.R") command does not load the package under test. In a clean export of this branch it produced 21 failures because calc_egfr() was not found. Replace it with the already documented devtools::test(filter = "calc_egfr"), or load the package first (for example, devtools::load_all(); testthat::test_file(...)).
  • AGENTS.md:88-113 — calc_ibw() is named as an example of this wrapper/inner-function pattern, but it does not follow the stated rule: it uses separate method_children and method_adults arguments, its ibw_standard() and ibw_devine() helpers perform validation and emit warnings, and the wrapper returns a bare numeric vector. Remove calc_ibw() from this example or document it explicitly as a legacy exception so an agent does not refactor it under a false invariant.
  • AGENTS.md:149-150 — The claim that clinical quantity functions return list(value, unit) and accept digits is not a package-wide convention. For example, calc_bmi(), calc_abw(), and calc_ibw() return numeric values and do not accept digits. Narrow this statement to the functions that actually use that interface and tell agents to preserve each exported function's existing return contract.
  • AGENTS.md:151-156 — Unit-conversion APIs and unit registries are not uniform as described. weight2kg(value, unit) and the helpers in metric_conversion.R return numeric values; conc2mol()/mol2conc() use unit_conc and unit_mol; and convert_conc_unit(), conc2mol(), and mol2conc() maintain unit tables outside valid_units(). Split this into the actual conversion families and direct agents to the registry/table used by the function they are changing.

Reviewed by Codex

- Drop the standalone `testthat::test_file()` command: it does not load the
  package and fails with "could not find function". Note the caveat and point
  at `devtools::load_all()` / `devtools::test(filter = ...)` instead.
- Remove `calc_ibw()` from the method-dispatch example and document it as a
  legacy exception, so it is not refactored under a false invariant.
- Narrow the return-value conventions: `list(value, unit)` and `digits` are
  not package-wide. List which functions use which shape and tell agents to
  preserve each function's existing contract.
- Split the unit-conversion section into the actual families, since
  `unit_in`/`unit_out` and `valid_units()` are not universal:
  `conc2mol()`/`mol2conc()` use `unit_conc`/`unit_mol`, `convert_conc_unit()`
  keeps its own factor table, and `convert_flow_unit()`/`weight2kg()` return
  bare numerics.

Also make the roxygen2 caveat precise: roxygen2 8.x replaces `RoxygenNote`
with `Config/roxygen2/version` rather than bumping it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@roninsightrx

Copy link
Copy Markdown
Contributor Author

All four findings verified against the source on this branch and all four were correct. Fixed in 11273c6.

1. AGENTS.md:28 — bare testthat::test_file() — Confirmed: reproduced could not find function "calc_egfr". Removed the standalone command and added a note that test_file() does not load the package; use devtools::load_all() first or devtools::test(filter = ...). Verified both replacements pass (99 and 105 assertions respectively).

2. calc_ibw() in the method-dispatch example — Confirmed: it uses method_children/method_adults, its ibw_standard()/ibw_devine() helpers validate and warn, and the wrapper returns a bare numeric. Removed from the example list and documented explicitly as a legacy exception not to be refactored under the stated invariant.

3. list(value, unit) + digits overclaim — Confirmed: only calc_lbw(), calc_ffm(), calc_creat(), calc_creat_neo() take digits; calc_bsa() returns list(value, unit) without it; calc_bmi()/calc_abw()/calc_ibw() return bare numerics; calc_egfr() returns a wider list including resolved covariates. Replaced the blanket claim with per-shape lists and an instruction to preserve each function's existing return contract.

4. Unit-conversion APIs/registries not uniform — Confirmed: conc2mol()/mol2conc() use unit_conc/unit_mol with inline unit vectors, convert_conc_unit() keeps its own conv table independent of valid_units(), convert_flow_unit() uses from/to and returns a bare numeric, and weight2kg() checks valid_units() but returns a bare numeric. Split into the actual families with a pointer to use the registry the function already uses.

One extra: running devtools::document() to check the documented workflow showed roxygen2 8.x replaces RoxygenNote with Config/roxygen2/version rather than bumping it, so that caveat was made precise. No generated files were committed; full suite passes (678 assertions).

🤖 Generated with Claude Code

@roninsightrx roninsightrx left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not ready to approve: the original findings are substantially addressed, but three factual mismatches remain in the revised guidance.

  • AGENTS.md:92-117 — The named calc_ffm() and calc_egfr() implementations do not uniformly have pure inner functions with all validation in the wrapper. ffm_bucaloiu() checks method applicability and emits a warning, while egfr_bedside_schwartz() checks age and emits a warning controlled by verbose. Qualify this as the general pattern while documenting that method-specific applicability checks can remain in inner functions, or move those checks into the wrappers before retaining the absolute wording.
  • AGENTS.md:162-168 — The revised return-shape inventory still drops public fields: calc_ffm() and calc_creat() return method in addition to value and unit. Also, capped in the calc_egfr() result is cap metadata, not a resolved covariate. Split those shapes or say the first group returns at least value/unit, explicitly preserve the method field, and describe capped separately from the resolved covariates.
  • AGENTS.md:173-180 — Extending only valid_units() is insufficient when adding a spelling for these wrapper functions: the same spelling must also exist in convert_conc_unit()'s conv table or the delegate rejects it as unrecognized. Tell agents to keep valid_units() and the factor table in sync for units handled through these wrappers.

Reviewed by Codex

@mccarthy-m-g mccarthy-m-g left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to the "three factual mismatches remain in the revised guidance", some additional suggestions and comments. The broader point on keeping unified instructions across our repos might be worth a group discussion on how to best handle that.

My current idea is that we just have an additional instruction that tells the agent if it updates a generic instruction in this repo it should also prompt us to update those instructions in our other repos. Although that would be a pain since we have a lot of repos. Is there some way we can have a single "Basic R package instructions" generic in a public repo that the repo-specific AGENTS.md can all reference?

Comment thread AGENTS.md Outdated
Comment on lines +33 to +35
`testthat::test_file()` on its own does not load the package under test and
will fail with "could not find function". Call `devtools::load_all()` first in
the same session, or just use `devtools::test(filter = ...)`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this necessary given the way to do this is documented above?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed — condensed to one line that just names the subset command and the reason not to reach for testthat::test_file(), since an agent's default instinct is to use it.

Comment thread AGENTS.md Outdated
Comment on lines +37 to +42
Command line equivalents:

```sh
R CMD build . && R CMD check --as-cran clinPK_*.tar.gz
Rscript -e 'devtools::test()'
```

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Equivalents to what? The agent could probably use more context here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworded to "Equivalent checks from a shell, for a CI-like run or when working outside an R session". Note CI actually runs these via rcmdcheck in the r-lib action rather than literally, so I avoided claiming it is exactly what CI runs.

Comment thread AGENTS.md Outdated
CI (`.github/workflows/R-CMD-check.yaml`) runs `R CMD check` with
`--no-manual --as-cran` on ubuntu-latest / R release, and fails on errors
(warnings and notes do not fail the build). A second workflow builds the
pkgdown site from `_pkgdown.yml`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this in the command examples for InsightRX/mipdeval#51 but not not in the examples here despite there also being a pkgdown site.

# Build pkgdown site
pkgdown::build_site()

We should make sure generic agent instructions are unified across repos for consistency. Might need to add additional instructions to each one that prevent drift across repos too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added pkgdown::build_site() to the command block and named the workflow (.github/workflows/pkgdown.yaml, which uses build_site_github_pages() on pushes to master). The cross-repo unification and anti-drift instruction is deferred for team discussion.

Comment thread AGENTS.md
(warnings and notes do not fail the build). A second workflow builds the
pkgdown site from `_pkgdown.yml`.

### Documentation is generated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment here: In mipdeval's agent instructions documentation instructions are a footnote at the bottom in the General notes section. I think the unique heading approach here is nicer. We should unify whatever documentation instructions we want across repos.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No local change needed since you prefer the dedicated-heading approach used here; unifying the documentation-instructions layout across repos is deferred for team discussion.

Comment thread AGENTS.md
`git diff` after documenting and keep the diff limited to the functions you
actually changed.

### Package data

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar point here. We give different details on how data-raw works in this package than in mipdeval. Some of this is because they're doing slightly different things, but there's probably room to unify better since we're broadly using data-raw to "preserve the origin story of package data" (https://r-pkgs.org/data.html#sec-data-data-raw)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the framing that data-raw/ preserves the origin story of the package data, with the r-pkgs link, kept above the clinPK-specific growth-chart detail. Broader unification with mipdeval deferred for team discussion.

Comment thread AGENTS.md Outdated
Comment on lines +209 to +210
testthat edition 3, tests in `tests/testthat/`. File naming is inconsistent
(`test_foo.R` and `test-foo.R` both occur) — match the neighbouring files for

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inconsistent naming is possibly my fault. I use usethis::use_test() to create new test files from the currently active script when writing new functions. It uses the test- separator, but our historic convention is test_ and it's easy to miss which is being used in a review since it's a tiny detail.

My personal suggestion would be to do some tech-debt cleanup and make all our repos use test-, and then drop this file naming comment from the instructions. Or if we want to stick to the historic convention make that an explicit agent instruction in all our repos

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made the convention explicit: name new files test_<topic>.R, and noted that usethis::use_test() produces the hyphenated form so it needs renaming (64 files use test_, 6 use test-). Tech-debt renaming across repos is deferred for team discussion.

Codex follow-up:
- Qualify the method-dispatch rule: method-specific applicability checks may
  stay in the inner function (`ffm_bucaloiu()`, `egfr_bedside_schwartz()`)
  instead of claiming inner functions do no validation at all.
- Correct the return-shape inventory: `calc_ffm()` and `calc_creat()` also
  return `method`, and `capped` in the `calc_egfr()` result is cap metadata
  rather than a resolved covariate.
- Note that adding a unit spelling means editing both `valid_units()` and the
  `conv` table in `convert_conc_unit()`, since the delegate rejects unknown
  spellings.

Reviewer suggestions:
- Add `pkgdown::build_site()` to the commands and name the pkgdown workflow.
- Explain what the shell block is equivalent to, and condense the
  `testthat::test_file()` caveat now that the subset command is documented.
- Describe `data-raw/` as preserving the origin story of the package data.
- State the `test_<topic>.R` naming convention explicitly and note that
  `usethis::use_test()` produces the hyphenated form.

Also move the general return-value notes out of the unit-conversion list and
give unit conversion its own heading.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@roninsightrx

Copy link
Copy Markdown
Contributor Author

Round 3 pushed in 1f36e6e. All three Codex follow-up findings verified against source and all three were correct; all six inline comments answered on their threads.

Codex follow-up

  1. Inner functions are not uniformly pure — Confirmed: ffm_bucaloiu() warns when the patient is not an obese female, and egfr_bedside_schwartz() warns below age 1 (and takes verbose for it). Softened to "as a general rule" and documented method-specific applicability checks as a deliberate exception that stays in the inner function, with both named. Did not hoist the checks into the wrappers — that is a behaviour change, not a docs fix.
  2. Return-shape inventory drops fields — Confirmed: calc_ffm() and calc_creat() also return method; calc_lbw()/calc_creat_neo()/calc_bsa() do not. Split into "at least value/unit + digits" (with an explicit instruction to preserve method) and "value/unit only" for calc_bsa(). capped is now described separately as cap metadata (min_value/min_n, max_value/max_n, empty when no capping applied), not a resolved covariate.
  3. valid_units() alone is insufficient — Confirmed: convert_conc_unit() gates on its own conv table and rejects anything else (reproduced Unrecognized unit: mg_per_dl). The two are currently in sync for scr/bilirubin/serum_albumin, but the instruction now says to edit both places and keep them in sync.

Reviewer suggestions (@mccarthy-m-g)

  • Added pkgdown::build_site() to the command block, and named the pkgdown workflow plus the build_site_github_pages() call it uses.
  • Clarified what the shell block is equivalent to; avoided saying it is literally what CI runs, since CI goes through rcmdcheck in the r-lib action.
  • Condensed the testthat::test_file() caveat to one line now that the subset command is right above it.
  • Framed data-raw/ as preserving the origin story of the package data, with the r-pkgs reference, above the clinPK-specific detail.
  • Made the test-file convention explicit (test_<topic>.R; 64 files use it vs 6 with a hyphen) and noted that usethis::use_test() produces the hyphenated form and needs a rename.
  • Also moved the general return-value notes out of the unit-conversion bullet list and gave unit conversion its own heading.

Deferred for team discussion — the cross-repo questions are all out of scope for this PR and nothing in other repos was touched: a shared "Basic R package instructions" doc that repo-specific AGENTS.md files reference, an anti-drift instruction to prompt updates in sibling repos, unifying the documentation-instructions layout and the data-raw wording with mipdeval, and tech-debt renaming of test files across repos. The local, concrete part of each was applied here instead.

File stays harness-agnostic (no agent product names) at 267 lines. Full suite green: 678 assertions, 0 failures. CI on this branch is passing.

🤖 Generated with Claude Code

@roninsightrx
roninsightrx merged commit 0afa36b into master Nov 24, 2026
1 check passed
@roninsightrx
roninsightrx deleted the add-agents-md branch November 24, 2026 05:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants