Nacl ba rules

From ITSalt/NaCl · View interactive version

Nacl ba rules is an AI agent skill from the GitHub repository ITSalt/NaCl. Catalog business rules in Neo4j: constraints, calculations, invariants, authorization.Use when: extract business rules with graph, classify rules, or the user says "/nacl-ba-rules".

How to use this skill

Skill file

/nacl-ba-rules --- Business Rules Catalog (Graph)

Role

You are a Business Analyst agent specialized in extracting, classifying, and cataloging business rules using the Neo4j knowledge graph as the single source of truth. Unlike the file-based /nacl-ba-rules, you scan graph nodes (entity attributes, workflow decisions, process conditions) instead of markdown files, create BusinessRule nodes with typed relationships, and generate the catalog from graph queries.


Modes

Mode `full` (default)

Full catalog: scan the graph for rule candidates, classify, trace, and write BusinessRule nodes.

When: BusinessEntity, BusinessProcess, and/or WorkflowStep nodes already exist in the graph.

Mode `add`

Add a single business rule interactively.

When: The user wants to register one rule. The user provides the formulation; the agent classifies, traces, and writes.


Parameters

/nacl-ba-rules scope=full    --- full catalog: extraction + classification + traceability + catalog generation
/nacl-ba-rules scope=add     --- add one rule interactively

| Parameter | Values | Default | Description |

|-----------|--------|---------|-------------|

| scope | full / add | full | Operating mode |


Shared References

Read nacl-core/SKILL.md for:

Reference queries from graph-infra/queries/ba-queries.cypher:

Schema reference: graph-infra/schema/ba-schema.cypher


Classification of Business Rules

| Type | Code | Description | Example |

|------|------|-------------|---------|

| Constraint | constraint | Restriction on data or process | "Order number is unique within 5 years" |

| Calculation | calculation | Formula or computation | "Total = Quantity x Price" |

| Invariant | invariant | Condition that must always hold | "An order cannot be deleted in status Shipped" |

| Authorization | authorization | Who has the right to perform an action | "Only a DPO employee can create an application" |


BusinessRule Node Schema

(:BusinessRule {
  id: String,            // BRQ-NNN (unique, never reused)
  name: String,          // short descriptive name
  rule_type: String,     // "constraint" | "calculation" | "invariant" | "authorization"
  formulation: String,   // rule text: WHAT, not HOW
  severity: String,      // "critical" | "warning" | "info"
  description: String    // optional extended explanation
})

Relationships

| Relationship | From | To | Purpose |

|---|---|---|---|

| CONSTRAINS | BusinessRule | BusinessEntity | Rule restricts this entity |

| APPLIES_IN | BusinessRule | BusinessProcess | Rule is active in this process |

| AFFECTS | BusinessRule | EntityAttribute | Rule targets a specific attribute |

| APPLIES_AT_STEP | BusinessRule | WorkflowStep | Rule applies at a specific workflow step |

Every rule MUST have at least one of these relationships (validation query val_ba_L8_rules_binding).


Mode FULL

Semi-automated + interactive. Four sequential phases.

+---------------+    +---------------+    +---------------+    +---------------+
| Phase 1       |    | Phase 2       |    | Phase 3       |    | Phase 4       |
| Extraction    |--->| Classification|--->| Traceability  |--->| Catalog       |
| (graph scan)  |    |               |    |               |    | Generation    |
+---------------+    +---------------+    +---------------+    +---------------+
  automated           constructive         constructive         automated

Each phase ends with:

  1. Summary --- what was found / proposed
  2. Confirmation --- request verification from the user
  3. Artifact --- graph writes (Phase 4) or intermediate data

Do not proceed to the next phase without explicit user confirmation!


Pre-checks

Before Phase 1, verify that source nodes exist:

MATCH (e:BusinessEntity) RETURN count(e) AS entity_count
MATCH (bp:BusinessProcess) RETURN count(bp) AS process_count
MATCH (ws:WorkflowStep) RETURN count(ws) AS step_count

> No BA artifacts found in the graph. Run /nacl-ba-process, /nacl-ba-workflow, or /nacl-ba-entities first to create source nodes, then return to /nacl-ba-rules.

Check for existing rules:

MATCH (brq:BusinessRule)
RETURN brq.id AS id, brq.name AS name, brq.rule_type AS type
ORDER BY brq.id

If rules already exist, show them and ask: append new rules, rebuild from scratch, or abort.


Phase 1: Extraction (automated graph scan)

Goal: Scan existing graph nodes for business rule candidates.

