aether-colony 3.1.5 → 3.1.16

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 (133) hide show
  1. package/.claude/commands/ant/archaeology.md +12 -0
  2. package/.claude/commands/ant/build.md +382 -319
  3. package/.claude/commands/ant/chaos.md +23 -1
  4. package/.claude/commands/ant/colonize.md +147 -87
  5. package/.claude/commands/ant/continue.md +213 -23
  6. package/.claude/commands/ant/council.md +22 -0
  7. package/.claude/commands/ant/dream.md +18 -0
  8. package/.claude/commands/ant/entomb.md +178 -6
  9. package/.claude/commands/ant/init.md +87 -13
  10. package/.claude/commands/ant/lay-eggs.md +45 -5
  11. package/.claude/commands/ant/oracle.md +82 -9
  12. package/.claude/commands/ant/organize.md +2 -2
  13. package/.claude/commands/ant/pause-colony.md +86 -28
  14. package/.claude/commands/ant/phase.md +26 -0
  15. package/.claude/commands/ant/plan.md +204 -111
  16. package/.claude/commands/ant/resume-colony.md +23 -1
  17. package/.claude/commands/ant/resume.md +159 -0
  18. package/.claude/commands/ant/seal.md +177 -3
  19. package/.claude/commands/ant/swarm.md +78 -97
  20. package/.claude/commands/ant/verify-castes.md +7 -7
  21. package/.claude/commands/ant/watch.md +17 -0
  22. package/.opencode/agents/aether-ambassador.md +97 -0
  23. package/.opencode/agents/aether-archaeologist.md +91 -0
  24. package/.opencode/agents/aether-architect.md +66 -0
  25. package/.opencode/agents/aether-auditor.md +111 -0
  26. package/.opencode/agents/aether-builder.md +28 -10
  27. package/.opencode/agents/aether-chaos.md +98 -0
  28. package/.opencode/agents/aether-chronicler.md +80 -0
  29. package/.opencode/agents/aether-gatekeeper.md +107 -0
  30. package/.opencode/agents/aether-guardian.md +107 -0
  31. package/.opencode/agents/aether-includer.md +108 -0
  32. package/.opencode/agents/aether-keeper.md +106 -0
  33. package/.opencode/agents/aether-measurer.md +119 -0
  34. package/.opencode/agents/aether-probe.md +91 -0
  35. package/.opencode/agents/aether-queen.md +72 -19
  36. package/.opencode/agents/aether-route-setter.md +85 -0
  37. package/.opencode/agents/aether-sage.md +98 -0
  38. package/.opencode/agents/aether-scout.md +33 -15
  39. package/.opencode/agents/aether-surveyor-disciplines.md +334 -0
  40. package/.opencode/agents/aether-surveyor-nest.md +272 -0
  41. package/.opencode/agents/aether-surveyor-pathogens.md +209 -0
  42. package/.opencode/agents/aether-surveyor-provisions.md +277 -0
  43. package/.opencode/agents/aether-tracker.md +91 -0
  44. package/.opencode/agents/aether-watcher.md +30 -12
  45. package/.opencode/agents/aether-weaver.md +87 -0
  46. package/.opencode/agents/workers.md +1034 -0
  47. package/.opencode/commands/ant/archaeology.md +44 -26
  48. package/.opencode/commands/ant/build.md +326 -294
  49. package/.opencode/commands/ant/chaos.md +32 -4
  50. package/.opencode/commands/ant/colonize.md +119 -93
  51. package/.opencode/commands/ant/continue.md +98 -10
  52. package/.opencode/commands/ant/council.md +28 -0
  53. package/.opencode/commands/ant/dream.md +24 -0
  54. package/.opencode/commands/ant/entomb.md +73 -1
  55. package/.opencode/commands/ant/feedback.md +8 -2
  56. package/.opencode/commands/ant/flag.md +9 -3
  57. package/.opencode/commands/ant/flags.md +8 -2
  58. package/.opencode/commands/ant/focus.md +8 -2
  59. package/.opencode/commands/ant/help.md +12 -0
  60. package/.opencode/commands/ant/init.md +49 -4
  61. package/.opencode/commands/ant/lay-eggs.md +30 -2
  62. package/.opencode/commands/ant/oracle.md +39 -7
  63. package/.opencode/commands/ant/organize.md +8 -2
  64. package/.opencode/commands/ant/pause-colony.md +54 -1
  65. package/.opencode/commands/ant/phase.md +36 -4
  66. package/.opencode/commands/ant/plan.md +224 -116
  67. package/.opencode/commands/ant/redirect.md +8 -2
  68. package/.opencode/commands/ant/resume-colony.md +51 -26
  69. package/.opencode/commands/ant/seal.md +76 -0
  70. package/.opencode/commands/ant/status.md +50 -20
  71. package/.opencode/commands/ant/swarm.md +108 -104
  72. package/.opencode/commands/ant/tunnels.md +107 -2
  73. package/CHANGELOG.md +16 -0
  74. package/README.md +199 -86
  75. package/bin/cli.js +142 -25
  76. package/bin/generate-commands.sh +100 -16
  77. package/bin/lib/caste-colors.js +5 -5
  78. package/bin/lib/errors.js +16 -0
  79. package/bin/lib/file-lock.js +279 -44
  80. package/bin/lib/state-sync.js +206 -23
  81. package/bin/lib/update-transaction.js +206 -24
  82. package/bin/sync-to-runtime.sh +138 -0
  83. package/package.json +2 -2
  84. package/runtime/CONTEXT.md +160 -0
  85. package/runtime/aether-utils.sh +1421 -55
  86. package/runtime/docs/AETHER-2.0-IMPLEMENTATION-PLAN.md +1343 -0
  87. package/runtime/docs/AETHER-PHEROMONE-SYSTEM-MASTER-SPEC.md +2642 -0
  88. package/runtime/docs/PHEROMONE-INJECTION.md +240 -0
  89. package/runtime/docs/PHEROMONE-INTEGRATION.md +192 -0
  90. package/runtime/docs/PHEROMONE-SYSTEM-DESIGN.md +426 -0
  91. package/runtime/docs/README.md +94 -0
  92. package/runtime/docs/VISUAL-OUTPUT-SPEC.md +219 -0
  93. package/runtime/docs/biological-reference.md +272 -0
  94. package/runtime/docs/codebase-review.md +399 -0
  95. package/runtime/docs/command-sync.md +164 -0
  96. package/runtime/docs/implementation-learnings.md +89 -0
  97. package/runtime/docs/known-issues.md +217 -0
  98. package/runtime/docs/namespace.md +148 -0
  99. package/runtime/docs/planning-discipline.md +159 -0
  100. package/runtime/exchange/pheromone-xml.sh +574 -0
  101. package/runtime/exchange/registry-xml.sh +269 -0
  102. package/runtime/exchange/wisdom-xml.sh +312 -0
  103. package/runtime/lib/queen-utils.sh +729 -0
  104. package/runtime/model-profiles.yaml +100 -0
  105. package/runtime/recover.sh +136 -0
  106. package/runtime/schemas/aether-types.xsd +255 -0
  107. package/runtime/schemas/colony-registry.xsd +309 -0
  108. package/runtime/schemas/pheromone.xsd +163 -0
  109. package/runtime/schemas/prompt.xsd +416 -0
  110. package/runtime/schemas/queen-wisdom.xsd +325 -0
  111. package/runtime/schemas/worker-priming.xsd +276 -0
  112. package/runtime/templates/QUEEN.md.template +79 -0
  113. package/runtime/utils/atomic-write.sh +5 -5
  114. package/runtime/utils/chamber-utils.sh +6 -3
  115. package/runtime/utils/error-handler.sh +200 -0
  116. package/runtime/utils/queen-to-md.xsl +395 -0
  117. package/runtime/utils/spawn-tree.sh +428 -0
  118. package/runtime/utils/spawn-with-model.sh +56 -0
  119. package/runtime/utils/state-loader.sh +215 -0
  120. package/runtime/utils/swarm-display.sh +5 -5
  121. package/runtime/utils/watch-spawn-tree.sh +90 -22
  122. package/runtime/utils/xml-compose.sh +247 -0
  123. package/runtime/utils/xml-core.sh +186 -0
  124. package/runtime/utils/xml-utils.sh +2196 -0
  125. package/runtime/verification-loop.md +1 -1
  126. package/runtime/workers-new-castes.md +516 -0
  127. package/runtime/workers.md +18 -6
  128. package/.aether/visualizations/anthill-stages/brood-stable.txt +0 -26
  129. package/.aether/visualizations/anthill-stages/crowned-anthill.txt +0 -30
  130. package/.aether/visualizations/anthill-stages/first-mound.txt +0 -18
  131. package/.aether/visualizations/anthill-stages/open-chambers.txt +0 -24
  132. package/.aether/visualizations/anthill-stages/sealed-chambers.txt +0 -28
  133. package/.aether/visualizations/anthill-stages/ventilated-nest.txt +0 -27
