rlhf-feedback-loop 0.5.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 (73) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/LICENSE +21 -0
  3. package/README.md +308 -0
  4. package/adapters/README.md +8 -0
  5. package/adapters/amp/skills/rlhf-feedback/SKILL.md +20 -0
  6. package/adapters/chatgpt/INSTALL.md +80 -0
  7. package/adapters/chatgpt/openapi.yaml +292 -0
  8. package/adapters/claude/.mcp.json +8 -0
  9. package/adapters/codex/config.toml +4 -0
  10. package/adapters/gemini/function-declarations.json +95 -0
  11. package/adapters/mcp/server-stdio.js +444 -0
  12. package/bin/cli.js +167 -0
  13. package/config/mcp-allowlists.json +29 -0
  14. package/config/policy-bundles/constrained-v1.json +53 -0
  15. package/config/policy-bundles/default-v1.json +80 -0
  16. package/config/rubrics/default-v1.json +52 -0
  17. package/config/subagent-profiles.json +32 -0
  18. package/openapi/openapi.yaml +292 -0
  19. package/package.json +91 -0
  20. package/plugins/amp-skill/INSTALL.md +52 -0
  21. package/plugins/amp-skill/SKILL.md +31 -0
  22. package/plugins/claude-skill/INSTALL.md +55 -0
  23. package/plugins/claude-skill/SKILL.md +46 -0
  24. package/plugins/codex-profile/AGENTS.md +20 -0
  25. package/plugins/codex-profile/INSTALL.md +57 -0
  26. package/plugins/gemini-extension/INSTALL.md +74 -0
  27. package/plugins/gemini-extension/gemini_prompt.txt +10 -0
  28. package/plugins/gemini-extension/tool_contract.json +28 -0
  29. package/scripts/billing.js +471 -0
  30. package/scripts/budget-guard.js +173 -0
  31. package/scripts/code-reasoning.js +307 -0
  32. package/scripts/context-engine.js +547 -0
  33. package/scripts/contextfs.js +513 -0
  34. package/scripts/contract-audit.js +198 -0
  35. package/scripts/dpo-optimizer.js +208 -0
  36. package/scripts/export-dpo-pairs.js +316 -0
  37. package/scripts/export-training.js +448 -0
  38. package/scripts/feedback-attribution.js +313 -0
  39. package/scripts/feedback-inbox-read.js +162 -0
  40. package/scripts/feedback-loop.js +838 -0
  41. package/scripts/feedback-schema.js +300 -0
  42. package/scripts/feedback-to-memory.js +165 -0
  43. package/scripts/feedback-to-rules.js +109 -0
  44. package/scripts/generate-paperbanana-diagrams.sh +99 -0
  45. package/scripts/hybrid-feedback-context.js +676 -0
  46. package/scripts/intent-router.js +164 -0
  47. package/scripts/mcp-policy.js +92 -0
  48. package/scripts/meta-policy.js +194 -0
  49. package/scripts/plan-gate.js +154 -0
  50. package/scripts/prove-adapters.js +364 -0
  51. package/scripts/prove-attribution.js +364 -0
  52. package/scripts/prove-automation.js +393 -0
  53. package/scripts/prove-data-quality.js +219 -0
  54. package/scripts/prove-intelligence.js +256 -0
  55. package/scripts/prove-lancedb.js +370 -0
  56. package/scripts/prove-loop-closure.js +255 -0
  57. package/scripts/prove-rlaif.js +404 -0
  58. package/scripts/prove-subway-upgrades.js +250 -0
  59. package/scripts/prove-training-export.js +324 -0
  60. package/scripts/prove-v2-milestone.js +273 -0
  61. package/scripts/prove-v3-milestone.js +381 -0
  62. package/scripts/rlaif-self-audit.js +123 -0
  63. package/scripts/rubric-engine.js +230 -0
  64. package/scripts/self-heal.js +127 -0
  65. package/scripts/self-healing-check.js +111 -0
  66. package/scripts/skill-quality-tracker.js +284 -0
  67. package/scripts/subagent-profiles.js +79 -0
  68. package/scripts/sync-gh-secrets-from-env.sh +29 -0
  69. package/scripts/thompson-sampling.js +331 -0
  70. package/scripts/train_from_feedback.py +914 -0
  71. package/scripts/validate-feedback.js +580 -0
  72. package/scripts/vector-store.js +100 -0
  73. package/src/api/server.js +497 -0
