|
| 1 | +# TrainCheck engineering guidance |
| 2 | + |
| 3 | +This file applies throughout the repository. Preserve TrainCheck's explicit type |
| 4 | +and runtime contracts: catching malformed data early is part of correctness. |
| 5 | +Follow the user's task scope and preserve unrelated work. Resolve routine choices |
| 6 | +using these rules; ask when an unresolved choice changes public behavior or scope. |
| 7 | + |
| 8 | +## Types and data contracts |
| 9 | + |
| 10 | +- Fully annotate new functions and changed function interfaces, including private |
| 11 | + helpers, worker payloads, container element types, and return values. When |
| 12 | + changing an untyped helper's behavior, give it an explicit contract. |
| 13 | +- Represent structured data with appropriate aliases, `TypedDict`, dataclasses, |
| 14 | + or small protocols. Prefer the simplest representation that states the real |
| 15 | + contract; do not introduce abstractions solely to satisfy a checker. |
| 16 | +- Do not remove annotations or assertions, widen a type to `Any`, coerce values, |
| 17 | + or add ignores merely to silence errors. A necessary contract change needs a |
| 18 | + stated reason, caller review, and tests. Document narrow uses of `Any`, `cast`, |
| 19 | + `type: ignore`, and `noqa`; existing typing debt is not a reason to add more. |
| 20 | +- Treat external JSON, framework metadata, and dataframe values as untrusted |
| 21 | + inputs. Validate and narrow at the boundary, using `object` for unknown values |
| 22 | + where practical. `cast` and `TypedDict` do not perform runtime validation. |
| 23 | +- Required fields must be required in both the type and implementation. Validate |
| 24 | + them and use direct access, such as `record["func_call_id"]`; do not substitute |
| 25 | + a default when a required field is absent or malformed. |
| 26 | +- `.get()`, `getattr()`, and optional types are appropriate when absence has a |
| 27 | + defined meaning, such as a cache miss or an unavailable framework capability. |
| 28 | + State that meaning and preserve it in the result. Do not blanket-ban these APIs. |
| 29 | +- Keep missing, empty, false, unobserved, skipped, unsupported, and failed states |
| 30 | + distinct where they mean different things. Never turn an unknown result into |
| 31 | + success, zero violations, or an empty dataset through a permissive default. |
| 32 | +- Types must describe accepted values, including legacy dataframe scalars. Do |
| 33 | + not claim all IDs are integers or silently coerce them unless that is the |
| 34 | + actual validated schema. |
| 35 | + |
| 36 | +## Failures and compatibility |
| 37 | + |
| 38 | +- Retain explicit assertions for internal invariants. Reject malformed external |
| 39 | + inputs with explicit validation and useful errors; assertions alone disappear |
| 40 | + under optimized Python execution. |
| 41 | +- Catch exceptions narrowly. A broad catch is justified for cleanup followed by |
| 42 | + re-raising, or an explicit failure result whose caller handles it. Do not swallow |
| 43 | + errors, continue with fabricated data, or silently fall back after corruption. |
| 44 | +- Keep compatibility fallbacks at clear boundaries. Distinguish an absent optional |
| 45 | + API from a present API with malformed metadata or unexpected runtime failure. |
| 46 | + Expose unsupported behavior and test supported and fallback paths. |
| 47 | +- Annotate context-manager exception and traceback parameters. If `__exit__` |
| 48 | + never suppresses an exception, use `Literal[False]` for its return type. |
| 49 | +- Prefer small, cohesive changes. Separate deliberate semantic changes from |
| 50 | + performance changes so reviewers can assess each independently. |
| 51 | + |
| 52 | +## Tracing, inference, and performance |
| 53 | + |
| 54 | +- Instrumentation must preserve application behavior: RNG streams, model state, |
| 55 | + return values, exceptions, and checkpoint compatibility. Test the relevant |
| 56 | + contract when changing wrappers, serialization, suppression, or sampling. |
| 57 | +- Tracer bookkeeping and serialization must not recursively generate user trace |
| 58 | + events. Check record completeness and multiplicity, including failure paths |
| 59 | + and concurrency where applicable. |
| 60 | +- Optimizations must preserve row order, dtypes, duplicates, identity, boundary |
| 61 | + conditions, and error behavior. Document cache ownership, data stability, and |
| 62 | + invalidation; test mutation or reuse when supported. A fast path needs the same |
| 63 | + semantics as the fallback. Inputs unsupported by the fast path must reach the |
| 64 | + fallback with unchanged semantics. |
| 65 | +- Compare outputs without normalizing away meaningful differences. Dropping |
| 66 | + records, IDs, or array contents to obtain equality does not establish parity. |
| 67 | +- Invariant validation must report what actually executed: activation/coverage, |
| 68 | + dormant rules, skips, failures, and violations. Zero violations alone is not |
| 69 | + evidence that the intended invariants were checked. |
| 70 | +- For performance claims, record the baseline and candidate revisions, workload, |
| 71 | + data/trace identity, configuration, environment, and reproducible commands. |
| 72 | + Use matched conditions and repeats/order controls when variability matters. |
| 73 | + Separate profiled timings from ordinary runs and component timings from total |
| 74 | + inference time. Give the denominator for percentages, memory cost, output |
| 75 | + equivalence, and material limitations. Label single-run results accordingly. |
| 76 | +- Keep large traces, datasets, checkpoints, credentials, and machine-specific |
| 77 | + scratch files out of commits. Store durable results in the agreed location and |
| 78 | + record how to find and reproduce them. Use isolated experiment directories and |
| 79 | + environments on shared machines; heavy cluster work belongs on allocated |
| 80 | + compute resources, not login nodes. |
| 81 | + |
| 82 | +## Validation |
| 83 | + |
| 84 | +- Use the repository's Python requirement in `pyproject.toml` and the tool versions |
| 85 | + and options in `.pre-commit-config.yaml` and `.github/workflows/`. Do not disable |
| 86 | + hooks, signing, assertions, or checks to make a change pass. |
| 87 | +- Run the configured checks for changed code, for example |
| 88 | + `pre-commit run --files path/to/changed.py`, and relevant CI checks before |
| 89 | + claiming readiness. Run `pre-commit run --all-files` in an isolated clean |
| 90 | + checkout when broader validation is needed. Hooks can edit files: inspect and |
| 91 | + retain only changes belonging to the task. |
| 92 | +- CI is a minimum, not complete coverage. Currently the mypy hook is scoped to |
| 93 | + `traincheck/`, untyped bodies are not fully checked, tests have lint/format |
| 94 | + exclusions, and tools-only changes may skip CI. Explicitly type-check changed |
| 95 | + reusable tools and run relevant tests even when automation does not select them. |
| 96 | +- Fully check new modules and reusable helpers; use stricter mypy options such as |
| 97 | + `--check-untyped-defs`, `--disallow-untyped-defs`, and `--disallow-any-generics` |
| 98 | + for their scope as appropriate. For legacy modules, compare with the base and |
| 99 | + introduce no new errors. Do not expand a focused task into a repository-wide |
| 100 | + strictness migration. Report any remaining errors and checker coverage limits. |
| 101 | +- Behavioral bug fixes need focused regression tests that fail before and pass |
| 102 | + after the fix. Exercise meaningful negative paths: missing required fields, wrong types, |
| 103 | + malformed records, propagated exceptions, and affected fallback behavior. |
| 104 | + Add state, RNG, cache, or concurrency tests when those contracts change. |
| 105 | +- Run existing tests relevant to affected callers and compare outputs for |
| 106 | + optimizations. Do not add tests that merely repeat implementation details. |
| 107 | + Documentation-only edits normally need content/link and diff checks, not a |
| 108 | + runtime test suite. Report exactly what ran and what could not run. |
| 109 | + |
| 110 | +## Git and review |
| 111 | + |
| 112 | +- Inspect branch, worktree status, and applicable guidance before editing. Preserve |
| 113 | + unrelated changes and shared environments. Use an isolated branch/worktree when |
| 114 | + needed; stage named files or hunks, never sweep unrelated work into a commit. |
| 115 | +- Checkpoint coherent progress with small, signed commits and focused PRs as |
| 116 | + authorized. Preserve configured signing; if signing fails, report the failure |
| 117 | + instead of silently producing an unsigned commit. Do not merge without explicit |
| 118 | + user authorization. |
| 119 | +- PR descriptions should state the problem, final approach, behavior/contracts |
| 120 | + preserved or changed, validation, and limitations. Include measured before/after |
| 121 | + numbers for performance work, with units, baseline, and end-to-end context. |
| 122 | + Do not invent improvement numbers for documentation or correctness-only work. |
| 123 | +- State dependencies between stacked PRs. Recheck the composed result after |
| 124 | + rebasing or integrating changes; a branch-local pass does not prove the final |
| 125 | + stack works. Keep experiment evidence separate from unverified expectations. |
0 commit comments