requirements-manager 0.1.1

Plain-text requirements management tool
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# Cycle Detection

> **Note**: Cycle detection is **planned but not yet implemented**. This chapter describes how the feature will work when available.

Cycle detection identifies circular dependencies in requirement graphs, which are usually errors that break valid traceability hierarchies.

## What are Cycles?

A cycle occurs when requirements form a circular dependency chain:

```
USR-001 → SYS-001 → SYS-002 → USR-001
```

Requirements form a loop: USR-001 depends on SYS-001, which depends on SYS-002, which depends back on USR-001.

### Why Cycles are Problematic

**Break hierarchy assumptions**:
- Requirements should form a Directed Acyclic Graph (DAG)
- Cycles violate DAG properties

**Logical impossibility**:
- A cannot be satisfied until B is satisfied
- B cannot be satisfied until C is satisfied
- C cannot be satisfied until A is satisfied
- Nothing can be satisfied!

**Review complexity**:
- Changes propagate indefinitely
- Impossible to determine impact

**Compliance issues**:
- Many standards require acyclic traceability
- Auditors expect clear hierarchies

## Example Cycles

### Simple Cycle (2 requirements)

```
USR-001 → SYS-001 → USR-001
```

**Why it happens**: Accidental bi-directional linking.

**Detection**: SYS-001 lists USR-001 as parent; USR-001 lists SYS-001 as parent.

### Complex Cycle (3+ requirements)

```
USR-001 → SYS-001 → SYS-002 → SWR-001 → USR-001
```

**Why it happens**: Accumulation of links over time without oversight.

**Detection**: Following parent chain eventually returns to starting requirement.

### Self-Reference

```
USR-001 → USR-001
```

**Why it happens**: Data entry error.

**Detection**: Requirement lists itself as a parent.

## Planned Functionality

### Cycle Detection Command

```bash
req check-cycles
```

**Output** (if cycles found):
```
Error: Cycles detected in requirement graph

Cycle 1 (length 3):
  USR-001 → SYS-001 → SYS-002 → USR-001

Cycle 2 (length 2):
  SYS-005 → SWR-003 → SYS-005

Cycle 3 (self-reference):
  SWR-012 → SWR-012

Total cycles: 3

Fix these cycles to ensure valid traceability.
```

**Output** (no cycles):
```
No cycles detected. Requirement graph is acyclic.
```

### Detailed Cycle Information

```bash
req check-cycles --detailed
```

**Output**:
```
Cycle 1:
  Path: USR-001 → SYS-001 → SYS-002 → USR-001
  Length: 3
  Requirements involved:
    - USR-001: User authentication
      Parent: SYS-002 (creates cycle)
    - SYS-001: Authentication service
      Parent: USR-001
    - SYS-002: Session management
      Parent: SYS-001

  Suggested fix:
    Remove parent link from USR-001 to SYS-002
    (User requirements should not depend on system requirements)
```

### Visualization

Generate cycle diagrams:

```bash
req check-cycles --visualize > cycles.dot
dot -Tpng cycles.dot -o cycles.png
```

**Output**: Graph highlighting cyclic paths in red.

## How Detection Works

### Algorithm: Depth-First Search

Requiem will use DFS with cycle detection:

1. **Start from each requirement**
2. **Follow parent links** (or child links for reverse traversal)
3. **Track visited requirements** in current path
4. **If revisit a requirement in current path**: Cycle detected
5. **Report cycle**: Full path from requirement back to itself

### Example Walkthrough

Requirements:
```
USR-001 → SYS-001
SYS-001 → SYS-002
SYS-002 → USR-001  (creates cycle)
```

**Detection**:
```
1. Start at USR-001
2. Visit SYS-001 (parent of USR-001)
3. Visit SYS-002 (parent of SYS-001)
4. Visit USR-001 (parent of SYS-002)
5. USR-001 is already in path → Cycle detected!
6. Report: USR-001 → SYS-001 → SYS-002 → USR-001
```

### Performance

- **Time complexity**: O(V + E) where V = requirements, E = links
- **Expected runtime**: Milliseconds for 1000s of requirements
- **Parallel detection**: Possible for disconnected subgraphs

## Use Cases

### Use Case 1: Continuous Validation

**Scenario**: Automatically check for cycles in CI.

**Workflow**:

```yaml
# .github/workflows/requirements.yml (planned)
- name: Check for cycles
  run: |
    req check-cycles
    if [ $? -ne 0 ]; then
      echo "Cycles detected!"
      req check-cycles --detailed
      exit 1
    fi
```

**Benefit**: Cycles are caught before merging.

### Use Case 2: Fixing Legacy Requirements

**Scenario**: Inherited a requirement set with unknown quality.

**Goal**: Find and fix all cycles.

**Workflow**:

```bash
# Detect cycles
req check-cycles --detailed

# Output:
# Cycle 1: USR-001 → SYS-001 → SYS-002 → USR-001
# Suggested fix: Remove link from USR-001 to SYS-002

# Fix cycle
vim USR-001.md  # Remove SYS-002 from parents

# Verify
req check-cycles
# No cycles detected
```

### Use Case 3: Incremental Checking

**Scenario**: Adding a new link between requirements.

**Goal**: Ensure the new link doesn't create a cycle.

**Workflow**:

```bash
# Add link
req link SWR-001 SYS-005

# Check if cycle created
req check-cycles

# If cycle:
# Error: Cycle detected: SYS-005 → ... → SWR-001 → SYS-005

# If no cycle:
# No cycles detected
```

**Benefit**: Immediate feedback; don't commit cycle-inducing links.

## Breaking Cycles

### Strategy 1: Remove Link

Identify the "weakest" link in the cycle and remove it:

```
USR-001 → SYS-001 → SYS-002 → USR-001
                               ^^^^^^^
                        Remove this link
```

**Criteria for "weakest"**:
- Violates hierarchy (e.g., user requirement depending on system requirement)
- Accidental or incorrect
- Least impactful to remove

### Strategy 2: Reverse Hierarchy

If requirements are at wrong levels, reassign:

```
Before (cycle):
  USR-001 → SYS-001
  SYS-001 → USR-002
  USR-002 → USR-001

After (fixed):
  USR-001, USR-002 → SYS-001
  (SYS-001 now has both as parents; no cycle)
```

### Strategy 3: Introduce Intermediate Requirement

Break cycle by adding a requirement:

```
Before (cycle):
  USR-001 → SYS-001 → USR-001

After (fixed):
  USR-001 → SYS-001 → SYS-002
  (SYS-002 is new, breaks cycle)
```

### Strategy 4: Merge Requirements

If cycle indicates redundancy, merge:

```
Before (cycle):
  REQ-A → REQ-B → REQ-A

After (merged):
  REQ-AB (combines A and B; no cycle)
```

## Configuration (Planned)

### Enable/Disable Cycle Checking

```toml
# config.toml (planned)
[cycles]
# Enable cycle detection
enabled = true

# Fail on cycle detection (exit with error)
fail_on_cycle = true

# Report self-references separately
detect_self_references = true
```

### CI Integration

```toml
[cycles.ci]
# Run cycle detection in CI
enabled = true

# Block merge if cycles detected
block_on_cycle = true
```

## Exceptions (Planned)

In rare cases, cycles might be intentional. Allow marking exceptions:

```yaml
# USR-001.md (hypothetical)
---
_version: '1'
uuid: ...
parents:
- uuid: ...
  hrid: SYS-002
  cycle_exception: true  # Mark as intentional cycle
---
```

Or in config:

```toml
[cycles.exceptions]
# Allow specific cycles
allowed = [
    ["USR-001", "SYS-001", "USR-001"],
]
```

**Use sparingly**: Cycles are almost always errors.

## Related Features

### Topological Sort

After ensuring no cycles, enable topological sorting:

```bash
req sort --topological
```

**Output**: Requirements in dependency order (parents before children).

**Use case**: Determine implementation order.

### Dependency Depth

Analyze requirement depth in hierarchy:

```bash
req depth USR-001
```

**Output**:
```
USR-001:
  Depth: 0 (root requirement)
  Max descendant depth: 4

  Path to deepest descendant:
    USR-001 → SYS-001 → SWR-003 → TST-012
```

## Workarounds (Until Implemented)

Manual cycle detection:

### Script to Detect Simple Cycles

```python
#!/usr/bin/env python3
import glob
import yaml
import re

def parse_requirement(path):
    with open(path) as f:
        content = f.read()
    match = re.match(r'^---\n(.*?)\n---', content, re.DOTALL)
    if match:
        frontmatter = yaml.safe_load(match.group(1))
        return frontmatter
    return None

def find_cycles():
    # Build graph
    graph = {}
    for req_file in glob.glob("*.md"):
        hrid = req_file.replace('.md', '')
        frontmatter = parse_requirement(req_file)
        if frontmatter:
            parents = frontmatter.get('parents', [])
            parent_hrids = [p['hrid'] for p in parents]
            graph[hrid] = parent_hrids

    # Detect cycles with DFS
    def visit(req, path):
        if req in path:
            cycle = path[path.index(req):] + [req]
            print(f"Cycle detected: {' → '.join(cycle)}")
            return True
        if req not in graph:
            return False
        for parent in graph[req]:
            if visit(parent, path + [req]):
                return True
        return False

    for req in graph:
        visit(req, [])

if __name__ == '__main__':
    find_cycles()
```

Run:
```bash
python detect-cycles.py
```

### Manual Inspection

For small requirement sets, manually trace parent chains:

1. Pick a requirement (e.g., USR-001)
2. Follow parent links
3. Track visited requirements
4. If you return to the starting requirement, cycle exists

## Summary

**Key concepts**:

- **Cycle**: Circular dependency chain in requirement graph
- **Problem**: Breaks hierarchy, causes logical impossibility
- **Detection**: Depth-first search with path tracking
- **Resolution**: Remove links, reverse hierarchy, introduce intermediate requirements

**Planned functionality**:
- Automatic cycle detection
- Detailed cycle reports with suggested fixes
- Visualization of cyclic paths
- CI/CD integration
- Configurable checking

**Use cases**:
- CI validation
- Legacy requirement cleanup
- Incremental validation

**Timeline**: Implementation planned for future release

## Next Steps

- Use [workarounds]#workarounds-until-implemented for manual cycle detection
- Plan your [cycle detection requirements]#configuration-planned for when feature is available
- See [Coverage Reports]./coverage.md for traceability analysis