@ekkos/cli 0.2.18 → 1.0.0

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 (98) hide show
  1. package/README.md +57 -0
  2. package/dist/agent/daemon.d.ts +27 -0
  3. package/dist/agent/daemon.js +254 -29
  4. package/dist/agent/health-check.d.ts +35 -0
  5. package/dist/agent/health-check.js +243 -0
  6. package/dist/agent/pty-runner.d.ts +1 -0
  7. package/dist/agent/pty-runner.js +6 -1
  8. package/dist/capture/eviction-client.d.ts +139 -0
  9. package/dist/capture/eviction-client.js +454 -0
  10. package/dist/capture/index.d.ts +2 -0
  11. package/dist/capture/index.js +2 -0
  12. package/dist/capture/jsonl-rewriter.d.ts +96 -0
  13. package/dist/capture/jsonl-rewriter.js +1369 -0
  14. package/dist/capture/transcript-repair.d.ts +51 -0
  15. package/dist/capture/transcript-repair.js +319 -0
  16. package/dist/commands/agent.d.ts +6 -0
  17. package/dist/commands/agent.js +244 -0
  18. package/dist/commands/dashboard.d.ts +25 -0
  19. package/dist/commands/dashboard.js +1175 -0
  20. package/dist/commands/doctor.js +23 -1
  21. package/dist/commands/run.d.ts +5 -0
  22. package/dist/commands/run.js +1605 -516
  23. package/dist/commands/setup-remote.js +146 -37
  24. package/dist/commands/swarm-dashboard.d.ts +20 -0
  25. package/dist/commands/swarm-dashboard.js +735 -0
  26. package/dist/commands/swarm-setup.d.ts +10 -0
  27. package/dist/commands/swarm-setup.js +956 -0
  28. package/dist/commands/swarm.d.ts +46 -0
  29. package/dist/commands/swarm.js +441 -0
  30. package/dist/commands/test-claude.d.ts +16 -0
  31. package/dist/commands/test-claude.js +156 -0
  32. package/dist/commands/usage/blocks.d.ts +8 -0
  33. package/dist/commands/usage/blocks.js +60 -0
  34. package/dist/commands/usage/daily.d.ts +9 -0
  35. package/dist/commands/usage/daily.js +96 -0
  36. package/dist/commands/usage/dashboard.d.ts +8 -0
  37. package/dist/commands/usage/dashboard.js +104 -0
  38. package/dist/commands/usage/formatters.d.ts +41 -0
  39. package/dist/commands/usage/formatters.js +147 -0
  40. package/dist/commands/usage/index.d.ts +13 -0
  41. package/dist/commands/usage/index.js +87 -0
  42. package/dist/commands/usage/monthly.d.ts +8 -0
  43. package/dist/commands/usage/monthly.js +66 -0
  44. package/dist/commands/usage/session.d.ts +11 -0
  45. package/dist/commands/usage/session.js +193 -0
  46. package/dist/commands/usage/weekly.d.ts +9 -0
  47. package/dist/commands/usage/weekly.js +61 -0
  48. package/dist/commands/usage.d.ts +7 -0
  49. package/dist/commands/usage.js +214 -0
  50. package/dist/cron/index.d.ts +7 -0
  51. package/dist/cron/index.js +13 -0
  52. package/dist/cron/promoter.d.ts +70 -0
  53. package/dist/cron/promoter.js +403 -0
  54. package/dist/deploy/instructions.d.ts +5 -2
  55. package/dist/deploy/instructions.js +11 -8
  56. package/dist/index.js +262 -5
  57. package/dist/lib/tmux-scrollbar.d.ts +14 -0
  58. package/dist/lib/tmux-scrollbar.js +296 -0
  59. package/dist/lib/usage-monitor.d.ts +47 -0
  60. package/dist/lib/usage-monitor.js +124 -0
  61. package/dist/lib/usage-parser.d.ts +162 -0
  62. package/dist/lib/usage-parser.js +583 -0
  63. package/dist/restore/RestoreOrchestrator.d.ts +4 -0
  64. package/dist/restore/RestoreOrchestrator.js +118 -30
  65. package/dist/utils/log-rotate.d.ts +18 -0
  66. package/dist/utils/log-rotate.js +74 -0
  67. package/dist/utils/platform.d.ts +2 -0
  68. package/dist/utils/platform.js +3 -1
  69. package/dist/utils/session-binding.d.ts +5 -0
  70. package/dist/utils/session-binding.js +46 -0
  71. package/dist/utils/state.js +4 -0
  72. package/dist/utils/verify-remote-terminal.d.ts +10 -0
  73. package/dist/utils/verify-remote-terminal.js +415 -0
  74. package/package.json +9 -2
  75. package/templates/CLAUDE.md +135 -23
  76. package/templates/ekkos-manifest.json +5 -5
  77. package/templates/hooks/lib/contract.sh +43 -31
  78. package/templates/hooks/lib/count-tokens.cjs +86 -0
  79. package/templates/hooks/lib/ekkos-reminders.sh +98 -0
  80. package/templates/hooks/lib/state.sh +53 -1
  81. package/templates/hooks/stop.sh +150 -388
  82. package/templates/hooks/user-prompt-submit.sh +353 -443
  83. package/templates/windsurf-hooks/README.md +212 -0
  84. package/templates/windsurf-hooks/hooks.json +9 -2
  85. package/templates/windsurf-hooks/install.sh +148 -0
  86. package/templates/windsurf-hooks/lib/contract.sh +2 -0
  87. package/templates/windsurf-hooks/post-cascade-response.sh +251 -0
  88. package/templates/windsurf-hooks/pre-user-prompt.sh +435 -0
  89. package/templates/windsurf-skills/ekkos-memory/SKILL.md +219 -0
  90. package/templates/agents/README.md +0 -182
  91. package/templates/agents/code-reviewer.md +0 -166
  92. package/templates/agents/debug-detective.md +0 -169
  93. package/templates/agents/ekkOS_Vercel.md +0 -99
  94. package/templates/agents/extension-manager.md +0 -229
  95. package/templates/agents/git-companion.md +0 -185
  96. package/templates/agents/github-test-agent.md +0 -321
  97. package/templates/agents/railway-manager.md +0 -215
  98. package/templates/windsurf-hooks/before-submit-prompt.sh +0 -238
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://ekkos.dev/schemas/manifest-v1.json",
3
3
  "manifestVersion": "1.0.0",
