safeword 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/dist/{check-J6DFVBCE.js → check-US6EQLNS.js} +3 -3
  2. package/dist/check-US6EQLNS.js.map +1 -0
  3. package/dist/chunk-2XWIUEQK.js +190 -0
  4. package/dist/chunk-2XWIUEQK.js.map +1 -0
  5. package/dist/{chunk-UQMQ64CB.js → chunk-GZRQL3SX.js} +41 -2
  6. package/dist/chunk-GZRQL3SX.js.map +1 -0
  7. package/dist/chunk-Z2SOGTNJ.js +7 -0
  8. package/dist/{chunk-WWQ4YRZN.js.map → chunk-Z2SOGTNJ.js.map} +1 -1
  9. package/dist/cli.js +6 -6
  10. package/dist/{diff-U4IELWRL.js → diff-72ZUEZ6A.js} +32 -29
  11. package/dist/diff-72ZUEZ6A.js.map +1 -0
  12. package/dist/index.d.ts +1 -1
  13. package/dist/index.js +1 -1
  14. package/dist/{reset-XETOHTCK.js → reset-3ACTIYYE.js} +44 -27
  15. package/dist/reset-3ACTIYYE.js.map +1 -0
  16. package/dist/{setup-CLDCHROZ.js → setup-TSFCHD2D.js} +77 -48
  17. package/dist/setup-TSFCHD2D.js.map +1 -0
  18. package/dist/{upgrade-DOKWRK7J.js → upgrade-XDPQFSMC.js} +38 -50
  19. package/dist/upgrade-XDPQFSMC.js.map +1 -0
  20. package/package.json +1 -1
  21. package/templates/SAFEWORD.md +776 -0
  22. package/templates/commands/arch-review.md +24 -0
  23. package/templates/commands/lint.md +11 -0
  24. package/templates/commands/quality-review.md +23 -0
  25. package/templates/doc-templates/architecture-template.md +136 -0
  26. package/templates/doc-templates/design-doc-template.md +134 -0
  27. package/templates/doc-templates/test-definitions-feature.md +131 -0
  28. package/templates/doc-templates/user-stories-template.md +92 -0
  29. package/templates/guides/architecture-guide.md +423 -0
  30. package/templates/guides/code-philosophy.md +195 -0
  31. package/templates/guides/context-files-guide.md +457 -0
  32. package/templates/guides/data-architecture-guide.md +200 -0
  33. package/templates/guides/design-doc-guide.md +171 -0
  34. package/templates/guides/learning-extraction.md +552 -0
  35. package/templates/guides/llm-instruction-design.md +248 -0
  36. package/templates/guides/llm-prompting.md +102 -0
  37. package/templates/guides/tdd-best-practices.md +615 -0
  38. package/templates/guides/test-definitions-guide.md +334 -0
  39. package/templates/guides/testing-methodology.md +618 -0
  40. package/templates/guides/user-story-guide.md +256 -0
  41. package/templates/guides/zombie-process-cleanup.md +219 -0
  42. package/templates/hooks/agents-md-check.sh +27 -0
  43. package/templates/hooks/inject-timestamp.sh +2 -3
  44. package/templates/hooks/post-tool.sh +4 -0
  45. package/templates/hooks/pre-commit.sh +10 -0
  46. package/templates/lib/common.sh +26 -0
  47. package/templates/lib/jq-fallback.sh +20 -0
  48. package/templates/markdownlint.jsonc +25 -0
  49. package/templates/prompts/arch-review.md +43 -0
  50. package/templates/prompts/quality-review.md +10 -0
  51. package/templates/skills/safeword-quality-reviewer/SKILL.md +207 -0
  52. package/dist/check-J6DFVBCE.js.map +0 -1
  53. package/dist/chunk-24OB57NJ.js +0 -78
  54. package/dist/chunk-24OB57NJ.js.map +0 -1
  55. package/dist/chunk-DB4CMUFD.js +0 -157
  56. package/dist/chunk-DB4CMUFD.js.map +0 -1
  57. package/dist/chunk-UQMQ64CB.js.map +0 -1
  58. package/dist/chunk-WWQ4YRZN.js +0 -7
  59. package/dist/diff-U4IELWRL.js.map +0 -1
  60. package/dist/reset-XETOHTCK.js.map +0 -1
  61. package/dist/setup-CLDCHROZ.js.map +0 -1
  62. package/dist/upgrade-DOKWRK7J.js.map +0 -1
