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,364 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ /**
5
+ * prove-attribution.js — Phase 6 gate proof script.
6
+ *
7
+ * Generates proof/attribution-report.md and proof/attribution-report.json
8
+ * documenting per-requirement evidence for ATTR-01, ATTR-02, ATTR-03.
9
+ *
10
+ * Mirrors the prove-rlaif.js structure exactly (mkdtempSync / env override /
11
+ * execSync node --test / write JSON + markdown report).
12
+ *
13
+ * Exit 0 if no 'fail' statuses; exit 1 if any 'fail'.
14
+ */
15
+
16
+ const fs = require('fs');
17
+ const path = require('path');
18
+ const os = require('os');
19
+ const { execSync } = require('child_process');
20
+
21
+ const ROOT = path.join(__dirname, '..');
22
+ const PROOF_DIR = path.join(ROOT, 'proof');
23
+
24
+ // Phase 5 node-runner test baseline (before Phase 6 attribution tests)
25
+ const PHASE5_BASELINE = 142;
26
+
27
+ function ensureDir(dirPath) {
28
+ if (!fs.existsSync(dirPath)) {
29
+ fs.mkdirSync(dirPath, { recursive: true });
30
+ }
31
+ }
32
+
33
+ async function runProof() {
34
+ const report = {
35
+ phase: '06-feedback-attribution',
36
+ generated: new Date().toISOString(),
37
+ requirements: {},
38
+ summary: { passed: 0, failed: 0 },
39
+ };
40
+
41
+ function addResult(reqId, reqStatus, evidence) {
42
+ report.requirements[reqId] = { status: reqStatus, evidence };
43
+ if (reqStatus === 'pass') report.summary.passed += 1;
44
+ else report.summary.failed += 1;
45
+ }
46
+
47
+ // ─────────────────────────────────────────────────────────────────────────
48
+ // ATTR-01: recordAction + attributeFeedback correctness
49
+ // - require feedback-attribution.js with tmpDir env overrides
50
+ // - call recordAction('Bash', '{"command":"git push --force"}')
51
+ // - assert result.ok === true, result.action.intent === 'git-risk'
52
+ // - call attributeFeedback('negative', 'bad git push force broke main')
53
+ // - assert fs.existsSync(RLHF_FEEDBACK_ATTRIBUTIONS path)
54
+ // - parse JSONL, assert attribution_id and signal === 'negative'
55
+ // ─────────────────────────────────────────────────────────────────────────
56
+ const tmpDir01 = fs.mkdtempSync(path.join(os.tmpdir(), 'prove-attr01-'));
57
+ let attr01Status = 'fail';
58
+ let attr01Evidence = '';
59
+ try {
60
+ process.env.RLHF_ACTION_LOG = path.join(tmpDir01, 'action-log.jsonl');
61
+ process.env.RLHF_FEEDBACK_ATTRIBUTIONS = path.join(tmpDir01, 'feedback-attributions.jsonl');
62
+ process.env.RLHF_ATTRIBUTED_FEEDBACK = path.join(tmpDir01, 'attributed-feedback.jsonl');
63
+
64
+ // Invalidate module cache so env vars take effect
65
+ for (const key of Object.keys(require.cache)) {
66
+ if (key.includes('feedback-attribution')) {
67
+ delete require.cache[key];
68
+ }
69
+ }
70
+ const { recordAction, attributeFeedback } = require('./feedback-attribution');
71
+
72
+ // Test recordAction
73
+ const recResult = recordAction('Bash', '{"command":"git push --force"}');
74
+ const recOk = recResult.ok === true && recResult.action.intent === 'git-risk';
75
+ const actionLogExists = fs.existsSync(path.join(tmpDir01, 'action-log.jsonl'));
76
+
77
+ // Test attributeFeedback — negative signal should write attributions
78
+ const attrResult = attributeFeedback('negative', 'bad git push force broke main');
79
+ const attrOk = attrResult.ok === true;
80
+ const attributionsPath = path.join(tmpDir01, 'feedback-attributions.jsonl');
81
+ const attributionsExist = fs.existsSync(attributionsPath);
82
+
83
+ let attributionValid = false;
84
+ if (attributionsExist) {
85
+ const lines = fs.readFileSync(attributionsPath, 'utf8').trim().split('\n').filter(Boolean);
86
+ if (lines.length > 0) {
87
+ try {
88
+ const parsed = JSON.parse(lines[lines.length - 1]);
89
+ attributionValid = typeof parsed.attribution_id === 'string' &&
90
+ parsed.attribution_id.startsWith('att_') &&
91
+ parsed.signal === 'negative';
92
+ } catch (_) {
93
+ attributionValid = false;
94
+ }
95
+ }
96
+ }
97
+
98
+ if (recOk && actionLogExists && attrOk && attributionsExist && attributionValid) {
99
+ attr01Status = 'pass';
100
+ attr01Evidence =
101
+ `recordAction('Bash', git push --force) returned ok=true, intent=git-risk. ` +
102
+ `action-log.jsonl written to ${tmpDir01}. ` +
103
+ `action_id=${recResult.action.action_id}, risk_score=${recResult.action.risk_score}. ` +
104
+ `attributeFeedback('negative', ...) returned ok=true, attributedCount=${attrResult.attributedCount}. ` +
105
+ `feedback-attributions.jsonl written. attribution_id=${attrResult.attributionId || 'written'}, signal=negative. ` +
106
+ `Module: scripts/feedback-attribution.js. Pure offline JSONL-based attribution.`;
107
+ } else {
108
+ const issues = [];
109
+ if (!recOk) issues.push(`recordAction returned ok=${recResult.ok}, intent=${recResult.action ? recResult.action.intent : 'none'} (expected git-risk)`);
110
+ if (!actionLogExists) issues.push(`action-log.jsonl not written to ${tmpDir01}`);
111
+ if (!attrOk) issues.push(`attributeFeedback returned ok=${attrResult.ok}`);
112
+ if (!attributionsExist) issues.push(`feedback-attributions.jsonl not written`);
113
+ if (!attributionValid) issues.push(`attribution entry missing attribution_id or signal`);
114
+ attr01Status = 'fail';
115
+ attr01Evidence = `ATTR-01 smoke test failed: ${issues.join('; ')}`;
116
+ }
117
+ } catch (err) {
118
+ attr01Status = 'fail';
119
+ attr01Evidence = `ATTR-01 threw: ${err.message}`;
120
+ } finally {
121
+ try { fs.rmSync(tmpDir01, { recursive: true, force: true }); } catch (_) {}
122
+ delete process.env.RLHF_ACTION_LOG;
123
+ delete process.env.RLHF_FEEDBACK_ATTRIBUTIONS;
124
+ delete process.env.RLHF_ATTRIBUTED_FEEDBACK;
125
+ }
126
+ addResult('ATTR-01', attr01Status, attr01Evidence);
127
+
128
+ // ─────────────────────────────────────────────────────────────────────────
129
+ // ATTR-02: evaluatePretool allow/block/warn paths
130
+ // - require hybrid-feedback-context.js with tmpDir env overrides
131
+ // - seed attributed-feedback.jsonl with 3 negative entries for Bash + git push force
132
+ // - call buildHybridState() — should detect recurringNegativePatterns with count >= 3
133
+ // - call evaluatePretoolFromState(state, 'Bash', 'git push force main') → assert mode === 'block'
134
+ // - call evaluatePretoolFromState(state, 'Read', 'some-unrelated-file.md') → assert mode === 'allow'
135
+ // ─────────────────────────────────────────────────────────────────────────
136
+ const tmpDir02 = fs.mkdtempSync(path.join(os.tmpdir(), 'prove-attr02-'));
137
+ let attr02Status = 'fail';
138
+ let attr02Evidence = '';
139
+ try {
140
+ process.env.RLHF_FEEDBACK_LOG = path.join(tmpDir02, 'feedback-log.jsonl');
141
+ process.env.RLHF_ATTRIBUTED_FEEDBACK = path.join(tmpDir02, 'attributed-feedback.jsonl');
142
+ process.env.RLHF_GUARDS_PATH = path.join(tmpDir02, 'pretool-guards.json');
143
+
144
+ // Seed attributed-feedback.jsonl with 3 identical negative entries
145
+ const attrFeedbackPath = path.join(tmpDir02, 'attributed-feedback.jsonl');
146
+ const ts = new Date().toISOString();
147
+ const seedEntries = [
148
+ {
149
+ timestamp: ts,
150
+ signal: 'negative',
151
+ feedback: 'negative',
152
+ tool_name: 'Bash',
153
+ context: 'git push force main branch override',
154
+ source: 'attributed',
155
+ },
156
+ {
157
+ timestamp: ts,
158
+ signal: 'negative',
159
+ feedback: 'negative',
160
+ tool_name: 'Bash',
161
+ context: 'git push force main branch override',
162
+ source: 'attributed',
163
+ },
164
+ {
165
+ timestamp: ts,
166
+ signal: 'negative',
167
+ feedback: 'negative',
168
+ tool_name: 'Bash',
169
+ context: 'git push force main branch override',
170
+ source: 'attributed',
171
+ },
172
+ ];
173
+ fs.mkdirSync(path.dirname(attrFeedbackPath), { recursive: true });
174
+ fs.writeFileSync(
175
+ attrFeedbackPath,
176
+ seedEntries.map((e) => JSON.stringify(e)).join('\n') + '\n',
177
+ );
178
+
179
+ // Invalidate module cache
180
+ for (const key of Object.keys(require.cache)) {
181
+ if (key.includes('hybrid-feedback-context')) {
182
+ delete require.cache[key];
183
+ }
184
+ }
185
+ const { buildHybridState, evaluatePretoolFromState } = require('./hybrid-feedback-context');
186
+
187
+ const state = buildHybridState({
188
+ feedbackLogPath: path.join(tmpDir02, 'feedback-log.jsonl'),
189
+ attributedFeedbackPath: attrFeedbackPath,
190
+ });
191
+
192
+ const hasRecurring = state.recurringNegativePatterns.length > 0;
193
+ const topCount = hasRecurring ? state.recurringNegativePatterns[0].count : 0;
194
+ const countOk = topCount >= 3;
195
+
196
+ // block path: git push force matches pattern
197
+ const blockResult = evaluatePretoolFromState(state, 'Bash', 'git push force main');
198
+ const blockOk = blockResult.mode === 'block';
199
+
200
+ // allow path: completely different tool+input
201
+ const allowResult = evaluatePretoolFromState(state, 'Read', 'some-unrelated-file.md');
202
+ const allowOk = allowResult.mode === 'allow';
203
+
204
+ if (hasRecurring && countOk && blockOk && allowOk) {
205
+ attr02Status = 'pass';
206
+ attr02Evidence =
207
+ `buildHybridState() detected ${state.recurringNegativePatterns.length} recurring pattern(s). ` +
208
+ `Top pattern count=${topCount} (>= 3 → critical). ` +
209
+ `evaluatePretoolFromState('Bash', 'git push force main') → mode=${blockResult.mode}. ` +
210
+ `evaluatePretoolFromState('Read', 'some-unrelated-file.md') → mode=${allowResult.mode}. ` +
211
+ `block + allow paths verified. No false positive for unrelated Read tool. ` +
212
+ `Module: scripts/hybrid-feedback-context.js. hasTwoKeywordHits enforces no-false-positive invariant.`;
213
+ } else {
214
+ const issues = [];
215
+ if (!hasRecurring) issues.push(`no recurring negative patterns detected (expected >= 1 from 3 identical entries)`);
216
+ if (!countOk) issues.push(`top pattern count=${topCount} (expected >= 3)`);
217
+ if (!blockOk) issues.push(`block path returned mode=${blockResult.mode} (expected block)`);
218
+ if (!allowOk) issues.push(`allow path returned mode=${allowResult.mode} (expected allow)`);
219
+ attr02Status = 'fail';
220
+ attr02Evidence = `ATTR-02 smoke test failed: ${issues.join('; ')}`;
221
+ }
222
+ } catch (err) {
223
+ attr02Status = 'fail';
224
+ attr02Evidence = `ATTR-02 threw: ${err.message}`;
225
+ } finally {
226
+ try { fs.rmSync(tmpDir02, { recursive: true, force: true }); } catch (_) {}
227
+ delete process.env.RLHF_FEEDBACK_LOG;
228
+ delete process.env.RLHF_ATTRIBUTED_FEEDBACK;
229
+ delete process.env.RLHF_GUARDS_PATH;
230
+ }
231
+ addResult('ATTR-02', attr02Status, attr02Evidence);
232
+
233
+ // ─────────────────────────────────────────────────────────────────────────
234
+ // ATTR-03: node --test on both attribution test files exits 0
235
+ // execSync('node --test tests/feedback-attribution.test.js tests/hybrid-feedback-context.test.js')
236
+ // Parse stdout to count passing tests vs baseline
237
+ // status: 'pass' if exit code 0 and pass count >= 1
238
+ // ─────────────────────────────────────────────────────────────────────────
239
+ let attr03Status = 'fail';
240
+ let attr03Evidence = '';
241
+ let attrPassCount = 0;
242
+ let attrFailCount = 0;
243
+ try {
244
+ const testOutput = execSync(
245
+ 'node --test tests/feedback-attribution.test.js tests/hybrid-feedback-context.test.js 2>&1',
246
+ { cwd: ROOT, timeout: 60000, encoding: 'utf-8' },
247
+ );
248
+
249
+ const passMatch = testOutput.match(/pass\s+(\d+)/);
250
+ const failMatch = testOutput.match(/fail\s+(\d+)/);
251
+ attrPassCount = passMatch ? parseInt(passMatch[1], 10) : 0;
252
+ attrFailCount = failMatch ? parseInt(failMatch[1], 10) : 0;
253
+
254
+ const meetsRequirement = attrPassCount >= 1 && attrFailCount === 0;
255
+
256
+ if (meetsRequirement) {
257
+ attr03Status = 'pass';
258
+ attr03Evidence =
259
+ `node --test (2 attribution test files): pass=${attrPassCount}, fail=${attrFailCount}. ` +
260
+ `Phase 5 baseline (test:api + test:proof + test:rlaif): ${PHASE5_BASELINE} tests. ` +
261
+ `Phase 6 adds ${attrPassCount} new attribution tests. ` +
262
+ `Total with attribution: ${PHASE5_BASELINE + attrPassCount} tests (node-runner only). ` +
263
+ `Files: tests/feedback-attribution.test.js (recordAction, attributeFeedback), ` +
264
+ `tests/hybrid-feedback-context.test.js (evaluatePretool, buildHybridState, compileGuardArtifact). ` +
265
+ `All tests use fs.mkdtempSync() tmpdir isolation — zero production feedback dirs touched.`;
266
+ } else {
267
+ attr03Status = 'fail';
268
+ attr03Evidence =
269
+ `node --test attribution files: pass=${attrPassCount}, fail=${attrFailCount}. ` +
270
+ `Expected >= 1 passing and 0 failures. ` +
271
+ `${attrFailCount > 0 ? `${attrFailCount} test(s) failing.` : `Only ${attrPassCount} tests passing (need >= 1).`}`;
272
+ }
273
+ } catch (err) {
274
+ const output = err.stdout || err.stderr || err.message || '';
275
+ const outStr = String(output);
276
+ const passMatch = outStr.match(/pass\s+(\d+)/);
277
+ const failMatch = outStr.match(/fail\s+(\d+)/);
278
+ attrPassCount = passMatch ? parseInt(passMatch[1], 10) : 0;
279
+ attrFailCount = failMatch ? parseInt(failMatch[1], 10) : 1;
280
+ attr03Status = 'fail';
281
+ attr03Evidence = `node --test attribution files exited non-zero (${attrFailCount} failures). Output: ${outStr.slice(0, 500)}`;
282
+ }
283
+ addResult('ATTR-03', attr03Status, attr03Evidence);
284
+
285
+ // ─────────────────────────────────────────────────────────────────────────
286
+ // Write proof artifacts
287
+ // ─────────────────────────────────────────────────────────────────────────
288
+ ensureDir(PROOF_DIR);
289
+
290
+ const jsonPath = path.join(PROOF_DIR, 'attribution-report.json');
291
+ fs.writeFileSync(jsonPath, `${JSON.stringify(report, null, 2)}\n`);
292
+
293
+ const mdLines = [
294
+ '# Feedback Attribution — Proof Report',
295
+ '',
296
+ `Generated: ${report.generated}`,
297
+ `Phase: ${report.phase}`,
298
+ '',
299
+ `**Passed: ${report.summary.passed} | Failed: ${report.summary.failed}**`,
300
+ '',
301
+ '## Requirements',
302
+ '',
303
+ '| Requirement | Status | Evidence |',
304
+ '|-------------|--------|----------|',
305
+ ...Object.entries(report.requirements).map(
306
+ ([reqId, { status: s, evidence }]) =>
307
+ `| ${reqId} | ${s.toUpperCase()} | ${evidence.replace(/\|/g, '\\|').replace(/\n/g, ' ')} |`
308
+ ),
309
+ '',
310
+ '## Requirement Details',
311
+ '',
312
+ ];
313
+
314
+ for (const [reqId, { status: s, evidence }] of Object.entries(report.requirements)) {
315
+ mdLines.push(`### ${reqId} — ${s.toUpperCase()}`);
316
+ mdLines.push('');
317
+ mdLines.push(evidence);
318
+ mdLines.push('');
319
+ }
320
+
321
+ mdLines.push('## Test Count Delta');
322
+ mdLines.push('');
323
+ mdLines.push('| Baseline (Phase 5 final) | Phase 6 Attribution Addition | Total (node-runner) |');
324
+ mdLines.push('|--------------------------|------------------------------|---------------------|');
325
+ mdLines.push(`| ${PHASE5_BASELINE} tests | +${attrPassCount} attribution tests (2 test files) | ${PHASE5_BASELINE + attrPassCount} |`);
326
+ mdLines.push('');
327
+ mdLines.push('Phase 6 (plan-03) added attribution test coverage:');
328
+ mdLines.push('- `tests/feedback-attribution.test.js` — recordAction(), attributeFeedback() (5 tests)');
329
+ mdLines.push('- `tests/hybrid-feedback-context.test.js` — evaluatePretool, buildHybridState, compileGuardArtifact (16 tests)');
330
+ mdLines.push('');
331
+ mdLines.push('All tests use `fs.mkdtempSync()` tmpdir isolation. Zero production feedback dirs touched.');
332
+ mdLines.push('');
333
+ mdLines.push('## Summary');
334
+ mdLines.push('');
335
+ mdLines.push(`${report.summary.passed}/3 requirements passed.`);
336
+ mdLines.push('');
337
+
338
+ const mdPath = path.join(PROOF_DIR, 'attribution-report.md');
339
+ fs.writeFileSync(mdPath, `${mdLines.join('\n')}\n`);
340
+
341
+ console.log(`Proof written to ${mdPath}`);
342
+ console.log(` and ${jsonPath}`);
343
+ console.log('');
344
+ console.log(JSON.stringify(report.summary, null, 2));
345
+
346
+ const hasFail = report.summary.failed > 0;
347
+ if (hasFail) {
348
+ process.exitCode = 1;
349
+ console.error('\nFAIL — one or more requirements did not pass. See proof/attribution-report.md for details.');
350
+ } else {
351
+ console.log('\nPASS — all requirements satisfied.');
352
+ }
353
+
354
+ return report;
355
+ }
356
+
357
+ module.exports = { runProof };
358
+
359
+ if (require.main === module) {
360
+ runProof().catch((err) => {
361
+ console.error('Fatal error in prove-attribution.js:', err);
362
+ process.exitCode = 1;
363
+ });
364
+ }