@ekkos/cli 1.0.35 → 1.0.36

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.
@@ -78,7 +78,7 @@ write_turn_contract() {
78
78
  "retrieved_directive_ids": $directive_array,
79
79
  "timestamp": "$timestamp",
80
80
  "query_hash": "$query_hash",
81
- "ekkos_strict": ${EKKOS_STRICT:-0}
81
+ "ekkos_strict": ${EKKOS_STRICT:-1}
82
82
  }
83
83
  EOF
84
84
 
@@ -134,7 +134,7 @@ cleanup_turn_contract() {
134
134
 
135
135
  # Check if strict mode is enabled
136
136
  is_strict_mode() {
137
- [ "${EKKOS_STRICT:-0}" = "1" ]
137
+ [ "${EKKOS_STRICT:-1}" = "1" ] # DEFAULT: ON
138
138
  }
139
139
 
140
140
  # Generate strict mode blocker message for Claude Code
@@ -157,50 +157,57 @@ EOF
157
157
  }
158
158
 
159
159
  # Validate PatternGuard coverage (returns 0-100)
160
+ # PORTABLE: Works on macOS without Perl regex (grep -P)
160
161
  calculate_pattern_guard_coverage() {
161
162
  local assistant_response="$1"
162
163
  local pattern_ids="$2" # Comma-separated
163
164
 
164
- # Count total patterns
165
- local total_count
166
- total_count=$(echo "$pattern_ids" | tr ',' '\n' | grep -c '.' || echo 0)
165
+ # Count total patterns - more robust counting
166
+ local total_count=0
167
+ if [ -n "$pattern_ids" ]; then
168
+ total_count=$(echo "$pattern_ids" | tr ',' '\n' | grep -v '^$' | wc -l | tr -d ' ')
169
+ fi
167
170
 
168
171
  if [ "$total_count" -eq 0 ]; then
169
172
  echo "100" # No patterns = 100% coverage by definition
170
173
  return 0
171
174
  fi
172
175
 
173
- # Extract acknowledged IDs from [ekkOS_SELECT] and [ekkOS_SKIP] blocks
176
+ # Extract acknowledged IDs using portable awk (works on macOS and Linux)
174
177
  local acknowledged_count=0
175
178
 
176
- # Check SELECT block
177
- local select_block
178
- select_block=$(echo "$assistant_response" | grep -ozP '\[ekkOS_SELECT\][\s\S]*?\[/ekkOS_SELECT\]' 2>/dev/null | tr '\0' '\n' || true)
179
- if [ -n "$select_block" ]; then
180
- local select_count
181
- select_count=$(echo "$select_block" | grep -oE 'id:\s*[a-f0-9-]+' | wc -l | tr -d ' ')
182
- acknowledged_count=$((acknowledged_count + select_count))
183
- fi
184
-
185
- # Check SKIP block
186
- local skip_block
187
- skip_block=$(echo "$assistant_response" | grep -ozP '\[ekkOS_SKIP\][\s\S]*?\[/ekkOS_SKIP\]' 2>/dev/null | tr '\0' '\n' || true)
188
- if [ -n "$skip_block" ]; then
189
- local skip_count
190
- skip_count=$(echo "$skip_block" | grep -oE 'id:\s*[a-f0-9-]+' | wc -l | tr -d ' ')
191
- acknowledged_count=$((acknowledged_count + skip_count))
192
- fi
179
+ # Use awk to extract SELECT block and count IDs - PORTABLE approach
180
+ local select_count=0
181
+ select_count=$(echo "$assistant_response" | awk '
182
+ /\[ekkOS_SELECT\]/{in_block=1; next}
183
+ /\[\/ekkOS_SELECT\]/{in_block=0}
184
+ in_block && /id:/{count++}
185
+ END{print count+0}
186
+ ')
187
+ acknowledged_count=$((acknowledged_count + select_count))
188
+
189
+ # Use awk to extract SKIP block and count IDs
190
+ local skip_count=0
191
+ skip_count=$(echo "$assistant_response" | awk '
192
+ /\[ekkOS_SKIP\]/{in_block=1; next}
193
+ /\[\/ekkOS_SKIP\]/{in_block=0}
194
+ in_block && /id:/{count++}
195
+ END{print count+0}
196
+ ')
197
+ acknowledged_count=$((acknowledged_count + skip_count))
193
198
 
194
199
  # Legacy: Check for [ekkOS_APPLY] markers (fallback)
195
200
  if [ "$acknowledged_count" -eq 0 ]; then
196
201
  local apply_count
197
- apply_count=$(echo "$assistant_response" | grep -c '\[ekkOS_APPLY\]' || echo 0)
198
- acknowledged_count=$apply_count
202
+ apply_count=$(echo "$assistant_response" | grep -c '\[ekkOS_APPLY\]' 2>/dev/null || echo 0)
203
+ acknowledged_count=$((acknowledged_count + apply_count))
199
204
  fi
200
205
 
201
206
  # Calculate coverage percentage
202
- local coverage
203
- coverage=$((acknowledged_count * 100 / total_count))
207
+ local coverage=0
208
+ if [ "$total_count" -gt 0 ]; then
209
+ coverage=$((acknowledged_count * 100 / total_count))
210
+ fi
204
211
 
205
212
  # Cap at 100%
206
213
  if [ "$coverage" -gt 100 ]; then
@@ -255,11 +262,16 @@ EOF
255
262
  }
256
263
 
257
264
  # Determine if turn is compliant
265
+ # ROBUST: Handles edge cases with non-numeric or empty values
258
266
  is_turn_compliant() {
259
- local retrieval_ok="$1"
260
- local pattern_guard_coverage="$2"
261
- local footer_present="$3"
262
- local pattern_count="$4"
267
+ local retrieval_ok="${1:-true}"
268
+ local pattern_guard_coverage="${2:-100}"
269
+ local footer_present="${3:-true}"
270
+ local pattern_count="${4:-0}"
271
+
272
+ # Sanitize numeric values (default to safe values)
273
+ pattern_guard_coverage=$(echo "$pattern_guard_coverage" | grep -oE '^[0-9]+$' || echo "100")
274
+ pattern_count=$(echo "$pattern_count" | grep -oE '^[0-9]+$' || echo "0")
263
275
 
264
276
  # Retrieval must have succeeded
265
277
  if [ "$retrieval_ok" != "true" ]; then
@@ -20,25 +20,40 @@ mkdir -p "$STATE_DIR"
20
20
  # State File Management
21
21
  # ═══════════════════════════════════════════════════════════════════════════
22
22
 
23
- # Save patterns for session
23
+ # Save patterns for session (with retrieval_token for verified tracking)
24
24
  save_patterns() {
25
25
  local session_id="$1"
26
26
  local patterns="$2"
27
27
  local model_used="$3"
28
+ local retrieval_token="${4:-}" # Optional retrieval_token for verified applications
28
29
 
29
30
  local state_file="$STATE_DIR/patterns-${session_id}.json"
30
31
 
31
32
  jq -n \
32
33
  --argjson patterns "$patterns" \
33
34
  --arg model "$model_used" \
35
+ --arg token "$retrieval_token" \
34
36
  --arg timestamp "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
35
37
  '{
36
38
  patterns: $patterns,
37
39
  model_used: $model,
40
+ retrieval_token: $token,
38
41
  saved_at: $timestamp
39
42
  }' > "$state_file"
40
43
  }
41
44
 
45
+ # Get retrieval token from saved patterns
46
+ get_retrieval_token() {
47
+ local session_id="$1"
48
+ local state_file="$STATE_DIR/patterns-${session_id}.json"
49
+
50
+ if [ -f "$state_file" ]; then
51
+ jq -r '.retrieval_token // ""' "$state_file" 2>/dev/null
52
+ else
53
+ echo ""
54
+ fi
55
+ }
56
+
42
57
  # Load patterns for session
43
58
  load_patterns() {
44
59
  local session_id="$1"
@@ -57,6 +72,43 @@ clear_patterns() {
57
72
  rm -f "$STATE_DIR/patterns-${session_id}.json"
58
73
  }
59
74
 
75
+ # ═══════════════════════════════════════════════════════════════════════════
76
+ # Conversation Context (for Golden Loop MEASURE phase)
77
+ # ═══════════════════════════════════════════════════════════════════════════
78
+
79
+ # Save current conversation context for pattern tracking
80
+ # Called by user-prompt-submit.sh when new query is submitted
81
+ save_conversation_context() {
82
+ local session_id="$1"
83
+ local user_query="$2"
84
+ local prev_response="$3"
85
+
86
+ local context_file="$STATE_DIR/conversation-${session_id}.json"
87
+
88
+ jq -n \
89
+ --arg query "$user_query" \
90
+ --arg response "$prev_response" \
91
+ --arg timestamp "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
92
+ '{
93
+ query: $query,
94
+ response: $response,
95
+ saved_at: $timestamp
96
+ }' > "$context_file" 2>/dev/null || true
97
+ }
98
+
99
+ # Load current conversation context
100
+ # Called by post-tool-use.sh when tracking pattern applications
101
+ load_conversation_context() {
102
+ local session_id="$1"
103
+ local context_file="$STATE_DIR/conversation-${session_id}.json"
104
+
105
+ if [ -f "$context_file" ]; then
106
+ cat "$context_file"
107
+ else
108
+ echo '{"query":"","response":""}'
109
+ fi
110
+ }
111
+
60
112
  # ═══════════════════════════════════════════════════════════════════════════
61
113
  # Capture Deduplication
62
114
  # ═══════════════════════════════════════════════════════════════════════════