---
name: ba-audit-cross-ref-code
description: >
  Cross-references the BA data model of a module against the EXISTING project
  code — detects entities that extend an existing class, reference a Core/existing
  entity as FK, or duplicate one (CODE-001..004). Reads the module's `entité.md`,
  scans `src/*.Domain/**/*.cs`, writes a verdict to `_audit/cross-ref-code.md`.
  Run after `/ba-create-data-model` or as part of pre-dev readiness.
allowed-tools: [Read, Write, Glob, Grep, Bash]  # Bash: scan existing .cs code files
---

# ba-audit-cross-ref-code — BA data model × existing code cross-reference

You cross-reference the conceptual data model of one module (its `entité.md`)
against the **existing source code** of the project, then write a verdict file.
The rules are unchanged from the SmartStack convention; only the I/O is
file-based and the code scan now uses Grep/Bash on the project tree.

## File model — how to audit

1. **Read the BA state**: `Glob .smartstack/ba/**/index.md` to locate the module.
   Read the module's `entité.md` (authoritative at Module level) for its entities
   (`### ENT-NNN — <Name>`), their **table prefix** and their **Relations** (FK
   targets). Read sibling-module `entité.md` files only to know which FK targets
   are already declared in BA (vs. expected from code).
2. **Scan the existing code**: the audited project is the working directory. Find
   domain classes with `Glob src/*.Domain/**/*.cs` (also try `**/Entities/**/*.cs`,
   `**/Domain/**/*.cs`). Use Grep for `class <Name>` and `ToTable("<prefix>` to
   resolve each rule. A `Bash` ripgrep/dotnet probe is allowed when a broader sweep
   is faster — keep the findings here, not the raw scan.
3. **Apply** the rules CODE-001..004 (below) to the module's entities.
4. **Write** the verdict to the module's `_audit/cross-ref-code.md`:
   `.smartstack/ba/<APP>/<MODULE>/_audit/cross-ref-code.md` (use the audit-verdict
   skeleton from `_workflow/doc-templates.md`). Then print a 3–6 line business
   summary in chat.

There is no database, no `[ACTION]` envelope, no i18n label codes — the `.md`
verdict IS the record. The verdict header (`0 err` = pass) is what a downstream
gate reads.

## Pre-check — greenfield project

Before applying any rule, check whether the project has existing domain code:

- `Glob src/*.Domain/**/*.cs` (then `**/Entities/**/*.cs`, `**/Domain/**/*.cs`).
- If **no** entity files are found, this is a greenfield project: there is nothing
  to cross-reference. Emit a single `ok` finding (CODE-001 — no existing code, the
  audit passes trivially) and skip CODE-002..004. Say so in the chat summary.

## Scope

- **Module scope** (the only scope): audit one module — apply CODE-001..004 to
  every entity in that module's `entité.md`. Don't scan sibling-module BA models
  except to know which FK targets are already BA-declared.
- The verdict file lives at the audited module's `_audit/cross-ref-code.md`.

## Rules

### CODE-001 — Entity already exists in code
- **Severity**: `warn` (a BA entity name matches an existing code class), else `ok`.
- Check: for each BA data model entity, Grep for `class {EntityName}` in the `.cs`
  files. If found, the BA entity is an **extension** of existing code, not a brand
  new entity.
- `warn`: state that **`<EntityName>`** already exists at `<path>`. Why it matters:
  the development phase should EXTEND this entity (add properties) rather than
  create a duplicate one — the data model must mark it as an extension.
- → Fix: `/ba-create-data-model` (declare `<EntityName>` as an extension of the
  existing class).

### CODE-002 — Entity references existing entity as FK
- **Severity**: `ok` (informational — confirms the FK target exists in code).
- Check: for each BA relationship whose FK target points to an entity **not** in
  the current module's BA data model, Grep for `class {targetName}` in the `.cs`
  files. If found, the FK target is a valid reference into existing/Core code.
- `ok`: note **`<EntityName>`** → **`<targetName>`** resolves to the existing class
  at `<path>` (a legitimate cross-reference, no action needed).

### CODE-003 — FK target not found in BA or code
- **Severity**: `err` (the FK target entity is not found anywhere).
- Check: for each BA relationship, if the FK target is **not** in the module's BA
  data model **and not** found via Grep in the `.cs` files, the FK is broken.
- `err`: state that the relationship target **`<targetName>`** (referenced by
  **`<EntityName>`**) is not found in the BA data model or in existing code. Why
  it matters: a dangling FK blocks entity/migration generation downstream.
- → Fix: `/ba-create-data-model` (either add the target entity to the data model
  or correct the target name).

### CODE-004 — Duplicate table prefix
- **Severity**: `warn` (a BA entity table prefix collides with existing code), else `ok`.
- Check: for each BA entity that declares a table prefix, Grep for
  `ToTable("{prefix}` in the `.cs` files (EF Core configurations). If found, there
  is a table-name collision risk.
- `warn`: state that the prefix **`<prefix>`** (used by **`<EntityName>`**) is
  already used in `<conflictFile>`. Why it matters: two entities mapping to the
  same table prefix collide at the database level.
- → Fix: `/ba-create-data-model` (choose a distinct table prefix for
  `<EntityName>`).

## Output

Write `_audit/cross-ref-code.md` per the doc-templates skeleton:
- Header `# Audit cross-ref-code — <APP> / <MODULE>` + `_<date> · Verdict :
  <emoji> N warn · M err · K ok_`.
- `## ✅ Conforme`, `## ⚠️ Avertissements`, `## ❌ Bloquants` sections; one bullet
  per finding. For `warn`/`err`: what's wrong (offending entity/prefix codes
  **bold**), why it matters, and a `→` fix naming `/ba-create-data-model`.
- Re-Write the whole file each run (overwrite — it's a fresh verdict).

Then a 3–6 line chat summary in the user's language — business terms, not rule
codes. State whether the data model lines up with the existing code (extensions
correctly flagged, FK targets resolved) and call out any `err` (a broken FK that
must be fixed before development). If the project is greenfield (no existing
code), say so and that the audit passes trivially.

## Used by the readiness orchestrator

`/ba-audit-pre-dev` runs every dimension and aggregates the verdicts. When invoked
by it, still write `_audit/cross-ref-code.md` as usual — the orchestrator reads
these files.