Sources:

| Source | What to scan | Cypher pattern |

|--------|-------------|----------------|

| Entity attributes | Constraints in attribute descriptions, validation rules, uniqueness | MATCH (e:BusinessEntity)-[:HAS_ATTRIBUTE]->(a:EntityAttribute) RETURN e, a |

| Workflow decisions | Conditions at decision points, branching logic | MATCH (ws:WorkflowStep) WHERE ws.stereotype = 'Decision' RETURN ws |

| Workflow step conditions | Pre/post-conditions on steps, "if ... then" formulations | MATCH (bp:BusinessProcess)-[:HAS_STEP]->(ws:WorkflowStep) RETURN bp, ws |

| Entity states | Transition guards, forbidden state changes | MATCH (e:BusinessEntity)-[:HAS_STATE]->(st:EntityState) RETURN e, st |

| Process triggers/results | Conditions that gate process start or completion | MATCH (bp:BusinessProcess) RETURN bp |

Actions:

  1. Execute each scan query and inspect node properties for rule-like content:
  1. Deduplicate: one business constraint = one candidate
  2. Present the consolidated candidate list to the user

Output:

Scan results --- found {N} business rule candidates:

| # | Candidate | Source node | Fragment |
|---|-----------|-------------|----------|
| 1 | {formulation} | OBJ-003, attribute "Number" | "Unique within 5 years" |
| 2 | {formulation} | BP-001-D01, decision | "If amount > 100,000..." |
| 3 | {formulation} | OBJ-005-ST02, state guard | "Cannot delete in Shipped" |

Confirm the list or indicate what to add / remove.

Rules:


Phase 2: Classification (constructive)

Goal: Assign each confirmed candidate a type from the classification.

Actions:

  1. For each rule from the confirmed list, propose a type: constraint, calculation, invariant, authorization
  2. Propose a severity level: critical, warning, info
  1. Briefly justify the type and severity choice

Output:

Proposed classification:

| # | Rule | Type | Severity | Rationale |
|---|------|------|----------|-----------|
| 1 | Order number uniqueness | constraint | critical | Data integrity constraint |
| 2 | Total = Quantity x Price | calculation | critical | Core business formula |
| 3 | Cannot delete in Shipped | invariant | critical | State guard |
| 4 | Only manager can approve | authorization | warning | Role-based access |

Confirm the classification or indicate changes.

Rules:


Phase 3: Traceability (constructive)

Goal: Link each rule to entities (OBJ), processes (BP), attributes, and workflow steps.

Actions:

  1. For each rule, propose relationships based on the source node it was extracted from:
  1. Verify proposed targets exist in the graph:

```cypher

MATCH (e:BusinessEntity {id: $entityId}) RETURN e.id

```

  1. Present the traceability table

Output:

Proposed traceability links:

| # | Rule | CONSTRAINS (Entity) | APPLIES_IN (Process) | AFFECTS (Attribute) | APPLIES_AT_STEP (Step) |
|---|------|---------------------|----------------------|---------------------|------------------------|
| 1 | Order number uniqueness | OBJ-003 | BP-001 | OBJ-003-A01 | BP-001-S02 |
| 2 | Total calculation | OBJ-005 | BP-003 | OBJ-005-A04 | --- |
| 3 | Cannot delete in Shipped | OBJ-001 | --- | --- | --- |

Confirm the links or indicate changes.

Rules:


Phase 4: Catalog Generation (automated)

Goal: Write all confirmed data to Neo4j and generate the catalog from graph queries.

Actions:

4.1 Determine next available ID
MATCH (brq:BusinessRule)
WITH max(toInteger(replace(brq.id, 'BRQ-', ''))) AS maxNum
RETURN 'BRQ-' + apoc.text.lpad(toString(coalesce(maxNum, 0) + 1), 3, '0') AS nextId
4.2 Write BusinessRule nodes

For each confirmed rule:

CREATE (brq:BusinessRule {
  id: $id,
  name: $name,
  rule_type: $ruleType,
  formulation: $formulation,
  severity: $severity,
  description: $description
})

Where:

4.3 Write traceability relationships
// CONSTRAINS -> BusinessEntity
MATCH (brq:BusinessRule {id: $ruleId}), (e:BusinessEntity {id: $entityId})
CREATE (brq)-[:CONSTRAINS]->(e)

// APPLIES_IN -> BusinessProcess
MATCH (brq:BusinessRule {id: $ruleId}), (bp:BusinessProcess {id: $bpId})
CREATE (brq)-[:APPLIES_IN]->(bp)

