|
| 1 | +# AGENTS.md |
| 2 | + |
| 3 | +## Scope |
| 4 | +- Applies to the whole repository. |
| 5 | +- Covers: **code edits, tests, and documentation**. |
| 6 | +- Default mode is **minimal, surgical modification** unless explicitly asked otherwise. |
| 7 | + |
| 8 | + |
| 9 | +## Language and dependencies |
| 10 | +- Use **base R (≥ 4.1)**. |
| 11 | +- Native pipe `|>` is allowed and preferred. |
| 12 | +- **Do not introduce new dependencies**. |
| 13 | +- Existing dependencies (e.g. `cli`, `glue`) may be used. |
| 14 | +- Use `glue::glue()` for string interpolation. |
| 15 | +- Avoid `paste()` unless strictly necessary for performance-critical code. |
| 16 | + |
| 17 | + |
| 18 | +## General principles |
| 19 | +- Assume inputs are **valid and well-formed**. |
| 20 | +- Do not add defensive programming or guards unless explicitly requested. |
| 21 | +- Prefer the **shortest correct implementation**. |
| 22 | +- Avoid unnecessary line breaks and verbosity. |
| 23 | +- Avoid introducing new variables unless they: |
| 24 | + - prevent recomputation, or |
| 25 | + - are required for correctness or performance. |
| 26 | + |
| 27 | + |
| 28 | +## Performance |
| 29 | +- **Performance is a priority**. |
| 30 | +- Always prefer: |
| 31 | + - vectorized operations |
| 32 | + - matrix/array operations |
| 33 | + - `rowSums`, `colSums`, `sweep`, `%*%`, `outer` |
| 34 | +- Prefer faster primitives over `apply()` when possible. |
| 35 | +- `apply()` and `Map()` are allowed when they are efficient and appropriate. |
| 36 | +- Avoid: |
| 37 | + - unnecessary copies |
| 38 | + - repeated computation |
| 39 | + - hidden coercions |
| 40 | +- Do not optimize memory vs speed unless explicitly requested. |
| 41 | + |
| 42 | + |
| 43 | +## Code style and structure |
| 44 | + |
| 45 | +### Pipes |
| 46 | +- Prefer compact pipe chains: |
| 47 | +```r |
| 48 | +x |> f() |> g() |> h() |
| 49 | +``` |
| 50 | +- Do not introduce intermediate variables unless justified. |
| 51 | + |
| 52 | +### Comments |
| 53 | +- Add minimal comments per logical section. |
| 54 | +- Comments describe **intent**, not obvious behavior. |
| 55 | +- Use short section headers when needed: |
| 56 | +```r |
| 57 | +# Compute pressure mismatch |
| 58 | +# Normalize likelihood surface |
| 59 | +``` |
| 60 | + |
| 61 | +### Function edits |
| 62 | +- Make the **smallest possible patch**. |
| 63 | +- Do not refactor unrelated code. |
| 64 | +- Do not reorder code unless required. |
| 65 | + |
| 66 | + |
| 67 | +## Validation and checks |
| 68 | +- Input validation belongs **only at public function entry points**. |
| 69 | +- Internal/helper functions should have **no validation**. |
| 70 | +- Do not add checks unless there is a **real risk of silent failure or corrupted results**. |
| 71 | + |
| 72 | + |
| 73 | +## Numerical stability |
| 74 | +- Preserve existing numerical behavior by default. |
| 75 | +- You may introduce safeguards **only when there is clear instability risk**, such as: |
| 76 | + - `log(0)` |
| 77 | + - division by zero |
| 78 | + - unstable normalization |
| 79 | +- When adding such safeguards: |
| 80 | + - keep them minimal |
| 81 | + - **explicitly explain why they are needed** |
| 82 | + |
| 83 | + |
| 84 | +## Output invariants (strict) |
| 85 | +Edits must preserve: |
| 86 | +- class |
| 87 | +- dimensions |
| 88 | +- ordering |
| 89 | +- column names |
| 90 | +- attributes |
| 91 | + |
| 92 | +If a change would improve performance or correctness but break these: |
| 93 | +- **do not apply it silently** |
| 94 | +- explain the tradeoff and ask or clearly flag it |
| 95 | + |
| 96 | + |
| 97 | +## Error handling |
| 98 | +- Do not introduce `tryCatch()` or defensive error handling. |
| 99 | +- Errors should occur naturally unless explicitly handled at entry points. |
| 100 | + |
| 101 | + |
| 102 | +## Matrix and vector behavior |
| 103 | +- Prefer matrix/array operations for performance. |
| 104 | +- Avoid unnecessary conversion between matrix and data.frame. |
| 105 | +- Preserve original data structures. |
| 106 | + |
| 107 | + |
| 108 | +## Silent failure risks |
| 109 | +Only intervene if there is clear risk of: |
| 110 | +- unintended recycling |
| 111 | +- dimension dropping |
| 112 | +- implicit coercion |
| 113 | +- invalid numerical operations |
| 114 | + |
| 115 | +Otherwise, assume correct usage. |
| 116 | + |
| 117 | + |
| 118 | +## Rewriting vs patching |
| 119 | +- Default: **minimal patch** |
| 120 | +- If code is clearly inconsistent, inefficient, or unclear: |
| 121 | + - rewriting the function is allowed |
| 122 | + - **must explicitly warn the user** |
| 123 | + |
| 124 | + |
| 125 | +## Tests and documentation |
| 126 | +- Do not add or modify tests unless explicitly requested. |
| 127 | +- Do not add documentation (roxygen, README, vignettes) unless requested. |
| 128 | + |
| 129 | + |
| 130 | +## Hard constraints (never do) |
| 131 | +- Do not introduce new dependencies |
| 132 | +- Do not add unnecessary validation |
| 133 | +- Do not refactor unrelated code |
| 134 | +- Do not change output structure silently |
| 135 | +- Do not convert data structures unnecessarily |
| 136 | +- Do not add helper functions used only once |
| 137 | + |
| 138 | + |
| 139 | +## Uncertainty |
| 140 | +- If requirements are unclear or incomplete, **ask for clarification**. |
| 141 | +- Do not guess when behavior may change. |
0 commit comments