@@ -0,0 +1,580 @@
1
+ 'use strict';
2
+ /**
3
+ * Feedback Data Quality Validator
4
+ *
5
+ * Implements a 4-level validation pipeline:
6
+ * 1. Schema validation (required fields, value ranges)
7
+ * 2. Semantic validation (logical consistency)
8
+ * 3. Anomaly detection (suspicious patterns, sensitive data)
9
+ * 4. Self-correction (auto-correct fixable errors)
10
+ *
11
+ * Ported from Subway_RN_Demo with rlhf schema adaptations:
12
+ * - Uses 'signal' (not 'feedback') with values 'positive'/'negative'
13
+ * - Uses 'id' as required field (not 'source')
14
+ * - RLHF_FEEDBACK_DIR env var for path resolution
15
+ *
16
+ * Usage (CLI):
17
+ * echo '{"signal":"positive",...}' | node validate-feedback.js
18
+ * node validate-feedback.js --audit # Audit existing feedback log
19
+ * node validate-feedback.js --stats # Show quality statistics
20
+ *
21
+ * Usage (module):
22
+ * const { validateEntry } = require('./validate-feedback');
23
+ *
24
+ * LOCAL ONLY - Do not commit feedback log data to repository
25
+ */
26
+
27
+ const fs = require('fs');
28
+ const path = require('path');
29
+
30
+ // =============================================================================
31
+ // PATH RESOLUTION
32
+ // =============================================================================
33
+
34
+ const DEFAULT_FEEDBACK_DIR = path.join(__dirname, '..', '.claude', 'memory', 'feedback');
35
+
36
+ function getFeedbackDir() {
37
+ return process.env.RLHF_FEEDBACK_DIR || DEFAULT_FEEDBACK_DIR;
38
+ }
39
+
40
+ function getFeedbackPaths() {
41
+ const dir = getFeedbackDir();
42
+ return {
43
+ FEEDBACK_LOG: path.join(dir, 'feedback-log.jsonl'),
44
+ VALIDATION_LOG: path.join(dir, 'validation-issues.jsonl'),
45
+ QUALITY_REPORT: path.join(dir, 'quality-report.json'),
46
+ };
47
+ }
48
+
49
+ // =============================================================================
50
+ // SCHEMA VALIDATION (Level 1)
51
+ // =============================================================================
52
+
53
+ const REQUIRED_FIELDS = ['timestamp', 'signal', 'id'];
54
+ const VALID_SIGNAL_VALUES = ['positive', 'negative'];
55
+ const VALID_REWARD_RANGE = [-1, 1];
56
+
57
+ function validateSchema(entry) {
58
+ const issues = [];
59
+
60
+ // Check required fields
61
+ for (const field of REQUIRED_FIELDS) {
62
+ if (!(field in entry)) {
63
+ issues.push({
64
+ level: 'error',
65
+ field,
66
+ message: `Missing required field: ${field}`,
67
+ suggestion: `Add "${field}" to the feedback entry`,
68
+ });
69
+ }
70
+ }
71
+
72
+ // Validate signal value
73
+ if (entry.signal && !VALID_SIGNAL_VALUES.includes(entry.signal)) {
74
+ issues.push({
75
+ level: 'warning',
76
+ field: 'signal',
77
+ message: `Invalid signal value: "${entry.signal}"`,
78
+ suggestion: `Use one of: ${VALID_SIGNAL_VALUES.join(', ')}`,
79
+ });
80
+ }
81
+
82
+ // Validate reward range
83
+ if ('reward' in entry) {
84
+ if (
85
+ typeof entry.reward !== 'number' ||
86
+ entry.reward < VALID_REWARD_RANGE[0] ||
87
+ entry.reward > VALID_REWARD_RANGE[1]
88
+ ) {
89
+ issues.push({
90
+ level: 'error',
91
+ field: 'reward',
92
+ message: `Reward out of range: ${entry.reward}`,
93
+ suggestion: `Reward must be between ${VALID_REWARD_RANGE[0]} and ${VALID_REWARD_RANGE[1]}`,
94
+ });
95
+ }
96
+ }
97
+
98
+ // Validate timestamp format
99
+ if (entry.timestamp) {
100
+ const ts = new Date(entry.timestamp);
101
+ if (isNaN(ts.getTime())) {
102
+ issues.push({
103
+ level: 'error',
104
+ field: 'timestamp',
105
+ message: `Invalid timestamp format: "${entry.timestamp}"`,
106
+ suggestion: 'Use ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ',
107
+ });
108
+ } else if (ts > new Date()) {
109
+ issues.push({
110
+ level: 'warning',
111
+ field: 'timestamp',
112
+ message: 'Timestamp is in the future',
113
+ suggestion: 'Check system clock synchronization',
114
+ });
115
+ }
116
+ }
117
+
118
+ return issues;
119
+ }
120
+
121
+ // =============================================================================
122
+ // SEMANTIC VALIDATION (Level 2)
123
+ // =============================================================================
124
+
125
+ function validateSemantics(entry) {
126
+ const issues = [];
127
+
128
+ // Signal-reward consistency
129
+ if (entry.signal === 'positive' && typeof entry.reward === 'number' && entry.reward < 0) {
130
+ issues.push({
131
+ level: 'error',
132
+ field: 'reward',
133
+ message: 'Positive signal but negative reward',
134
+ explanation: 'Semantic inconsistency: positive signal should have reward >= 0',
135
+ suggestion: 'Either change signal to "negative" or reward to positive value',
136
+ });
137
+ }
138
+
139
+ if (entry.signal === 'negative' && typeof entry.reward === 'number' && entry.reward > 0) {
140
+ issues.push({
141
+ level: 'error',
142
+ field: 'reward',
143
+ message: 'Negative signal but positive reward',
144
+ explanation: 'Semantic inconsistency: negative signal should have reward <= 0',
145
+ suggestion: 'Either change signal to "positive" or reward to negative value',
146
+ });
147
+ }
148
+
149
+ // Context validation
150
+ if (entry.context !== undefined) {
151
+ // Empty or too short context
152
+ if (typeof entry.context === 'string' && entry.context.trim().length < 5) {
153
+ issues.push({
154
+ level: 'warning',
155
+ field: 'context',
156
+ message: 'Context too short to be meaningful',
157
+ explanation: 'Short context reduces ML training value',
158
+ suggestion: 'Provide more descriptive context (at least 10 characters)',
159
+ });
160
+ }
161
+
162
+ // Check for placeholder text
163
+ const placeholders = ['TODO', 'FIXME', 'placeholder', 'test', 'example'];
164
+ for (const ph of placeholders) {
165
+ if (
166
+ typeof entry.context === 'string' &&
167
+ entry.context.toLowerCase().includes(ph.toLowerCase())
168
+ ) {
169
+ issues.push({
170
+ level: 'warning',
171
+ field: 'context',
172
+ message: `Context contains placeholder text: "${ph}"`,
173
+ explanation: 'Placeholder text may indicate incomplete entry',
174
+ suggestion: 'Replace with actual context or remove entry',
175
+ });
176
+ break;
177
+ }
178
+ }
179
+ }
180
+
181
+ // Tool-name validation
182
+ if (entry.tool_name) {
183
+ const validTools = ['Read', 'Write', 'Edit', 'Bash', 'Glob', 'Grep', 'Task', 'WebFetch'];
184
+ if (!validTools.includes(entry.tool_name)) {
185
+ issues.push({
186
+ level: 'info',
187
+ field: 'tool_name',
188
+ message: `Uncommon tool: "${entry.tool_name}"`,
189
+ explanation: 'Tool not in standard list - may be valid but unusual',
190
+ suggestion: 'Verify tool name is correct',
191
+ });
192
+ }
193
+ }
194
+
195
+ return issues;
196
+ }
197
+
198
+ // =============================================================================
199
+ // ANOMALY DETECTION (Level 3)
200
+ // =============================================================================
201
+
202
+ function detectAnomalies(entry, allEntries) {
203
+ const entries = Array.isArray(allEntries) ? allEntries : [];
204
+ const issues = [];
205
+
206
+ // Rapid feedback burst (more than 5 in 1 minute)
207
+ if (entry.timestamp && entries.length > 0) {
208
+ const entryTime = new Date(entry.timestamp);
209
+ const recentEntries = entries.filter((e) => {
210
+ const t = new Date(e.timestamp);
211
+ return Math.abs(entryTime - t) < 60000; // 1 minute
212
+ });
213
+
214
+ if (recentEntries.length > 5) {
215
+ issues.push({
216
+ level: 'warning',
217
+ type: 'anomaly',
218
+ message: 'Feedback burst detected',
219
+ explanation: `${recentEntries.length} entries within 1 minute - unusual pattern`,
220
+ suggestion: 'Verify this is not automated noise or duplicate entries',
221
+ });
222
+ }
223
+ }
224
+
225
+ // Same feedback repeated exactly (duplicate detection)
226
+ if (entry.context && entries.length > 0) {
227
+ const duplicates = entries.filter(
228
+ (e) =>
229
+ e.context === entry.context &&
230
+ e.signal === entry.signal &&
231
+ e.tool_name === entry.tool_name
232
+ );
233
+
234
+ if (duplicates.length > 0) {
235
+ issues.push({
236
+ level: 'warning',
237
+ type: 'anomaly',
238
+ message: 'Duplicate feedback entry',
239
+ explanation: `Found ${duplicates.length} identical entries`,
240
+ suggestion: 'Consider deduplication or review capture logic',
241
+ });
242
+ }
243
+ }
244
+
245
+ // Feedback balance check (session imbalance)
246
+ if (entries.length >= 10) {
247
+ const positiveCount = entries.filter((e) => e.signal === 'positive').length;
248
+ const ratio = positiveCount / entries.length;
249
+
250
+ if (ratio > 0.95) {
251
+ issues.push({
252
+ level: 'info',
253
+ type: 'anomaly',
254
+ message: 'Feedback heavily skewed positive',
255
+ explanation: `${(ratio * 100).toFixed(1)}% positive - may indicate capture bias`,
256
+ suggestion: 'Review if negative cases are being properly captured',
257
+ });
258
+ } else if (ratio < 0.05) {
259
+ issues.push({
260
+ level: 'warning',
261
+ type: 'anomaly',
262
+ message: 'Feedback heavily skewed negative',
263
+ explanation: `${((1 - ratio) * 100).toFixed(1)}% negative - unusual pattern`,
264
+ suggestion: 'Check for systematic issues or misconfigured error detection',
265
+ });
266
+ }
267
+ }
268
+
269
+ // Sensitive data leakage detection
270
+ if (entry.context) {
271
+ const sensitivePatterns = [
272
+ /api[_-]?key/i,
273
+ /password/i,
274
+ /secret/i,
275
+ /token/i,
276
+ /bearer/i,
277
+ /\b[A-Za-z0-9]{32,}\b/, // Long alphanumeric strings (possible keys)
278
+ ];
279
+
280
+ for (const pattern of sensitivePatterns) {
281
+ if (pattern.test(entry.context)) {
282
+ issues.push({
283
+ level: 'error',
284
+ type: 'security',
285
+ message: 'Potential sensitive data in context',
286
+ explanation: `Pattern matched: ${pattern.toString()}`,
287
+ suggestion: 'Redact sensitive information before logging',
288
+ });
289
+ break;
290
+ }
291
+ }
292
+ }
293
+
294
+ return issues;
295
+ }
296
+
297
+ // =============================================================================
298
+ // SELF-CORRECTION (Level 4)
299
+ // =============================================================================
300
+
301
+ function generateCorrections(entry, issues) {
302
+ const corrections = [];
303
+
304
+ for (const issue of issues) {
305
+ if (issue.level === 'error') {
306
+ // Auto-correct reward to match signal
307
+ if (issue.field === 'reward' && entry.signal) {
308
+ const correctedReward =
309
+ entry.signal === 'positive' ? 1 : entry.signal === 'negative' ? -1 : 0;
310
+ corrections.push({
311
+ field: 'reward',
312
+ original: entry.reward,
313
+ corrected: correctedReward,
314
+ reason: 'Auto-corrected to match signal type',
315
+ });
316
+ }
317
+
318
+ // Auto-add missing timestamp
319
+ if (issue.field === 'timestamp' && !entry.timestamp) {
320
+ corrections.push({
321
+ field: 'timestamp',
322
+ original: null,
323
+ corrected: new Date().toISOString(),
324
+ reason: 'Added missing timestamp',
325
+ });
326
+ }
327
+ }
328
+ }
329
+
330
+ return corrections;
331
+ }
332
+
333
+ function applyCorrections(entry, corrections) {
334
+ const corrected = { ...entry };
335
+ for (const c of corrections) {
336
+ corrected[c.field] = c.corrected;
337
+ }
338
+ corrected._corrected = true;
339
+ corrected._corrections = corrections;
340
+ return corrected;
341
+ }
342
+
343
+ // =============================================================================
344
+ // MAIN VALIDATION PIPELINE
345
+ // =============================================================================
346
+
347
+ function validateEntry(entry, allEntries) {
348
+ const entries = Array.isArray(allEntries) ? allEntries : [];
349
+ const result = {
350
+ valid: true,
351
+ entry,
352
+ issues: [],
353
+ corrections: [],
354
+ correctedEntry: null,
355
+ };
356
+
357
+ // Level 1: Schema
358
+ result.issues.push(...validateSchema(entry));
359
+
360
+ // Level 2: Semantics
361
+ result.issues.push(...validateSemantics(entry));
362
+
363
+ // Level 3: Anomalies
364
+ result.issues.push(...detectAnomalies(entry, entries));
365
+
366
+ // Level 4: Self-correction
367
+ result.corrections = generateCorrections(entry, result.issues);
368
+
369
+ // Determine validity (errors make entry invalid)
370
+ const hasErrors = result.issues.some((i) => i.level === 'error');
371
+ result.valid = !hasErrors;
372
+
373
+ // Apply corrections if available
374
+ if (result.corrections.length > 0) {
375
+ result.correctedEntry = applyCorrections(entry, result.corrections);
376
+ }
377
+
378
+ return result;
379
+ }
380
+
381
+ // =============================================================================
382
+ // FEEDBACK LOG UTILITIES
383
+ // =============================================================================
384
+
385
+ function loadFeedbackLog() {
386
+ const { FEEDBACK_LOG } = getFeedbackPaths();
387
+ if (!fs.existsSync(FEEDBACK_LOG)) return [];
388
+
389
+ const content = fs.readFileSync(FEEDBACK_LOG, 'utf8');
390
+ return content
391
+ .trim()
392
+ .split('\n')
393
+ .filter((line) => line.trim())
394
+ .map((line) => {
395
+ try {
396
+ return JSON.parse(line);
397
+ } catch {
398
+ return null;
399
+ }
400
+ })
401
+ .filter((e) => e !== null);
402
+ }
403
+
404
+ // =============================================================================
405
+ // AUDIT MODE
406
+ // =============================================================================
407
+
408
+ function auditFeedbackLog() {
409
+ const { VALIDATION_LOG, QUALITY_REPORT } = getFeedbackPaths();
410
+ console.log('Auditing feedback log...\n');
411
+
412
+ const entries = loadFeedbackLog();
413
+ if (entries.length === 0) {
414
+ console.log('No entries to audit.');
415
+ return;
416
+ }
417
+
418
+ const results = {
419
+ total: entries.length,
420
+ valid: 0,
421
+ invalid: 0,
422
+ corrected: 0,
423
+ issuesByLevel: { error: 0, warning: 0, info: 0 },
424
+ issuesByField: {},
425
+ };
426
+
427
+ const validationIssues = [];
428
+
429
+ for (const entry of entries) {
430
+ const validation = validateEntry(entry, entries);
431
+
432
+ if (validation.valid) {
433
+ results.valid++;
434
+ } else {
435
+ results.invalid++;
436
+ }
437
+
438
+ if (validation.corrections.length > 0) {
439
+ results.corrected++;
440
+ }
441
+
442
+ for (const issue of validation.issues) {
443
+ results.issuesByLevel[issue.level] = (results.issuesByLevel[issue.level] || 0) + 1;
444
+ if (issue.field) {
445
+ results.issuesByField[issue.field] = (results.issuesByField[issue.field] || 0) + 1;
446
+ }
447
+
448
+ validationIssues.push({
449
+ timestamp: entry.timestamp,
450
+ entryId: entry.id,
451
+ ...issue,
452
+ });
453
+ }
454
+ }
455
+
456
+ // Save validation issues log
457
+ if (validationIssues.length > 0) {
458
+ const dir = getFeedbackDir();
459
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
460
+ const issueLog = validationIssues.map((i) => JSON.stringify(i)).join('\n');
461
+ fs.writeFileSync(VALIDATION_LOG, issueLog + '\n');
462
+ }
463
+
464
+ // Save quality report
465
+ const report = {
466
+ ...results,
467
+ validityRate: ((results.valid / results.total) * 100).toFixed(2) + '%',
468
+ auditedAt: new Date().toISOString(),
469
+ };
470
+ const dir = getFeedbackDir();
471
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
472
+ fs.writeFileSync(QUALITY_REPORT, JSON.stringify(report, null, 2) + '\n');
473
+
474
+ // Print summary
475
+ console.log(`Total entries: ${results.total}`);
476
+ console.log(`Valid: ${results.valid} (${report.validityRate})`);
477
+ console.log(`Invalid: ${results.invalid}`);
478
+ console.log(`Auto-correctable: ${results.corrected}`);
479
+ console.log('\nIssues by level:');
480
+ console.log(` Errors: ${results.issuesByLevel.error || 0}`);
481
+ console.log(` Warnings: ${results.issuesByLevel.warning || 0}`);
482
+ console.log(` Info: ${results.issuesByLevel.info || 0}`);
483
+
484
+ if (Object.keys(results.issuesByField).length > 0) {
485
+ console.log('\nTop issue fields:');
486
+ const sorted = Object.entries(results.issuesByField)
487
+ .sort((a, b) => b[1] - a[1])
488
+ .slice(0, 5);
489
+ for (const [field, count] of sorted) {
490
+ console.log(` ${field}: ${count}`);
491
+ }
492
+ }
493
+
494
+ console.log(`\nValidation issues saved to: ${VALIDATION_LOG}`);
495
+ console.log(`Quality report saved to: ${QUALITY_REPORT}`);
496
+ }
497
+
498
+ function showStats() {
499
+ const { QUALITY_REPORT } = getFeedbackPaths();
500
+ if (!fs.existsSync(QUALITY_REPORT)) {
501
+ console.log('No quality report found. Run --audit first.');
502
+ return;
503
+ }
504
+
505
+ const report = JSON.parse(fs.readFileSync(QUALITY_REPORT, 'utf8'));
506
+ console.log('Feedback Quality Statistics\n');
507
+ console.log(JSON.stringify(report, null, 2));
508
+ }
509
+
510
+ // =============================================================================
511
+ // CLI ENTRY POINT
512
+ // =============================================================================
513
+
514
+ async function main() {
515
+ const args = process.argv.slice(2);
516
+
517
+ if (args.includes('--audit')) {
518
+ auditFeedbackLog();
519
+ } else if (args.includes('--stats')) {
520
+ showStats();
521
+ } else {
522
+ // Read from stdin (piped input)
523
+ let input = '';
524
+
525
+ if (!process.stdin.isTTY) {
526
+ for await (const chunk of process.stdin) {
527
+ input += chunk;
528
+ }
529
+ }
530
+
531
+ if (input.trim()) {
532
+ try {
533
+ const entry = JSON.parse(input);
534
+ const allEntries = loadFeedbackLog();
535
+ const result = validateEntry(entry, allEntries);
536
+
537
+ if (result.valid) {
538
+ const output = result.correctedEntry || result.entry;
539
+ console.log(JSON.stringify(output));
540
+ } else {
541
+ console.error('[VALIDATION] Issues found:');
542
+ for (const issue of result.issues) {
543
+ console.error(` [${issue.level}] ${issue.message}`);
544
+ }
545
+ console.log(JSON.stringify(result.correctedEntry || result.entry));
546
+ }
547
+ } catch (e) {
548
+ console.error(`[VALIDATION] Invalid JSON: ${e.message}`);
549
+ process.exit(1);
550
+ }
551
+ } else {
552
+ console.log('Feedback Data Quality Validator');
553
+ console.log('\nUsage:');
554
+ console.log(" echo '{\"signal\":\"positive\",...}' | node validate-feedback.js");
555
+ console.log(' node validate-feedback.js --audit # Audit existing log');
556
+ console.log(' node validate-feedback.js --stats # Show statistics');
557
+ }
558
+ }
559
+ }
560
+
561
+ // =============================================================================
562
+ // MODULE EXPORTS
563
+ // =============================================================================
564
+
565
+ module.exports = {
566
+ validateEntry,
567
+ validateSchema,
568
+ validateSemantics,
569
+ detectAnomalies,
570
+ generateCorrections,
571
+ applyCorrections,
572
+ loadFeedbackLog,
573
+ auditFeedbackLog,
574
+ showStats,
575
+ };
576
+
577
+ // Run CLI only when invoked directly
578
+ if (require.main === module) {
579
+ main().catch(console.error);
580
+ }
@@ -0,0 +1,100 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+
5
+ const PROJECT_ROOT = path.join(__dirname, '..');
6
+ const DEFAULT_LANCE_DIR = path.join(PROJECT_ROOT, '.claude', 'memory', 'feedback', 'lancedb');
7
+
8
+ // Module-level cache — prevents re-importing on every upsertFeedback() call
9
+ // First ESM import takes ~200ms; second is instant from cache.
10
+ let _lancedb = null;
11
+ let _pipeline = null;
12
+ const TABLE_NAME = 'rlhf_memories';
13
+
14
+ async function getLanceDB() {
15
+ if (!_lancedb) {
16
+ _lancedb = await import('@lancedb/lancedb');
17
+ }
18
+ return _lancedb;
19
+ }
20
+
21
+ async function getEmbeddingPipeline() {
22
+ if (!_pipeline) {
23
+ const { pipeline } = await import('@huggingface/transformers');
24
+ _pipeline = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2', {
25
+ quantized: true,
26
+ });
27
+ }
28
+ return _pipeline;
29
+ }
30
+
31
+ // Stub embed support for unit tests — avoids HuggingFace ONNX model download.
32
+ // Set RLHF_VECTOR_STUB_EMBED=true to get a deterministic 384-dim unit vector.
33
+ // The real embed() is used in production and integration tests
34
+ // (gated by absence of this env var).
35
+ async function embed(text) {
36
+ if (process.env.RLHF_VECTOR_STUB_EMBED === 'true') {
37
+ // Deterministic 384-dim unit vector: first element = 1.0, rest = 0.0
38
+ const stub = Array(384).fill(0);
39
+ stub[0] = 1.0;
40
+ return stub;
41
+ }
42
+ const pipe = await getEmbeddingPipeline();
43
+ const output = await pipe(text, { pooling: 'mean', normalize: true });
44
+ return Array.from(output.data); // Float32Array -> plain number[] for LanceDB Arrow serialization
45
+ }
46
+
47
+ async function upsertFeedback(feedbackEvent) {
48
+ const lanceDir = process.env.RLHF_FEEDBACK_DIR
49
+ ? path.join(process.env.RLHF_FEEDBACK_DIR, 'lancedb')
50
+ : DEFAULT_LANCE_DIR;
51
+
52
+ const { connect } = await getLanceDB();
53
+ const db = await connect(lanceDir);
54
+
55
+ const textForEmbedding = [
56
+ feedbackEvent.context || '',
57
+ (feedbackEvent.tags || []).join(' '),
58
+ feedbackEvent.whatWentWrong || '',
59
+ feedbackEvent.whatWorked || '',
60
+ ].filter(Boolean).join('. ');
61
+
62
+ const vector = await embed(textForEmbedding);
63
+
64
+ const record = {
65
+ id: feedbackEvent.id,
66
+ text: textForEmbedding,
67
+ vector,
68
+ signal: feedbackEvent.signal,
69
+ tags: (feedbackEvent.tags || []).join(','),
70
+ timestamp: feedbackEvent.timestamp,
71
+ context: feedbackEvent.context || '',
72
+ };
73
+
74
+ const tableNames = await db.tableNames();
75
+ if (tableNames.includes(TABLE_NAME)) {
76
+ const table = await db.openTable(TABLE_NAME);
77
+ await table.add([record]);
78
+ } else {
79
+ await db.createTable(TABLE_NAME, [record]);
80
+ }
81
+ }
82
+
83
+ async function searchSimilar(queryText, limit = 5) {
84
+ const lanceDir = process.env.RLHF_FEEDBACK_DIR
85
+ ? path.join(process.env.RLHF_FEEDBACK_DIR, 'lancedb')
86
+ : DEFAULT_LANCE_DIR;
87
+
88
+ const { connect } = await getLanceDB();
89
+ const db = await connect(lanceDir);
90
+
91
+ const tableNames = await db.tableNames();
92
+ if (!tableNames.includes(TABLE_NAME)) return [];
93
+
94
+ const vector = await embed(queryText);
95
+ const table = await db.openTable(TABLE_NAME);
96
+ const results = await table.search(vector).limit(limit).toArray();
97
+ return results;
98
+ }
99
+
100
+ module.exports = { upsertFeedback, searchSimilar, TABLE_NAME };