4
- "generatedAt": "2026-01-22T06:19:22.744Z",
4
+ "generatedAt": "2026-02-18T04:29:25.535Z",
5
5
  "platforms": {
6
6
  "darwin": {
7
7
  "configDir": "~/.ekkos",
@@ -68,14 +68,14 @@
68
68
  "source": "hooks/user-prompt-submit.sh",
69
69
  "destination": "user-prompt-submit.sh",
70
70
  "description": "User prompt submit hook (Unix)",
71
- "checksum": "0e006eb7becba874f34fc622c9ee83b3c96a6e00daf95db840369894af94abf3",
71
+ "checksum": "ef294d0088a94c6588a91fc91e6cc06a14dd46bc83ca09d73c64abfb11984f40",
72
72
  "executable": true
73
73
  },
74
74
  {
75
75
  "source": "hooks/stop.sh",
76
76
  "destination": "stop.sh",
77
77
  "description": "Session stop hook (Unix)",
78
- "checksum": "fd436ea847bf6fbe367f8aa61ddf6d97e8605837badc3af8d433b59599ef6615",
78
+ "checksum": "f3b4495c0c2bbdf5bfef762568faec7b70b41b463dad4c88b37cf7ee32b5257d",
79
79
  "executable": true
80
80
  },
81
81
  {
@@ -124,14 +124,14 @@
124
124
  "source": "hooks/lib/contract.sh",
125
125
  "destination": "lib/contract.sh",
126
126
  "description": "Hook contract library",
127
- "checksum": "1d86128a2a4a25e653a02c4eb08dbfd28db53063c618cd43b85f0603a135e8b2",
127
+ "checksum": "3ef16930ba10cd37a1c5fa8469ec13a6a84cadb882d3f9a96c72da875ba07fc7",
128
128
  "executable": true
129
129
  },
130
130
  {
131
131
  "source": "hooks/lib/state.sh",
132
132
  "destination": "lib/state.sh",
133
133
  "description": "Hook state management library",
134
- "checksum": "ba72c00a1fb0f6768e37aff754ad6ac0e8e20aa0b9b393193e2d92357cac5a71",
134
+ "checksum": "99223ce9b869d78762084ff184327fa2814af7a16e5f2ac9619af43b5e551d7c",
135
135
  "executable": true
136
136
  }
137
137
  ]