@@ -0,0 +1,457 @@
1
+ # Project Context Files (CLAUDE.md, CURSOR.md, AGENTS.md)
2
+
3
+ ## When to Create Context Files
4
+
5
+ Create the file(s) that best match your tooling. It’s okay to have more than one if different tools are used.
6
+
7
+ - `CLAUDE.md` (Claude-specific)
8
+ - Claude/Desktop or Claude Code-specific guidance (hooks, skills, MCP gateways)
9
+ - Prompts, behaviors, and non-obvious conventions for Claude
10
+ - `CURSOR.md` (Cursor-specific)
11
+ - Editor/extension conventions, quick commands, keybindings, gotchas for Cursor
12
+ - `AGENTS.md` (tool-agnostic)
13
+ - Generic “project context” consumed by multiple agents/editors
14
+
15
+ All project context files should:
16
+
17
+ - **CRITICAL:** Reference `.safeword/SAFEWORD.md` at the top
18
+ - Architecture decisions with "why" (not just "what")
19
+ - Design philosophy and conventions
20
+ - Common gotchas specific to this codebase
21
+ - Cross-references to subdirectory files
22
+
23
+ **Subdirectory files** (e.g., `tests/AGENTS.md`, `packages/app/CLAUDE.md`) - Create when:
24
+
25
+ - > 3 unique rules that don't fit in root
26
+ - Working in that directory needs specialized context
27
+ - **Skip if:** Directory is straightforward or already covered in root
28
+
29
+ **Naming convention summary:**
30
+
31
+ - `CLAUDE.md` for Claude-specific guidance
32
+ - `CURSOR.md` for Cursor-specific guidance
33
+ - `AGENTS.md` for cross-agent compatibility (tests, docs, most projects)
34
+
35
+ ## SAFEWORD Trigger (Required)
36
+
37
+ **MANDATORY:** Every project-level context file (CLAUDE.md, CURSOR.md, AGENTS.md) must start with a trigger to SAFEWORD:
38
+
39
+ ```markdown
40
+ # Project Name - Developer Context
41
+
42
+ **⚠️ ALWAYS READ FIRST: @./.safeword/SAFEWORD.md**
43
+
44
+ The SAFEWORD.md file contains core development patterns, workflows, and conventions.
45
+ Read it BEFORE working on any task in this project.
46
+
47
+ ---
48
+
49
+ [Rest of project-specific content...]
50
+ ```
51
+
52
+ **Why:** `.safeword/SAFEWORD.md` contains universal workflows (TDD, feature development, etc.) that apply across all projects. Project files should only contain project-specific context.
53
+
54
+ ## Agent Context Auto-Loading Behavior
55
+
56
+ **How Context Loading Works:**
57
+
58
+ 1. Tools typically load a root context file (CLAUDE.md, CURSOR.md, AGENTS.md)
59
+ 2. Subdirectory context files may load when working inside those dirs
60
+ 3. Hierarchical loading (root + subdirectory) is common
61
+
62
+ **Example:**
63
+
64
+ ```
65
+ Working in: /project/src/feature/
66
+ Loaded context:
67
+ ✓ /project/CLAUDE.md (Claude-specific guidance)
68
+ ✓ /project/src/feature/AGENTS.md (feature-specific rules)
69
+ ```
70
+
71
+ **Design implication:** Subdirectory files should assume root context is available
72
+
73
+ - Use "See root SAFEWORD.md or AGENTS.md for architecture" cross-references
74
+ - Don't duplicate root content in subdirectory files
75
+ - Focus subdirectory files on specialized conventions for that area
76
+
77
+ ```markdown
78
+ ❌ BAD - tests/AGENTS.md duplicates root TDD workflow:
79
+
80
+ ## TDD Workflow
81
+
82
+ 1. Write failing tests first (RED)
83
+ 2. Implement minimum code (GREEN)
84
+ 3. Refactor while green
85
+
86
+ ✅ GOOD - tests/AGENTS.md references root:
87
+
88
+ ## Testing Conventions
89
+
90
+ See root AGENTS.md for TDD workflow. This file covers test-specific patterns.
91
+ ```
92
+
93
+ **Reliability note:** Auto-loading works best when you explicitly reference your file in conversation (e.g., "following the guidelines in CLAUDE.md"). Implicit automatic reference can be less reliable.
94
+
95
+ **Deprecated:** `*.local.md` is no longer recommended - use imports instead for better multi-worktree support
96
+
97
+ ## File Structure Pattern
98
+
99
+ ```
100
+ project/
101
+ ├─ SAFEWORD.md # Project context (references guides)
102
+ ├─ CLAUDE.md # Claude-specific context
103
+ ├─ CURSOR.md # Cursor-specific context (optional)
104
+ └─ tests/AGENTS.md # Test conventions (cross-agent)
105
+ ```
106
+
107
+ **Modular Approach (Recommended):**
108
+
109
+ ```
110
+ project/
111
+ ├─ AGENTS.md / CLAUDE.md # 50 lines: imports + structure
112
+ ├─ docs/architecture.md # 100 lines: architecture decisions
113
+ └─ docs/conventions.md # 80 lines: coding conventions
114
+ ```
115
+
116
+ Main context file imports modules:
117
+
118
+ ```markdown
119
+ @docs/architecture.md
120
+ @docs/conventions.md
121
+ ```
122
+
123
+ ## Content Guidelines
124
+
125
+ **Include:**
126
+
127
+ - "Why" over "what" - Explain architectural trade-offs, not features
128
+ - Project-specific conventions - Unique to THIS codebase
129
+ - Domain requirements - Specialized knowledge (game mechanics, industry standards, UX patterns)
130
+ - Concrete examples - Good vs bad patterns
131
+ - Gotchas - Common mistakes developers make HERE
132
+ - Cross-references - Link to subdirectories, don't duplicate
133
+
134
+ **Exclude:**
135
+
136
+ - Generic advice ("follow best practices")
137
+ - Feature lists (put in README.md)
138
+ - Setup instructions (put in README.md)
139
+ - Phase tracking (put in ROADMAP.md)
140
+ - API documentation (put in code comments)
141
+
142
+ ## Target Line Counts
143
+
144
+ - Root: 100-200 lines (architecture + philosophy)
145
+ - Subdirectories: 60-100 lines (focused conventions)
146
+ - Total project: <500 lines across all files
147
+ - **With imports:** Main file 50 lines, modules 100-150 lines each
148
+
149
+ **Rule:** If >200 lines, extract to subdirectory or use imports.
150
+
151
+ **File size:** Keep under 50KB for optimal performance (though no hard limit)
152
+
153
+ ## Anti-Patterns to Avoid
154
+
155
+ ❌ **Redundancy between root and subdirectory files** (#1 source of bloat)
156
+
157
+ - Don't list all stores in root if packages/web/AGENTS.md covers them
158
+ - Don't document testing patterns in root if tests/AGENTS.md exists
159
+ - Don't repeat gotchas - reference subdirectory for details
160
+ - Each fact stated exactly once, use cross-references elsewhere
161
+
162
+ ❌ **Implementation details in root file**
163
+
164
+ - File paths (store.ts:127-137) belong in subdirectory files
165
+ - Specific line numbers change frequently
166
+ - File trees and directory structures
167
+ - Line counts and feature status lists
168
+
169
+ ❌ **Testing sections in non-test files**
170
+
171
+ - Testing philosophy → tests/AGENTS.md (always)
172
+ - Test commands → tests/AGENTS.md or README.md
173
+ - Test patterns → tests/AGENTS.md
174
+
175
+ ❌ **User-facing documentation**
176
+
177
+ - Setup instructions → README.md
178
+ - Development commands → README.md
179
+ - Feature lists → ROADMAP.md
180
+ - API documentation → Code comments or separate docs
181
+
182
+ ❌ **Generic advice**
183
+
184
+ - "Use TypeScript" (not project-specific)
185
+ - "Follow best practices" (too vague)
186
+ - "Write tests" (duh - say WHICH tests for WHAT)
187
+
188
+ ❌ **Meta-commentary**
189
+
190
+ - "Last Updated: 2025-01-19"
191
+ - "This file was reduced from 634 → 152 lines"
192
+ - Commit history (that's what git is for)
193
+
194
+ ❌ **Outdated information**
195
+
196
+ - Revisit after major architectural changes
197
+ - Remove sections when they no longer apply
198
+ - Update cross-references when files move
199
+
200
+ ## Cross-Reference Pattern
201
+
202
+ **Root file:**
203
+
204
+ ```markdown
205
+ **Agents** (`app/src/agents/`) - LLM logic. See `/AGENTS.md`.
206
+ ```
207
+
208
+ **Subdirectory file:**
209
+
210
+ ```markdown
211
+ **Context:** Working with AI agents. See root `SAFEWORD.md`/`AGENTS.md` for architecture.
212
+ ```
213
+
214
+ **Import pattern:**
215
+
216
+ ```markdown
217
+ # Project Context
218
+
219
+ See @README for project overview and @package.json for available npm commands.
220
+
221
+ ## Architecture
222
+
223
+ @docs/architecture.md
224
+
225
+ ## Coding Standards
226
+
227
+ @.safeword/guides/llm-prompting.md
228
+
229
+ ## Git Workflow
230
+
231
+ Details in @docs/git-workflow.md
232
+ ```
233
+
234
+ **Import features:**
235
+
236
+ - **Relative paths:** `@docs/file.md` (relative to AGENTS.md location)
237
+ - **Absolute paths:** `@/path/to/file.md`
238
+ - **Home directory:** `@.safeword/guides/file.md` (personal conventions across all projects)
239
+ - **Recursive imports:** Imported files can import others (max depth: 5 hops)
240
+ - **Inline usage:** Can reference imports in text, not just standalone lines
241
+ - **Code blocks:** Imports ignored inside `` `code spans` `` and code blocks
242
+
243
+ ## Example: Well-Structured Root (AGENTS.md / CLAUDE.md / CURSOR.md)
244
+
245
+ ```markdown
246
+ # Project Name - Developer Context
247
+
248
+ **⚠️ ALWAYS READ FIRST: @./.safeword/SAFEWORD.md**
249
+
250
+ The SAFEWORD.md file contains core development patterns, workflows, and conventions.
251
+ Read it BEFORE working on any task in this project.
252
+
253
+ ---
254
+
255
+ Brief description. Current status.
256
+
257
+ ## Design Philosophy
258
+
259
+ 1. **Test-Driven Development (TDD):** Write tests before implementation
260
+ 2. **Core principle:** Why we chose this approach
261
+ 3. **Core principle:** Trade-offs we accepted
262
+
263
+ ## Architecture Decisions
264
+
265
+ ### Tech Choice 1
266
+
267
+ **Decision:** What we chose
268
+ **Why:** Reduces X, improves Y (specific numbers)
269
+ **Trade-off:** Harder to debug, but worth it for UX
270
+ **Gotcha:** Must do Z or it breaks
271
+
272
+ ## Domain Requirements
273
+
274
+ ### Game Mechanics (Blades in the Dark)
275
+
276
+ - Position/effect system (rulebook p.8-12)
277
+ - Fiction-first approach
278
+
279
+ **Key principles:**
280
+
281
+ - Position telegraphed before roll
282
+ - Consequences flow from fiction
283
+
284
+ ### Document Editor UX
285
+
286
+ - Performance <16ms per keystroke
287
+ - Standard OS text editing conventions
288
+
289
+ **Key principles:**
290
+
291
+ - Fast is a feature
292
+ - Don't reinvent native behavior
293
+
294
+ ## Common Gotchas
295
+
296
+ 1. **Thing:** Why it breaks (see Design Philosophy → Section)
297
+
298
+ ## File Organization
299
+
300
+ **Dir** (`path/`) - Purpose. See `path/AGENTS.md`.
301
+ ```
302
+
303
+ ## Maintenance
304
+
305
+ - Update when architecture changes
306
+ - Remove outdated sections immediately
307
+ - Consolidate if multiple files reference same concept
308
+ - Test: Can new developer understand "why" from reading this?
309
+ - **Use imports** to keep main file under 200 lines
310
+ - Verify loaded context matches intent (check hierarchical loading behavior)
311
+
312
+ ---
313
+
314
+ ## Domain Requirements Section (Optional)
315
+
316
+ **When to add:**
317
+
318
+ - Project has specialized domain knowledge (game mechanics, industry standards, UX patterns)
319
+ - Domain concerns are non-obvious from codebase alone
320
+ - You find yourself repeatedly explaining domain rules to AI
321
+
322
+ **When to skip:**
323
+
324
+ - Domain is obvious from code structure (REST API patterns)
325
+ - Tech stack IS the domain (generic CRUD app)
326
+ - Simple projects without specialized knowledge
327
+
328
+ **Structure (easy to parse):**
329
+
330
+ ```markdown
331
+ ## Domain Requirements
332
+
333
+ ### [Domain Name 1]
334
+
335
+ - [Key principle or rule]
336
+ - [Key principle or rule]
337
+ - [Resource reference if applicable]
338
+
339
+ **Key principles:**
340
+
341
+ - [Specific guideline with rationale]
342
+ - [Specific guideline with rationale]
343
+
344
+ ### [Domain Name 2]
345
+
346
+ - [Key principle or rule]
347
+ - [Key principle or rule]
348
+
349
+ **Key principles:**
350
+
351
+ - [Specific guideline with rationale]
352
+ ```
353
+
354
+ **Examples:**
355
+
356
+ ```markdown
357
+ ## Domain Requirements
358
+
359
+ ### Blades in the Dark Mechanics
360
+
361
+ - Position/effect system (BITD rulebook p.8-12)
362
+ - Stress and harm tracking (p.13-15)
363
+ - Downtime and long-term projects (p.16-20)
364
+
365
+ **Key principles:**
366
+
367
+ - Fiction drives mechanics (not dice → narrative)
368
+ - Position must be telegraphed before roll
369
+ - Effect established collaboratively with player
370
+ - Consequences flow from fiction, not arbitrary
371
+
372
+ ### Document Editor UX
373
+
374
+ - Text editing performance (<16ms per keystroke)
375
+ - Undo/redo granularity (word-level, not character)
376
+ - Selection handling (standard OS conventions)
377
+
378
+ **Key principles:**
379
+
380
+ - Performance over features (fast is a feature)
381
+ - Native OS behavior (don't reinvent text editing)
382
+ - Accessibility first (screen readers, keyboard nav)
383
+
384
+ ### Conversational AI Patterns
385
+
386
+ - GM tone: Collaborative, not dictatorial
387
+ - Player agency: Always offer meaningful choices
388
+ - Emergent narrative: Build on player ideas
389
+
390
+ **Key principles:**
391
+
392
+ - Ask questions, don't declare outcomes
393
+ - "Yes, and..." over "No, but..."
394
+ - Telegraph consequences before they happen
395
+ ```
396
+
397
+ **Why this structure:**
398
+
399
+ - ✅ **Easy to parse** - Simple markdown hierarchy (##, ###, bullets)
400
+ - ✅ **Scannable** - Domain names as headers, principles as bullets
401
+ - ✅ **Rationale included** - Explains "why" not just "what"
402
+ - ✅ **Resource references** - Links to rulebooks, docs, standards
403
+ - ✅ **Concrete** - Specific guidelines, not vague advice
404
+
405
+ **Integration with auto-quality-review hook:**
406
+
407
+ - Hook prompts: "Does it adhere to... domain requirements?"
408
+ - Claude checks CLAUDE.md/AGENTS.md for Domain Requirements section
409
+ - If present → Reviews against documented principles
410
+ - If absent → Infers domain from context as usual
411
+
412
+ ---
413
+
414
+ ## Writing for LLM Comprehension
415
+
416
+ **Critical:** These files are instructions consumed by LLMs.
417
+
418
+ **See:** `@.safeword/guides/llm-instruction-design.md` for 13 core principles for writing LLM-consumable documentation.
419
+
420
+ ### Quality Checklist
421
+
422
+ Before committing:
423
+
424
+ - [ ] All decision logic uses MECE principle (mutually exclusive, collectively exhaustive)
425
+ - [ ] All project-specific terms are defined
426
+ - [ ] No contradictions between sections
427
+ - [ ] Rules have concrete examples
428
+ - [ ] Edge cases explicitly covered
429
+ - [ ] Vague terms replaced with actionable principles
430
+ - [ ] No redundancy (each fact stated once)
431
+ - [ ] File under 200 lines (or uses imports)
432
+
433
+ ---
434
+
435
+ ## Best Practices from Anthropic
436
+
437
+ **Conciseness:**
438
+
439
+ - Use short, declarative bullet points (not narrative paragraphs)
440
+ - Trim redundancy (don't explain obvious folder names like "components folder contains components")
441
+ - Don't include commentary or nice-to-have information
442
+ - Files are loaded with every request - keep lean
443
+
444
+ **Effectiveness:**
445
+
446
+ - **Treat as living document** - Constantly refine based on what works
447
+ - Periodically review and refactor for clarity
448
+ - At Anthropic, teams use prompt improver to tune instructions
449
+ - Add emphasis ("IMPORTANT", "YOU MUST") for critical rules
450
+ - Explicitly reference your context file in prompts ("following CLAUDE.md guidelines") for better adherence
451
+
452
+ **Token Budget:**
453
+
454
+ - Context files are often prepended to prompts - they consume context with each interaction
455
+ - Bloated files cost more tokens and introduce noise
456
+ - Keep under 50KB for optimal performance (though no hard limit)
457
+ - Use imports to modularize instead of monolithic files
@@ -0,0 +1,200 @@
1
+ # Data Architecture Documentation Guide
2
+
3
+ **Context:** How to document data architecture decisions, models, and flows for software projects. Applies LLM instruction design principles for clarity and reliability.
4
+
5
+ **See:** `@.safeword/guides/llm-instruction-design.md` for comprehensive framework on writing LLM-consumable documentation.
6
+
7
+ ---
8
+
9
+ ## When to Document Data Architecture
10
+
11
+ **Decision Tree (Answer IN ORDER, stop at first match):**
12
+
13
+ 1. **Project initialization?** → Create `DATA_ARCHITECTURE.md` or `ARCHITECTURE.md` with data section
14
+ 2. **Adding new data store?** (database, cache, file system) → Update architecture doc
15
+ 3. **Changing data model?** (schema, entities, relationships) → Update architecture doc
16
+ 4. **Data flow integration?** (API, ETL, sync) → Update architecture doc
17
+ 5. **Single feature implementation?** → Use design doc (reference architecture doc)
18
+
19
+ **Tie-breaking rule:** If multiple apply, stop at first match (earlier = more general).
20
+
21
+ **Edge cases:**
22
+
23
+ - Single feature but adds 3+ entities → Architecture doc (impacts data model)
24
+ - Bug fix changes schema → Architecture doc (schema changes always documented)
25
+ - Feature uses existing data model → Design doc only
26
+
27
+ ---
28
+
29
+ ## Core Principles (Define First, Then Apply)
30
+
31
+ ### 1. Data Quality
32
+
33
+ **What:** Ensure data is accurate, complete, consistent, and timely.
34
+
35
+ **Why:** Poor data quality cascades to business logic bugs, corrupted state, and user-facing errors.
36
+
37
+ **Document:**
38
+
39
+ - Validation rules (types, constraints, ranges)
40
+ - Data source of truth (which store is canonical)
41
+ - Quality checkpoints (where validation happens)
42
+
43
+ **Example format:**
44
+
45
+ ```markdown
46
+ **[Entity] state** (source of truth: [storage type])
47
+
48
+ - `field1`: constraint (e.g., 0-100 integer)
49
+ - `field2[]`: max N entries, validation rule
50
+ **Validation checkpoint:** `validateFunction()` in `file.ts:line`
51
+ ```
52
+
53
+ ### 2. Data Governance
54
+
55
+ **What:** Policies that govern data access, modification, and lifecycle.
56
+
57
+ **Why:** Prevents unauthorized access, conflicting writes, and data loss.
58
+
59
+ **Document:**
60
+
61
+ - Who can read/write each data entity
62
+ - When data is created/updated/deleted
63
+ - Conflict resolution strategies
64
+
65
+ **Example format:**
66
+
67
+ ```markdown
68
+ **[Entity] state**:
69
+
70
+ - Read: [roles with read access]
71
+ - Write: [roles with write access] (via `updateFunction()`)
72
+ - Delete: [strategy] (e.g., soft delete with `deletedAt`)
73
+ - Conflict: [resolution strategy] (e.g., last-write-wins, CRDT merge)
74
+ ```
75
+
76
+ ### 3. Data Accessibility
77
+
78
+ **What:** Ensure data is available when needed, performantly.
79
+
80
+ **Why:** Users expect instant feedback; slow queries degrade UX.
81
+
82
+ **Document:**
83
+
84
+ - Access patterns (how data is queried)
85
+ - Performance targets (max query time)
86
+ - Caching strategies
87
+
88
+ **Example format:**
89
+
90
+ ```markdown
91
+ **[Entity] list** (accessed on [trigger]):
92
+
93
+ - Target: <Nms load time
94
+ - Strategy: [database index/optimization]
95
+ - Cache: [caching approach] or "No cache needed"
96
+ ```
97
+
98
+ ### 4. Living Documentation
99
+
100
+ **What:** Documentation stays current with code, not a one-time artifact.
101
+
102
+ **Why:** Outdated docs are worse than no docs (mislead developers).
103
+
104
+ **Document:**
105
+
106
+ - Version/status (Production/Proposed)
107
+ - Last updated date
108
+ - Migration strategy when changing
109
+
110
+ **Example format:**
111
+
112
+ ```markdown
113
+ **Version:** X.Y
114
+ **Status:** Production (vX) + Proposed (vY)
115
+ **Last Updated:** YYYY-MM-DD
116
+
117
+ ## Current Schema (vX - Production)
118
+
119
+ ## Proposed Schema (vY - Feature Name)
120
+
121
+ ## Migration Strategy
122
+ ```
123
+
124
+ ---
125
+
126
+ ## What to Document
127
+
128
+ ### 1. Data Models
129
+
130
+ **Three levels (conceptual → logical → physical):**
131
+
132
+ - **Conceptual**: High-level entities with descriptions (e.g., "User - person with account", "Order - purchase transaction")
133
+ - **Logical**: Attributes, types, relationships, constraints (e.g., `userId: UUID`, `orders: Order[]` 1:N relationship)
134
+ - **Physical**: Storage technology, tables/collections, indexes, WHY this tech (trade-offs) - see Core Principles → Data Quality (lines 41-47) for format
135
+
136
+ ### 2. Data Flows
137
+
138
+ **Document:** Sources → Transformations → Destinations + Error Handling
139
+
140
+ **Include:** Input validation, business logic transformations, persistence steps, UI updates, error handling for each step.
141
+
142
+ **Example format:**
143
+
144
+ ```markdown
145
+ **[Flow Name]** (trigger: [user action/event])
146
+
147
+ 1. **Input** → Validation (`validateFn()`) | Error: return 400
148
+ 2. **Transform** → Business logic | Error: rollback + notify
149
+ 3. **Persist** → Database write | Error: retry 3x, then fail
150
+ 4. **UI Update** → Optimistic update | Error: revert state
151
+ ```
152
+
153
+ ### 3. Data Policies
154
+
155
+ **Document:** Access control (who reads/writes), validation rules, lifecycle (creation, updates, deletion, purging).
156
+
157
+ ### 4. Data Integration
158
+
159
+ **Document:** External systems (APIs, files, services), sync strategies, conflict resolution, error handling.
160
+
161
+ ---
162
+
163
+ ## Integration with TDD Workflow
164
+
165
+ **See:** `@.safeword/guides/architecture-guide.md` for full TDD workflow integration.
166
+
167
+ **Data-specific triggers for updating architecture doc:**
168
+
169
+ - Adding new data entities
170
+ - Changing schema (new fields, relationships)
171
+ - Changing storage technology
172
+ - Discovering performance bottlenecks
173
+
174
+ ---
175
+
176
+ ## Common Mistakes
177
+
178
+ ❌ **No source of truth defined** → Conflicting data in multiple stores
179
+ ❌ **Missing validation rules** → Invalid data written to persistence
180
+ ❌ **No migration strategy** → Breaking changes brick user data
181
+ ❌ **Outdated documentation** → Schema and docs don't match (worse than no docs)
182
+ ❌ **Implementation details in architecture doc** → Save for design docs
183
+ ❌ **Ignoring performance targets** → Slow queries degrade UX
184
+
185
+ ---
186
+
187
+ ## Best Practices Checklist
188
+
189
+ Before finalizing data architecture doc:
190
+
191
+ - [ ] Principles follow What/Why/Document/Example format (4 principles minimum)
192
+ - [ ] All entities defined with descriptions (3+ entities for conceptual model)
193
+ - [ ] Each entity has attributes, types, relationships (logical model complete)
194
+ - [ ] Storage tech documented with WHY + trade-offs (physical model includes rationale)
195
+ - [ ] Each data flow includes error handling (not just happy path)
196
+ - [ ] Validation checkpoints specified with line numbers (where validation happens)
197
+ - [ ] Performance targets use concrete numbers (<Nms, not "fast")
198
+ - [ ] Migration strategy covers both additive and breaking changes
199
+ - [ ] Version and status match codebase (verify with git/deployment)
200
+ - [ ] Cross-referenced from root ARCHITECTURE.md or SAFEWORD.md (link exists)