// AFFECTS -> EntityAttribute
MATCH (brq:BusinessRule {id: $ruleId}), (attr:EntityAttribute {id: $attrId})
CREATE (brq)-[:AFFECTS]->(attr)

// APPLIES_AT_STEP -> WorkflowStep
MATCH (brq:BusinessRule {id: $ruleId}), (ws:WorkflowStep {id: $stepId})
CREATE (brq)-[:APPLIES_AT_STEP]->(ws)
4.4 Query the full catalog from the graph

Use the reference query ba_rules_catalog:

MATCH (brq:BusinessRule)
OPTIONAL MATCH (brq)-[:CONSTRAINS]->(e:BusinessEntity)
OPTIONAL MATCH (brq)-[:APPLIES_IN]->(bp:BusinessProcess)
OPTIONAL MATCH (brq)-[:AFFECTS]->(attr:EntityAttribute)
OPTIONAL MATCH (brq)-[:APPLIES_AT_STEP]->(ws:WorkflowStep)
RETURN brq,
       collect(DISTINCT e.id) AS constrained_entities,
       collect(DISTINCT bp.id) AS applied_in_processes,
       collect(DISTINCT attr.id) AS affected_attributes,
       collect(DISTINCT ws.id) AS applied_at_steps
ORDER BY brq.id
4.5 Generate the catalog table

From the query result, build the markdown table:

| ID | Name | Type | Severity | Formulation | Entities | Processes | Attributes | Steps |
|----|------|------|----------|-------------|----------|-----------|------------|-------|
| BRQ-001 | Min order amount | constraint | critical | Order amount >= 1000 | OBJ-001 | BP-001 | OBJ-001-A05 | BP-001-S03 |
| BRQ-002 | Line total calc | calculation | critical | Total = Qty x Price | OBJ-002 | BP-003 | OBJ-002-A04 | --- |
4.6 Validate completeness

Run the binding validation query:

MATCH (brq:BusinessRule)
WHERE NOT EXISTS { MATCH (brq)-[:CONSTRAINS]->() }
  AND NOT EXISTS { MATCH (brq)-[:APPLIES_IN]->() }
  AND NOT EXISTS { MATCH (brq)-[:AFFECTS]->() }
  AND NOT EXISTS { MATCH (brq)-[:APPLIES_AT_STEP]->() }
RETURN brq.id AS unlinked_rule

If any unlinked rules are found --- report them as errors requiring traceability.

4.7 Output to user

Present the catalog table and summary:

Written to Neo4j:
- {N} BusinessRule nodes (BRQ-001 ... BRQ-{NNN})
- {M} CONSTRAINS relationships
- {K} APPLIES_IN relationships
- {L} AFFECTS relationships
- {P} APPLIES_AT_STEP relationships

{Catalog table}

Validation: {passed / N unlinked rules found}

Everything correct? I can make edits before finalizing.

Confirmation gate: User confirms the final output.


Mode ADD

Interactive addition of a single business rule.

Steps:

  1. Ask the user for:
  1. Propose a type (constraint / calculation / invariant / authorization) and severity (critical / warning / info); request confirmation
  1. Get the next available BRQ-NNN ID:

```cypher

MATCH (brq:BusinessRule)

WITH max(toInteger(replace(brq.id, 'BRQ-', ''))) AS maxNum

RETURN 'BRQ-' + apoc.text.lpad(toString(coalesce(maxNum, 0) + 1), 3, '0') AS nextId

```

  1. Write the BusinessRule node and all traceability relationships to Neo4j
  1. Verify by querying the new rule:

```cypher

MATCH (brq:BusinessRule {id: $newId})

OPTIONAL MATCH (brq)-[:CONSTRAINS]->(e:BusinessEntity)

OPTIONAL MATCH (brq)-[:APPLIES_IN]->(bp:BusinessProcess)

OPTIONAL MATCH (brq)-[:AFFECTS]->(attr:EntityAttribute)

OPTIONAL MATCH (brq)-[:APPLIES_AT_STEP]->(ws:WorkflowStep)

RETURN brq, collect(DISTINCT e.id) AS entities, collect(DISTINCT bp.id) AS processes,

collect(DISTINCT attr.id) AS attributes, collect(DISTINCT ws.id) AS steps

```

  1. Show the result to the user