@@ -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
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Token counter for Claude Code hooks
4
+ * Extracts ACTUAL token data from Anthropic API usage field
5
+ *
6
+ * The transcript contains the real usage data from each API response:
7
+ * - input_tokens: new tokens in this request
8
+ * - cache_read_input_tokens: cached tokens from previous turns
9
+ * - cache_creation_input_tokens: tokens added to cache
10
+ *
11
+ * Usage:
12
+ * node count-tokens.cjs <transcript.jsonl> -> outputs total token count
13
+ * node count-tokens.cjs <transcript.jsonl> --json -> outputs full breakdown as JSON
14
+ */
15
+
16
+ const fs = require('fs');
17
+
18
+ const filePath = process.argv[2];
19
+ const outputJson = process.argv.includes('--json');
20
+
21
+ if (!filePath) {
22
+ console.error('Usage: count-tokens.cjs <transcript.jsonl> [--json]');
23
+ process.exit(1);
24
+ }
25
+
26
+ if (!fs.existsSync(filePath)) {
27
+ console.error(`[count-tokens] ERROR: File not found: ${filePath}`);
28
+ process.exit(1);
29
+ }
30
+
31
+ /**
32
+ * Extract token breakdown from the most recent assistant message's usage field
33
+ * This is the authoritative data from Anthropic's API
34
+ */
35
+ function getTokenBreakdown(filePath) {
36
+ const content = fs.readFileSync(filePath, 'utf-8');
37
+ const lines = content.trim().split('\n').filter(Boolean);
38
+
39
+ // Find the most recent assistant message with usage data
40
+ let latestUsage = null;
41
+
42
+ for (const line of lines) {
43
+ try {
44
+ const entry = JSON.parse(line);
45
+
46
+ if (entry.type === 'assistant' && entry.message?.usage) {
47
+ latestUsage = entry.message.usage;
48
+ }
49
+ } catch (e) {
50
+ continue;
51
+ }
52
+ }
53
+
54
+ if (!latestUsage) {
55
+ return {
56
+ input_tokens: 0,
57
+ cache_read_tokens: 0,
58
+ cache_creation_tokens: 0,
59
+ total_tokens: 0,
60
+ output_tokens: 0
61
+ };
62
+ }
63
+
64
+ const inputTokens = latestUsage.input_tokens || 0;
65
+ const cacheRead = latestUsage.cache_read_input_tokens || 0;
66
+ const cacheCreation = latestUsage.cache_creation_input_tokens || 0;
67
+ const outputTokens = latestUsage.output_tokens || 0;
68
+
69
+ return {
70
+ input_tokens: inputTokens,
71
+ cache_read_tokens: cacheRead,
72
+ cache_creation_tokens: cacheCreation,
73
+ total_tokens: inputTokens + cacheRead + cacheCreation,
74
+ output_tokens: outputTokens
75
+ };
76
+ }
77
+
78
+ const breakdown = getTokenBreakdown(filePath);
79
+
80
+ if (outputJson) {
81
+ // Output full breakdown as JSON (for API)
82
+ console.log(JSON.stringify(breakdown));
83
+ } else {
84
+ // Output just the total (backward compatible)
85
+ console.log(breakdown.total_tokens);
86
+ }
@@ -0,0 +1,98 @@
1
+ #!/bin/bash
2
+ # ═══════════════════════════════════════════════════════════════════════════
3
+ # ekkOS Tool Usage Reminders - Inject proactive tool usage hints
4
+ # ═══════════════════════════════════════════════════════════════════════════
5
+
6
+ # Detect if user query suggests using specific ekkOS tools
7
+ # Returns markdown reminders to inject into Claude's context
8
+
9
+ get_ekkos_reminders() {
10
+ local query="$1"
11
+ local prev_response="$2"
12
+ local reminders=""
13
+
14
+ # Convert to lowercase for matching
15
+ query_lower=$(echo "$query" | tr '[:upper:]' '[:lower:]')
16
+ response_lower=$(echo "$prev_response" | tr '[:upper:]' '[:lower:]')
17
+
18
+ # ═══════════════════════════════════════════════════════════════════════════
19
+ # ekkOS_Search - Before answering technical questions
20
+ # ═══════════════════════════════════════════════════════════════════════════
21
+ if echo "$query_lower" | grep -qE "(how do|how to|what is|why|explain|security|authentication|database|rls|deployment|error|bug|fix)"; then
22
+ reminders+="🧠 **ekkOS_Search Required**: Search memory before answering technical questions\n"
23
+ fi
24
+
25
+ # ═══════════════════════════════════════════════════════════════════════════
26
+ # ekkOS_Forge - When fixing bugs or learning
27
+ # ═══════════════════════════════════════════════════════════════════════════
28
+ if echo "$response_lower" | grep -qE "(fixed|solved|resolved|bug|issue|error|migration|implemented|created|updated)"; then
29
+ reminders+="🔨 **ekkOS_Forge Recommended**: Forge pattern after fixing bugs or implementing solutions\n"
30
+ fi
31
+
32
+ # ═══════════════════════════════════════════════════════════════════════════
33
+ # ekkOS_Directive - User preferences
34
+ # ═══════════════════════════════════════════════════════════════════════════
35
+ if echo "$query_lower" | grep -qE "(always|never|dont|don't|prefer|avoid|should not|shouldn't)"; then
36
+ directive_type="PREFER"
37
+ if echo "$query_lower" | grep -qE "(always|must)"; then
38
+ directive_type="MUST"
39
+ elif echo "$query_lower" | grep -qE "(never|dont|don't|avoid|should not)"; then
40
+ directive_type="NEVER"
41
+ fi
42
+ reminders+="📋 **ekkOS_Directive Required**: User stated a preference - save as ${directive_type} directive\n"
43
+ fi
44
+
45
+ # ═══════════════════════════════════════════════════════════════════════════
46
+ # ekkOS_Conflict - Destructive operations
47
+ # ═══════════════════════════════════════════════════════════════════════════
48
+ if echo "$query_lower" | grep -qE "(delete|remove|drop|truncate|destroy|wipe)"; then
49
+ reminders+="⚠️ **ekkOS_Conflict Required**: Check for conflicts before destructive operations\n"
50
+ fi
51
+
52
+ # ═══════════════════════════════════════════════════════════════════════════
53
+ # ekkOS_Plan - Complex multi-step tasks
54
+ # ═══════════════════════════════════════════════════════════════════════════
55
+ if echo "$query_lower" | grep -qE "(implement|build|create.*feature|add.*functionality|refactor|migrate)"; then
56
+ reminders+="📝 **ekkOS_Plan Recommended**: Create structured plan for multi-step implementation\n"
57
+ fi
58
+
59
+ # ═══════════════════════════════════════════════════════════════════════════
60
+ # ekkOS_StoreSecret - Credentials shared
61
+ # ═══════════════════════════════════════════════════════════════════════════
62
+ if echo "$query_lower" | grep -qE "(api.?key|token|password|secret|credential|github.*key|openai.*key)"; then
63
+ reminders+="🔐 **ekkOS_StoreSecret Available**: Use to securely store credentials (AES-256-GCM)\n"
64
+ fi
65
+
66
+ # ═══════════════════════════════════════════════════════════════════════════
67
+ # ekkOS_Recall - Past conversations
68
+ # ═══════════════════════════════════════════════════════════════════════════
69
+ if echo "$query_lower" | grep -qE "(yesterday|last week|previously|we discussed|earlier|before)"; then
70
+ reminders+="🕐 **ekkOS_Recall Available**: Retrieve past conversations by time\n"
71
+ fi
72
+
73
+ # ═══════════════════════════════════════════════════════════════════════════
74
+ # ekkOS_Codebase - Project-specific search
75
+ # ═══════════════════════════════════════════════════════════════════════════
76
+ if echo "$query_lower" | grep -qE "(in this project|in our codebase|where is.*defined|find.*file)"; then
77
+ reminders+="📂 **ekkOS_Codebase Available**: Search project-specific memory\n"
78
+ fi
79
+
80
+ # ═══════════════════════════════════════════════════════════════════════════
81
+ # ekkOS_Export - Backup request
82
+ # ═══════════════════════════════════════════════════════════════════════════
83
+ if echo "$query_lower" | grep -qE "(export|backup|download|save.*memory|portable)"; then
84
+ reminders+="💾 **ekkOS_Export Available**: Export all patterns/directives as portable JSON\n"
85
+ fi
86
+
87
+ # ═══════════════════════════════════════════════════════════════════════════
88
+ # ekkOS_Summary - Show activity
89
+ # ═══════════════════════════════════════════════════════════════════════════
90
+ if echo "$query_lower" | grep -qE "(what did.*do|ekkos.*activity|memory.*stats|show.*patterns)"; then
91
+ reminders+="📊 **ekkOS_Summary Available**: Show recent ekkOS activity\n"
92
+ fi
93
+
94
+ echo "$reminders"
95
+ }
96
+
97
+ # Export function
98
+ export -f get_ekkos_reminders
@@ -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
  # ═══════════════════════════════════════════════════════════════════════════