@@ -62,6 +62,11 @@ atomic_write() {
62
62
  return 1
63
63
  fi
64
64
 
65
+ # Create backup if target exists (do this BEFORE validation to avoid race condition)
66
+ if [ -f "$target_file" ]; then
67
+ create_backup "$target_file"
68
+ fi
69
+
65
70
  # Validate JSON if it's a JSON file
66
71
  if [[ "$target_file" == *.json ]]; then
67
72
  if ! python3 -c "import json; json.load(open('$temp_file'))" 2>/dev/null; then
@@ -71,11 +76,6 @@ atomic_write() {
71
76
  fi
72
77
  fi
73
78
 
74
- # Create backup if target exists
75
- if [ -f "$target_file" ]; then
76
- create_backup "$target_file"
77
- fi
78
-
79
79
  # Atomic rename (overwrites target if exists)
80
80
  if ! mv "$temp_file" "$target_file"; then
81
81
  echo "Failed to rename temp file to target: $target_file"
@@ -15,9 +15,12 @@ set -euo pipefail
15
15
  LOCK_ACQUIRED=${LOCK_ACQUIRED:-false}
16
16
  CURRENT_LOCK=${CURRENT_LOCK:-""}
17
17
 
18
- # Get script directory for sourcing
19
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
20
- AETHER_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd 2>/dev/null || echo "$SCRIPT_DIR/../..")"
18
+ # Get script directory for sourcing (preserve parent SCRIPT_DIR if set)
19
+ __chamber_utils_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
20
+ AETHER_ROOT="$(cd "$__chamber_utils_dir/../.." && pwd 2>/dev/null || echo "$__chamber_utils_dir/../..")"
21
+
22
+ # Use parent SCRIPT_DIR if available, otherwise use local
23
+ SCRIPT_DIR="${SCRIPT_DIR:-$__chamber_utils_dir}"
21
24
 
22
25
  # Source atomic-write for safe file operations
23
26
  [[ -f "$SCRIPT_DIR/atomic-write.sh" ]] && source "$SCRIPT_DIR/atomic-write.sh"
@@ -0,0 +1,200 @@
1
+ #!/bin/bash
2
+ # Aether Colony Error Handler Module
3
+ # Structured JSON error handling for bash utilities
4
+ #
5
+ # Usage: source "$SCRIPT_DIR/utils/error-handler.sh"
6
+ #
7
+ # Provides consistent error format between Node.js CLI and bash utilities
8
+
9
+ # --- Error Code Constants (matching Node.js error codes) ---
10
+ E_UNKNOWN="E_UNKNOWN"
11
+ E_HUB_NOT_FOUND="E_HUB_NOT_FOUND"
12
+ E_REPO_NOT_INITIALIZED="E_REPO_NOT_INITIALIZED"
13
+ E_FILE_NOT_FOUND="E_FILE_NOT_FOUND"
14
+ E_JSON_INVALID="E_JSON_INVALID"
15
+ E_LOCK_FAILED="E_LOCK_FAILED"
16
+ E_GIT_ERROR="E_GIT_ERROR"
17
+ E_VALIDATION_FAILED="E_VALIDATION_FAILED"
18
+ E_FEATURE_UNAVAILABLE="E_FEATURE_UNAVAILABLE"
19
+ E_BASH_ERROR="E_BASH_ERROR"
20
+
21
+ # --- Recovery Suggestion Functions (internal, prefixed with _) ---
22
+ _recovery_hub_not_found() { echo '"Run: aether install"'; }
23
+ _recovery_repo_not_init() { echo '"Run /ant:init in this repo first"'; }
24
+ _recovery_file_not_found() { echo '"Check file path and permissions"'; }
25
+ _recovery_json_invalid() { echo '"Validate JSON syntax"'; }
26
+ _recovery_lock_failed() { echo '"Wait for other operations to complete"'; }
27
+ _recovery_git_error() { echo '"Check git status and resolve conflicts"'; }
28
+ _recovery_default() { echo 'null'; }
29
+
30
+ # Get recovery suggestion based on error code
31
+ _get_recovery() {
32
+ local code="$1"
33
+ case "$code" in
34
+ "$E_HUB_NOT_FOUND") _recovery_hub_not_found ;;
35
+ "$E_REPO_NOT_INITIALIZED") _recovery_repo_not_init ;;
36
+ "$E_FILE_NOT_FOUND") _recovery_file_not_found ;;
37
+ "$E_JSON_INVALID") _recovery_json_invalid ;;
38
+ "$E_LOCK_FAILED") _recovery_lock_failed ;;
39
+ "$E_GIT_ERROR") _recovery_git_error ;;
40
+ *) _recovery_default ;;
41
+ esac
42
+ }
43
+
44
+ # --- Enhanced json_err function ---
45
+ # Signature: json_err [code] [message] [details] [recovery]
46
+ # All parameters optional with sensible defaults
47
+ json_err() {
48
+ local code="${1:-$E_UNKNOWN}"
49
+ local message="${2:-An unknown error occurred}"
50
+ local details="${3:-null}"
51
+ local recovery="${4:-}"
52
+ local timestamp
53
+ timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
54
+
55
+ # Get recovery suggestion if not provided
56
+ if [[ -z "$recovery" ]]; then
57
+ recovery=$(_get_recovery "$code")
58
+ else
59
+ # Escape and quote the recovery string
60
+ recovery=$(echo "$recovery" | sed 's/"/\\"/g' | tr '\n' ' ')
61
+ recovery="\"$recovery\""
62
+ fi
63
+
64
+ # Escape message for JSON
65
+ local escaped_message
66
+ escaped_message=$(echo "$message" | sed 's/"/\\"/g' | tr '\n' ' ')
67
+
68
+ # Build details JSON
69
+ local details_json
70
+ if [[ "$details" == "null" || -z "$details" ]]; then
71
+ details_json="null"
72
+ else
73
+ details_json="$details"
74
+ fi
75
+
76
+ # Output structured JSON to stderr
77
+ printf '{"ok":false,"error":{"code":"%s","message":"%s","details":%s,"recovery":%s,"timestamp":"%s"}}\n' \
78
+ "$code" "$escaped_message" "$details_json" "$recovery" "$timestamp" >&2
79
+
80
+ # Log to activity.log (best effort)
81
+ if [[ -n "${DATA_DIR:-}" ]]; then
82
+ echo "[$timestamp] ERROR $code: $escaped_message" >> "$DATA_DIR/activity.log" 2>/dev/null || true
83
+ fi
84
+
85
+ exit 1
86
+ }
87
+
88
+ # --- json_warn function for non-fatal warnings ---
89
+ # Signature: json_warn [code] [message]
90
+ json_warn() {
91
+ local code="${1:-W_UNKNOWN}"
92
+ local message="${2:-Warning}"
93
+ local timestamp
94
+ timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
95
+
96
+ # Escape message for JSON
97
+ local escaped_message
98
+ escaped_message=$(echo "$message" | sed 's/"/\\"/g' | tr '\n' ' ')
99
+
100
+ # Output warning JSON to stdout (not stderr - this is non-fatal)
101
+ printf '{"ok":true,"warning":{"code":"%s","message":"%s","timestamp":"%s"}}\n' \
102
+ "$code" "$escaped_message" "$timestamp"
103
+
104
+ # Log to activity.log (best effort)
105
+ if [[ -n "${DATA_DIR:-}" ]]; then
106
+ echo "[$timestamp] WARN $code: $escaped_message" >> "$DATA_DIR/activity.log" 2>/dev/null || true
107
+ fi
108
+ }
109
+
110
+ # --- error_handler function for trap ERR ---
111
+ # Captures: line number, command, exit code
112
+ # Usage: trap 'error_handler ${LINENO} "$BASH_COMMAND" $?' ERR
113
+ error_handler() {
114
+ local line_num="${1:-unknown}"
115
+ local command="${2:-unknown}"
116
+ local exit_code="${3:-1}"
117
+ local timestamp
118
+ timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
119
+
120
+ # Escape command for JSON
121
+ local escaped_command
122
+ escaped_command=$(echo "$command" | sed 's/"/\\"/g' | tr '\n' ' ')
123
+
124
+ # Build details JSON
125
+ local details
126
+ details="{\"line\":$line_num,\"command\":\"$escaped_command\",\"exit_code\":$exit_code}"
127
+
128
+ # Output structured JSON to stderr
129
+ printf '{"ok":false,"error":{"code":"%s","message":"Bash command failed","details":%s,"recovery":%s,"timestamp":"%s"}}\n' \
130
+ "$E_BASH_ERROR" "$details" "$(_recovery_default)" "$timestamp" >&2
131
+
132
+ # Log to activity.log (best effort)
133
+ if [[ -n "${DATA_DIR:-}" ]]; then
134
+ echo "[$timestamp] ERROR $E_BASH_ERROR: Command failed at line $line_num (exit $exit_code)" >> "$DATA_DIR/activity.log" 2>/dev/null || true
135
+ fi
136
+
137
+ exit 1
138
+ }
139
+
140
+ # --- Feature flag functions for graceful degradation ---
141
+ # Using simple variables for bash 3.2+ compatibility (no associative arrays)
142
+
143
+ # Track disabled features as colon-separated list: "feature1:reason1|feature2:reason2"
144
+ _FEATURES_DISABLED=""
145
+
146
+ # Enable a feature (remove from disabled list if present)
147
+ feature_enable() {
148
+ local name="$1"
149
+ # Remove from disabled list if present
150
+ _FEATURES_DISABLED=$(echo "$_FEATURES_DISABLED" | sed "s/:$name:[^|]*//g" | sed 's/^|//;s/|$//')
151
+ }
152
+
153
+ # Disable a feature with reason
154
+ feature_disable() {
155
+ local name="$1"
156
+ local reason="${2:-disabled}"
157
+ # Remove existing entry if present, then add new
158
+ _FEATURES_DISABLED=$(echo "$_FEATURES_DISABLED" | sed "s/:$name:[^|]*//g")
159
+ if [[ -z "$_FEATURES_DISABLED" ]]; then
160
+ _FEATURES_DISABLED=":$name:$reason"
161
+ else
162
+ _FEATURES_DISABLED="${_FEATURES_DISABLED}|:$name:$reason"
163
+ fi
164
+ }
165
+
166
+ # Check if feature is enabled (returns 0 if enabled, 1 if disabled)
167
+ feature_enabled() {
168
+ local name="$1"
169
+ if echo "$_FEATURES_DISABLED" | grep -q ":$name:"; then
170
+ return 1
171
+ fi
172
+ return 0
173
+ }
174
+
175
+ # Get reason for feature being disabled
176
+ _feature_reason() {
177
+ local name="$1"
178
+ echo "$_FEATURES_DISABLED" | grep -o ":$name:[^|]*" | sed "s/:$name://" || echo "unknown"
179
+ }
180
+
181
+ # Log degradation warning
182
+ feature_log_degradation() {
183
+ local name="$1"
184
+ local reason="${2:-}"
185
+ if [[ -z "$reason" ]]; then
186
+ reason=$(_feature_reason "$name")
187
+ fi
188
+ json_warn "W_DEGRADED" "Feature '$name' is disabled: $reason"
189
+ }
190
+
191
+ # --- Export all functions and variables ---
192
+ export -f json_err json_warn error_handler
193
+ export -f feature_enable feature_disable feature_enabled feature_log_degradation
194
+ export -f _get_recovery _recovery_hub_not_found _recovery_repo_not_init
195
+ export -f _recovery_file_not_found _recovery_json_invalid _recovery_lock_failed
196
+ export -f _recovery_git_error _recovery_default _feature_reason
197
+ export E_UNKNOWN E_HUB_NOT_FOUND E_REPO_NOT_INITIALIZED E_FILE_NOT_FOUND
198
+ export E_JSON_INVALID E_LOCK_FAILED E_GIT_ERROR E_VALIDATION_FAILED
199
+ export E_FEATURE_UNAVAILABLE E_BASH_ERROR
200
+ export _FEATURES_DISABLED
@@ -0,0 +1,395 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!--
3
+ XSLT Stylesheet: queen-wisdom to Markdown
4
+
5
+ Purpose: Transform queen-wisdom.xml into human-readable QUEEN.md format
6
+ for display and documentation purposes.
7
+
8
+ Usage: xsltproc queen-to-md.xsl queen-wisdom.xml > QUEEN.md
9
+
10
+ Version: 1.0.0
11
+ Namespace: http://aether.colony/schemas/queen-wisdom/1.0
12
+ -->
13
+ <xsl:stylesheet version="1.0"
14
+ xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
15
+
16
+ <xsl:output method="text" encoding="UTF-8" indent="no"/>
17
+
18
+ <!-- ============================================================ -->
19
+ <!-- Main Template -->
20
+ <!-- ============================================================ -->
21
+
22
+ <xsl:template match="/queen-wisdom">
23
+ <xsl:text># QUEEN.md — Colony Wisdom&#10;&#10;</xsl:text>
24
+
25
+ <!-- Metadata Header -->
26
+ <xsl:text>&gt; Last evolved: </xsl:text>
27
+ <xsl:value-of select="metadata/modified"/>
28
+ <xsl:text>&#10;</xsl:text>
29
+
30
+ <xsl:text>&gt; Colonies contributed: </xsl:text>
31
+ <xsl:value-of select="count(//evidence/colony-ref/@id)"/>
32
+ <xsl:text>&#10;</xsl:text>
33
+
34
+ <xsl:text>&gt; Wisdom version: </xsl:text>
35
+ <xsl:value-of select="metadata/version"/>
36
+ <xsl:text>&#10;&#10;</xsl:text>
37
+
38
+ <xsl:text>---&#10;&#10;</xsl:text>
39
+
40
+ <!-- Philosophies Section -->
41
+ <xsl:apply-templates select="philosophies"/>
42
+
43
+ <!-- Patterns Section -->
44
+ <xsl:apply-templates select="patterns"/>
45
+
46
+ <!-- Redirects Section -->
47
+ <xsl:apply-templates select="redirects"/>
48
+
49
+ <!-- Stack Wisdom Section -->
50
+ <xsl:apply-templates select="stack-wisdom"/>
51
+
52
+ <!-- Decrees Section -->
53
+ <xsl:apply-templates select="decrees"/>
54
+
55
+ <!-- Evolution Log Section -->
56
+ <xsl:apply-templates select="evolution-log"/>
57
+
58
+ <xsl:text>---&#10;&#10;</xsl:text>
59
+
60
+ <!-- JSON Metadata Block (for backward compatibility) -->
61
+ <xsl:call-template name="metadata-block"/>
62
+ </xsl:template>
63
+
64
+ <!-- ============================================================ -->
65
+ <!-- Section Templates -->
66
+ <!-- ============================================================ -->
67
+
68
+ <xsl:template match="philosophies">
69
+ <xsl:text>## 📜 Philosophies&#10;&#10;</xsl:text>
70
+ <xsl:text>Core beliefs that guide all colony work. These are validated through repeated successful application across multiple colonies.&#10;&#10;</xsl:text>
71
+
72
+ <xsl:for-each select="philosophy">
73
+ <xsl:text>- **</xsl:text>
74
+ <xsl:value-of select="@id"/>
75
+ <xsl:text>** (</xsl:text>
76
+ <xsl:value-of select="@created_at"/>
77
+ <xsl:text>): </xsl:text>
78
+ <xsl:value-of select="content"/>
79
+ <xsl:text>&#10;</xsl:text>
80
+
81
+ <!-- Principles subsection if present -->
82
+ <xsl:if test="principles/principle">
83
+ <xsl:text> - Principles:&#10;</xsl:text>
84
+ <xsl:for-each select="principles/principle">
85
+ <xsl:text> - </xsl:text>
86
+ <xsl:value-of select="."/>
87
+ <xsl:text>&#10;</xsl:text>
88
+ </xsl:for-each>
89
+ </xsl:if>
90
+
91
+ <!-- Evidence subsection -->
92
+ <xsl:if test="evidence/colony-ref">
93
+ <xsl:text> - Validated by: </xsl:text>
94
+ <xsl:for-each select="evidence/colony-ref">
95
+ <xsl:value-of select="@id"/>
96
+ <xsl:if test="position() != last()">, </xsl:if>
97
+ </xsl:for-each>
98
+ <xsl:text>&#10;</xsl:text>
99
+ </xsl:if>
100
+ </xsl:for-each>
101
+
102
+ <xsl:text>&#10;</xsl:text>
103
+ </xsl:template>
104
+
105
+ <xsl:template match="patterns">
106
+ <xsl:text>## 🧭 Patterns&#10;&#10;</xsl:text>
107
+ <xsl:text>Validated approaches that consistently work. These represent discovered best practices that have proven themselves in the field.&#10;&#10;</xsl:text>
108
+
109
+ <xsl:for-each select="pattern">
110
+ <xsl:text>- **</xsl:text>
111
+ <xsl:value-of select="@id"/>
112
+ <xsl:text>**</xsl:text>
113
+
114
+ <xsl:if test="pattern_type">
115
+ <xsl:text> [</xsl:text>
116
+ <xsl:value-of select="pattern_type"/>
117
+ <xsl:text>]</xsl:text>
118
+ </xsl:if>
119
+
120
+ <xsl:text> (</xsl:text>
121
+ <xsl:value-of select="@created_at"/>
122
+ <xsl:text>): </xsl:text>
123
+ <xsl:value-of select="content"/>
124
+ <xsl:text>&#10;</xsl:text>
125
+
126
+ <!-- Detection criteria if present -->
127
+ <xsl:if test="detection_criteria">
128
+ <xsl:text> - When to apply: </xsl:text>
129
+ <xsl:value-of select="detection_criteria"/>
130
+ <xsl:text>&#10;</xsl:text>
131
+ </xsl:if>
132
+
133
+ <!-- Examples if present -->
134
+ <xsl:if test="examples/example">
135
+ <xsl:text> - Examples:&#10;</xsl:text>
136
+ <xsl:for-each select="examples/example">
137
+ <xsl:text> - Scenario: </xsl:text>
138
+ <xsl:value-of select="scenario"/>
139
+ <xsl:text> → Application: </xsl:text>
140
+ <xsl:value-of select="application"/>
141
+ <xsl:if test="outcome">
142
+ <xsl:text> (Outcome: </xsl:text>
143
+ <xsl:value-of select="outcome"/>
144
+ <xsl:text>)</xsl:text>
145
+ </xsl:if>
146
+ <xsl:text>&#10;</xsl:text>
147
+ </xsl:for-each>
148
+ </xsl:if>
149
+ </xsl:for-each>
150
+
151
+ <xsl:text>&#10;</xsl:text>
152
+ </xsl:template>
153
+
154
+ <xsl:template match="redirects">
155
+ <xsl:text>## ⚠️ Redirects&#10;&#10;</xsl:text>
156
+ <xsl:text>Anti-patterns to avoid. These represent approaches that have caused problems and should be redirected away from.&#10;&#10;</xsl:text>
157
+
158
+ <xsl:for-each select="redirect">
159
+ <xsl:text>- **</xsl:text>
160
+ <xsl:value-of select="@id"/>
161
+ <xsl:text>**</xsl:text>
162
+
163
+ <xsl:if test="@confidence">
164
+ <xsl:text> [strength: </xsl:text>
165
+ <xsl:value-of select="@confidence"/>
166
+ <xsl:text>]</xsl:text>
167
+ </xsl:if>
168
+
169
+ <xsl:text> (</xsl:text>
170
+ <xsl:value-of select="@created_at"/>
171
+ <xsl:text>): </xsl:text>
172
+ <xsl:value-of select="content"/>
173
+ <xsl:text>&#10;</xsl:text>
174
+
175
+ <!-- Constraint type if present -->
176
+ <xsl:if test="constraint_type">
177
+ <xsl:text> - Constraint: </xsl:text>
178
+ <xsl:value-of select="constraint_type"/>
179
+ <xsl:text>&#10;</xsl:text>
180
+ </xsl:if>
181
+
182
+ <!-- Context if present -->
183
+ <xsl:if test="context">
184
+ <xsl:text> - Context: </xsl:text>
185
+ <xsl:value-of select="context"/>
186
+ <xsl:text>&#10;</xsl:text>
187
+ </xsl:if>
188
+ </xsl:for-each>
189
+
190
+ <xsl:text>&#10;</xsl:text>
191
+ </xsl:template>
192
+
193
+ <xsl:template match="stack-wisdom">
194
+ <xsl:text>## 🔧 Stack Wisdom&#10;&#10;</xsl:text>
195
+ <xsl:text>Technology-specific insights and constraints detected through codebase analysis.&#10;&#10;</xsl:text>
196
+
197
+ <xsl:for-each select="wisdom">
198
+ <xsl:text>- **</xsl:text>
199
+ <xsl:value-of select="@id"/>
200
+ <xsl:text>**</xsl:text>
201
+
202
+ <xsl:if test="technology">
203
+ <xsl:text> [</xsl:text>
204
+ <xsl:value-of select="technology"/>
205
+ <xsl:text>]</xsl:text>
206
+ </xsl:if>
207
+
208
+ <xsl:text> (</xsl:text>
209
+ <xsl:value-of select="@created_at"/>
210
+ <xsl:text>): </xsl:text>
211
+ <xsl:value-of select="content"/>
212
+ <xsl:text>&#10;</xsl:text>
213
+
214
+ <!-- Version range if present -->
215
+ <xsl:if test="version_range">
216
+ <xsl:text> - Applies to: </xsl:text>
217
+ <xsl:value-of select="version_range"/>
218
+ <xsl:text>&#10;</xsl:text>
219
+ </xsl:if>
220
+
221
+ <!-- Workaround if present -->
222
+ <xsl:if test="workaround">
223
+ <xsl:text> - Workaround: </xsl:text>
224
+ <xsl:value-of select="workaround"/>
225
+ <xsl:text>&#10;</xsl:text>
226
+ </xsl:if>
227
+ </xsl:for-each>
228
+
229
+ <xsl:text>&#10;</xsl:text>
230
+ </xsl:template>
231
+
232
+ <xsl:template match="decrees">
233
+ <xsl:text>## 🏛️ Decrees&#10;&#10;</xsl:text>
234
+ <xsl:text>User-mandated rules that override other guidance. These represent explicit directives from the Queen.&#10;&#10;</xsl:text>
235
+
236
+ <xsl:for-each select="decree">
237
+ <xsl:text>- **</xsl:text>
238
+ <xsl:value-of select="@id"/>
239
+ <xsl:text>**</xsl:text>
240
+
241
+ <xsl:if test="scope">
242
+ <xsl:text> [</xsl:text>
243
+ <xsl:value-of select="scope"/>
244
+ <xsl:text>]</xsl:text>
245
+ </xsl:if>
246
+
247
+ <xsl:text> (</xsl:text>
248
+ <xsl:value-of select="@created_at"/>
249
+ <xsl:text>): </xsl:text>
250
+ <xsl:value-of select="content"/>
251
+ <xsl:text>&#10;</xsl:text>
252
+
253
+ <!-- Authority if present -->
254
+ <xsl:if test="authority">
255
+ <xsl:text> - Authority: </xsl:text>
256
+ <xsl:value-of select="authority"/>
257
+ <xsl:text>&#10;</xsl:text>
258
+ </xsl:if>
259
+
260
+ <!-- Expiration if present -->
261
+ <xsl:if test="expiration">
262
+ <xsl:text> - Expires: </xsl:text>
263
+ <xsl:value-of select="expiration"/>
264
+ <xsl:text>&#10;</xsl:text>
265
+ </xsl:if>
266
+ </xsl:for-each>
267
+
268
+ <xsl:text>&#10;</xsl:text>
269
+ </xsl:template>
270
+
271
+ <xsl:template match="evolution-log">
272
+ <xsl:text>## 📊 Evolution Log&#10;&#10;</xsl:text>
273
+ <xsl:text>Track how wisdom has evolved over time.&#10;&#10;</xsl:text>
274
+
275
+ <xsl:text>| Date | Colony | Change | Details |&#10;</xsl:text>
276
+ <xsl:text>|------|--------|--------|---------|&#10;</xsl:text>
277
+
278
+ <xsl:for-each select="entry">
279
+ <xsl:text>| </xsl:text>
280
+ <xsl:value-of select="@timestamp"/>
281
+ <xsl:text> | </xsl:text>
282
+ <xsl:value-of select="@colony"/>
283
+ <xsl:text> | </xsl:text>
284
+ <xsl:value-of select="@action"/>
285
+ <xsl:text> | </xsl:text>
286
+
287
+ <!-- Build details string -->
288
+ <xsl:choose>
289
+ <xsl:when test="@type and @from">
290
+ <xsl:text>Promoted </xsl:text>
291
+ <xsl:value-of select="@type"/>
292
+ <xsl:text> from </xsl:text>
293
+ <xsl:value-of select="@from"/>
294
+ </xsl:when>
295
+ <xsl:when test="@type">
296
+ <xsl:text>Added: </xsl:text>
297
+ <xsl:value-of select="@type"/>
298
+ </xsl:when>
299
+ <xsl:otherwise>
300
+ <xsl:text>Entry recorded</xsl:text>
301
+ </xsl:otherwise>
302
+ </xsl:choose>
303
+
304
+ <xsl:if test="note">
305
+ <xsl:text> - </xsl:text>
306
+ <xsl:value-of select="note"/>
307
+ </xsl:if>
308
+
309
+ <xsl:text> |&#10;</xsl:text>
310
+ </xsl:for-each>
311
+
312
+ <xsl:text>&#10;</xsl:text>
313
+ </xsl:template>
314
+
315
+ <!-- ============================================================ -->
316
+ <!-- Metadata Block Template -->
317
+ <!-- ============================================================ -->
318
+
319
+ <xsl:template name="metadata-block">
320
+ <xsl:text>&lt;!-- METADATA&#10;</xsl:text>
321
+ <xsl:text>{&#10;</xsl:text>
322
+
323
+ <xsl:text> "version": "</xsl:text>
324
+ <xsl:value-of select="metadata/version"/>
325
+ <xsl:text>",&#10;</xsl:text>
326
+
327
+ <xsl:text> "last_evolved": "</xsl:text>
328
+ <xsl:value-of select="metadata/modified"/>
329
+ <xsl:text>",&#10;</xsl:text>
330
+
331
+ <!-- Count colonies contributed -->
332
+ <xsl:text> "colonies_contributed": [</xsl:text>
333
+ <xsl:for-each select="//evidence/colony-ref[not(@id=preceding::colony-ref/@id)]">
334
+ <xsl:text>"</xsl:text>
335
+ <xsl:value-of select="@id"/>
336
+ <xsl:text>"</xsl:text>
337
+ <xsl:if test="position() != last()">, </xsl:if>
338
+ </xsl:for-each>
339
+ <xsl:text>],&#10;</xsl:text>
340
+
341
+ <!-- Promotion thresholds -->
342
+ <xsl:text> "promotion_thresholds": {&#10;</xsl:text>
343
+ <xsl:text> "philosophy": 5,&#10;</xsl:text>
344
+ <xsl:text> "pattern": 3,&#10;</xsl:text>
345
+ <xsl:text> "redirect": 2,&#10;</xsl:text>
346
+ <xsl:text> "stack": 1,&#10;</xsl:text>
347
+ <xsl:text> "decree": 0&#10;</xsl:text>
348
+ <xsl:text> },&#10;</xsl:text>
349
+
350
+ <!-- Stats -->
351
+ <xsl:text> "stats": {&#10;</xsl:text>
352
+ <xsl:text> "total_philosophies": </xsl:text>
353
+ <xsl:value-of select="count(//philosophy)"/>
354
+ <xsl:text>,&#10;</xsl:text>
355
+
356
+ <xsl:text> "total_patterns": </xsl:text>
357
+ <xsl:value-of select="count(//pattern)"/>
358
+ <xsl:text>,&#10;</xsl:text>
359
+
360
+ <xsl:text> "total_redirects": </xsl:text>
361
+ <xsl:value-of select="count(//redirect)"/>
362
+ <xsl:text>,&#10;</xsl:text>
363
+
364
+ <xsl:text> "total_stack_entries": </xsl:text>
365
+ <xsl:value-of select="count(//stack-wisdom/wisdom)"/>
366
+ <xsl:text>,&#10;</xsl:text>
367
+
368
+ <xsl:text> "total_decrees": </xsl:text>
369
+ <xsl:value-of select="count(//decree)"/>
370
+ <xsl:text>&#10;</xsl:text>
371
+
372
+ <xsl:text> }&#10;</xsl:text>
373
+ <xsl:text>}&#10;</xsl:text>
374
+ <xsl:text>--&gt;&#10;</xsl:text>
375
+ </xsl:template>
376
+
377
+ <!-- ============================================================ -->
378
+ <!-- Utility Templates -->
379
+ <!-- ============================================================ -->
380
+
381
+ <!-- Strip trailing/leading whitespace -->
382
+ <xsl:template name="trim">
383
+ <xsl:param name="string"/>
384
+ <xsl:value-of select="normalize-space($string)"/>
385
+ </xsl:template>
386
+
387
+ <!-- Escape markdown special characters in content -->
388
+ <xsl:template name="escape-markdown">
389
+ <xsl:param name="text"/>
390
+ <!-- Note: In XSLT 1.0, complex string replacement requires recursive templates -->
391
+ <!-- For simplicity, we assume content doesn't contain problematic markdown -->
392
+ <xsl:value-of select="$text"/>
393
+ </xsl:template>
394
+
395
+ </xsl:stylesheet>