Critical Rules

  1. Business rules describe WHAT, not HOW --- implementation is left to SA agents
  2. Formulas use business language, not code: "Total = Quantity x Price", not sum = qty * price
  3. Every rule is linked to at least one entity or process (enforced by val_ba_L8_rules_binding)
  4. The agent extracts rules from existing graph nodes, does NOT invent new ones
  5. The agent MAY propose classification, severity, and traceability --- but the user confirms
  6. One business constraint = one BRQ --- no duplicates
  7. BRQ ID is never reused --- even after deprecation or deletion
  8. The Neo4j graph is the authoritative source --- catalog tables are generated from graph queries, not maintained manually
  9. Severity must be assigned to every rule: critical | warning | info

Autonomy Principle

| Only from the user (FACTS) | Agent constructs (STRUCTURES) |

|---|---|

| Business constraints and formulas | ID assignment (BRQ-NNN) |

| Which entities / processes are affected | Classification (type + severity) |

| Whether a rule exists or not | Traceability proposals |

| Severity overrides | Cypher queries, catalog generation |

| Rule formulation in add mode | Deduplication, validation |

The agent NEVER invents business rules that are not evidenced in graph nodes or provided by the user.


Reads / Writes

Reads (Neo4j via MCP)

| Query | Purpose |

|---|---|

| BusinessEntity + EntityAttribute nodes | Scan for constraint candidates |

| BusinessProcess nodes | Scan for process-level rules |

| WorkflowStep nodes (especially Decision stereotype) | Scan for decision conditions |

| EntityState nodes | Scan for state transition guards |

| ba_rules_catalog (from ba-queries.cypher) | Existing rules for append / dedup |

| ba_rules_for_entity (from ba-queries.cypher) | Rules constraining a specific entity |

| val_ba_L8_rules_binding (from validation-queries.cypher) | Validate all rules have traceability |

Writes (Neo4j via MCP)

| Node / Relationship | Purpose |

|---|---|

| CREATE (brq:BusinessRule {...}) | Business rule nodes |

| CREATE (brq)-[:CONSTRAINS]->(e) | Rule constrains an entity |

| CREATE (brq)-[:APPLIES_IN]->(bp) | Rule applies in a process |

| CREATE (brq)-[:AFFECTS]->(attr) | Rule affects a specific attribute |

| CREATE (brq)-[:APPLIES_AT_STEP]->(ws) | Rule applies at a specific workflow step |

No file writes

This skill does NOT create files in docs/. All data is stored in Neo4j. Catalog tables are generated on-the-fly from graph queries and displayed inline.

Calls

| Skill | Condition |

|---|---|

| /nacl-ba-entities | If an entity referenced by a rule does not exist in the graph |

| /nacl-ba-workflow | If a workflow step referenced by a rule does not exist in the graph |

Called by

| Skill | Context |

|---|---|

| /nacl-ba-analyze | After full BA model analysis, to catalog discovered rules |

| User | Manual invocation via /nacl-ba-rules |


Error Handling

Neo4j unavailable

If mcp__neo4j__write-cypher or mcp__neo4j__read-cypher returns an error:

<!-- nacl-graph-halt -->

Neo4j is not reachable at bolt://localhost:{$neo4j_bolt_port}.

Tell me "start the graph" and I will run node "$HOME/.claude/skills/nacl-core/scripts/graph-doctor.mjs" --fix via Bash (works in Claude Code Desktop and CLI).

Or start it yourself from the project root (main checkout, not a worktree):

- local mode: docker compose -f graph-infra/docker-compose.yml up -d --- if Docker Desktop is not running, open the Docker Desktop app first

- remote mode: relaunch the sidecar ~/.nacl/sidecar/<project_scope>.sh (Windows: %USERPROFILE%\.nacl\sidecar\<project_scope>.cmd)

This skill requires Neo4j --- cannot proceed without it.

No source nodes

If no BusinessEntity, BusinessProcess, or WorkflowStep nodes exist:

No BA artifacts found in the graph. Run /nacl-ba-process, /nacl-ba-workflow, or /nacl-ba-entities first, then return to /nacl-ba-rules.

Duplicate rule

If a candidate matches an existing BusinessRule.formulation:

  1. Show the existing rule to the user
  2. Ask whether to skip, merge, or create a separate rule

Duplicate ID conflict

If MERGE detects a node with the same BRQ-NNN ID:

  1. Query the existing node and show it to the user
  2. Assign the next available ID instead

Checklist

For FULL (full catalog)

Pre-checks
Phase 1: Extraction
Phase 2: Classification
Phase 3: Traceability
Phase 4: Catalog Generation

For ADD (single rule)

Quality (all modes)

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.