claude-flow 3.65.1 → 3.66.1

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 (61) hide show
  1. package/.claude/helpers/hook-handler.cjs +66 -0
  2. package/.claude/helpers/learning-loop.cjs +594 -0
  3. package/node_modules/@claude-flow/codex/dist/cli.js +0 -0
  4. package/node_modules/@claude-flow/plugin-agent-federation/dist/application/policy-engine.d.ts.map +1 -1
  5. package/node_modules/@claude-flow/plugin-agent-federation/dist/application/policy-engine.js +31 -0
  6. package/node_modules/@claude-flow/plugin-agent-federation/dist/application/policy-engine.js.map +1 -1
  7. package/node_modules/@claude-flow/plugin-agent-federation/dist/bin.js +0 -0
  8. package/node_modules/@claude-flow/plugin-agent-federation/package.json +1 -1
  9. package/node_modules/@claude-flow/security/dist/input-validator.d.ts +6 -6
  10. package/package.json +2 -2
  11. package/v3/@claude-flow/cli/catalog-manifest.json +2 -2
  12. package/v3/@claude-flow/cli/dist/src/commands/hooks.js +4 -1
  13. package/v3/@claude-flow/cli/dist/src/init/executor.js +1 -1
  14. package/v3/@claude-flow/cli/dist/src/init/helper-refresh.js +4 -0
  15. package/v3/@claude-flow/cli/dist/src/mcp-tools/memory-tools.js +14 -0
  16. package/v3/@claude-flow/cli/dist/src/memory/memory-bridge.js +21 -1
  17. package/v3/@claude-flow/cli/dist/src/memory/sona-optimizer.d.ts +7 -0
  18. package/v3/@claude-flow/cli/dist/src/memory/sona-optimizer.js +36 -6
  19. package/v3/@claude-flow/cli/dist/src/memory/verified-lessons.d.ts +80 -0
  20. package/v3/@claude-flow/cli/dist/src/memory/verified-lessons.js +113 -0
  21. package/v3/@claude-flow/cli/dist/src/memory/verified-outcomes.d.ts +9 -47
  22. package/v3/@claude-flow/cli/dist/src/memory/verified-outcomes.js +11 -46
  23. package/v3/@claude-flow/cli/dist/src/missions/schemas.d.ts +122 -122
  24. package/v3/@claude-flow/cli/dist/src/mods/command-registry/schema.d.ts +189 -189
  25. package/v3/@claude-flow/cli/dist/src/update/rate-limiter.d.ts +6 -0
  26. package/v3/@claude-flow/cli/dist/src/update/rate-limiter.js +12 -1
  27. package/v3/@claude-flow/cli/package.json +4 -4
  28. package/v3/@claude-flow/shared/dist/core/config/schema.d.ts +96 -96
  29. package/node_modules/@claude-flow/plugin-agent-federation/dist/a2a/agent-card.d.ts +0 -130
  30. package/node_modules/@claude-flow/plugin-agent-federation/dist/a2a/consume.d.ts +0 -34
  31. package/node_modules/@claude-flow/plugin-agent-federation/dist/a2a/planner-descriptor.d.ts +0 -40
  32. package/node_modules/@claude-flow/plugin-agent-federation/dist/a2a/well-known.d.ts +0 -46
  33. package/node_modules/@claude-flow/plugin-agent-federation/dist/application/claim-checker.d.ts +0 -27
  34. package/node_modules/@claude-flow/plugin-agent-federation/dist/application/federation-breaker-service.d.ts +0 -192
  35. package/node_modules/@claude-flow/plugin-agent-federation/dist/application/federation-coordinator.d.ts +0 -211
  36. package/node_modules/@claude-flow/plugin-agent-federation/dist/application/inbound-dispatcher.d.ts +0 -113
  37. package/node_modules/@claude-flow/plugin-agent-federation/dist/application/policy-engine.d.ts +0 -41
  38. package/node_modules/@claude-flow/plugin-agent-federation/dist/application/spend-reporter.d.ts +0 -150
  39. package/node_modules/@claude-flow/plugin-agent-federation/dist/application/trust-evaluator.d.ts +0 -74
  40. package/node_modules/@claude-flow/plugin-agent-federation/dist/bin.d.ts +0 -13
  41. package/node_modules/@claude-flow/plugin-agent-federation/dist/cli-commands.d.ts +0 -7
  42. package/node_modules/@claude-flow/plugin-agent-federation/dist/domain/entities/federation-envelope.d.ts +0 -45
  43. package/node_modules/@claude-flow/plugin-agent-federation/dist/domain/entities/federation-node.d.ts +0 -104
  44. package/node_modules/@claude-flow/plugin-agent-federation/dist/domain/entities/federation-session.d.ts +0 -55
  45. package/node_modules/@claude-flow/plugin-agent-federation/dist/domain/entities/trust-level.d.ts +0 -17
  46. package/node_modules/@claude-flow/plugin-agent-federation/dist/domain/services/audit-service.d.ts +0 -69
  47. package/node_modules/@claude-flow/plugin-agent-federation/dist/domain/services/discovery-service.d.ts +0 -76
  48. package/node_modules/@claude-flow/plugin-agent-federation/dist/domain/services/handshake-service.d.ts +0 -62
  49. package/node_modules/@claude-flow/plugin-agent-federation/dist/domain/services/pii-pipeline-service.d.ts +0 -65
  50. package/node_modules/@claude-flow/plugin-agent-federation/dist/domain/services/routing-service.d.ts +0 -54
  51. package/node_modules/@claude-flow/plugin-agent-federation/dist/domain/services/wg-firewall-service.d.ts +0 -71
  52. package/node_modules/@claude-flow/plugin-agent-federation/dist/domain/services/wg-mesh-service.d.ts +0 -156
  53. package/node_modules/@claude-flow/plugin-agent-federation/dist/domain/services/wg-witness-service.d.ts +0 -98
  54. package/node_modules/@claude-flow/plugin-agent-federation/dist/domain/value-objects/federation-budget.d.ts +0 -89
  55. package/node_modules/@claude-flow/plugin-agent-federation/dist/domain/value-objects/federation-node-state.d.ts +0 -92
  56. package/node_modules/@claude-flow/plugin-agent-federation/dist/domain/value-objects/wg-config.d.ts +0 -70
  57. package/node_modules/@claude-flow/plugin-agent-federation/dist/index.d.ts +0 -24
  58. package/node_modules/@claude-flow/plugin-agent-federation/dist/mcp-tools.d.ts +0 -10
  59. package/node_modules/@claude-flow/plugin-agent-federation/dist/plugin.d.ts +0 -19
  60. package/node_modules/@claude-flow/plugin-agent-federation/dist/transport/midstream-aware-loader.d.ts +0 -72
  61. package/node_modules/@claude-flow/plugin-agent-federation/dist/version.d.ts +0 -3
@@ -26,20 +26,17 @@
26
26
  * it trains, and an anomaly gate holds suspicious patterns back from the
27
27
  * prompt. Every one of these can only downgrade.
28
28
  */
29
- import { type HoldReason, type LoopConfig, type PatternStats, type Taint } from './verified-guards.js';
29
+ import { type LoopConfig, type PatternStats, type Taint } from './verified-guards.js';
30
30
  import { type ReverifyRunner } from './verified-reverify.js';
31
+ import { type LessonsFile, type PatternExport } from './verified-lessons.js';
32
+ export { buildLessons, CAUTION_MIN_FAILURES, LESSON_GATE, lessonFile, lessonId, MAX_LESSONS, type Lesson, type LessonsFile } from './verified-lessons.js';
31
33
  export declare const VERIFIED_PATH: string;
32
34
  export declare const LEDGER_PATH: string;
33
35
  export declare const LESSONS_PATH: string;
34
- /** The SONA routing gate (sona-optimizer.ts `SONA_ROUTING_GATE`); a lesson below it never influences routing. */
35
- export declare const LESSON_GATE = 0.6;
36
36
  /** Ingested ids remembered; the mod keeps at most 500 lines, so this always covers the whole file. */
37
37
  export declare const MAX_LEDGER_IDS = 2000;
38
38
  /** A file past this many bytes is not read (the mod caps its own at far less). */
39
39
  export declare const MAX_FILE_BYTES: number;
40
- export declare const MAX_LESSONS = 20;
41
- /** Verified failures (and more failures than successes) before a pattern becomes a caution. */
42
- export declare const CAUTION_MIN_FAILURES = 2;
43
40
  export type Outcome = 'verified-success' | 'verified-failure' | 'unknown';
44
41
  export type VerifierKind = 'test' | 'build' | 'typecheck' | 'lint';
45
42
  export interface VerifiedRecord {
@@ -61,6 +58,8 @@ export interface VerifiedRecord {
61
58
  readonly taint: readonly Taint[];
62
59
  /** The mod process that recorded it, when the mod wrote one (ADR-503 §2.4). */
63
60
  readonly session: string | null;
61
+ /** ADR-511: files a verified turn edited, screened (`lessonFile`); empty for `unknown`. */
62
+ readonly files: readonly string[];
64
63
  readonly startedAt: number;
65
64
  readonly endedAt: number;
66
65
  }
@@ -84,6 +83,7 @@ export interface IngestLedger {
84
83
  evidence: string;
85
84
  at: number;
86
85
  stats: PatternStats;
86
+ files: Record<string, number>;
87
87
  }>;
88
88
  /** ADR-503 §2.3: the rotation cursor and the latest re-verifications (which commit, which check, what happened). */
89
89
  reverify: {
@@ -99,29 +99,6 @@ export interface ReverifyLogEntry {
99
99
  readonly at: number;
100
100
  }
101
101
  export declare const MAX_REVERIFY_LOG = 50;
102
- export interface Lesson {
103
- readonly id: string;
104
- readonly kind: 'lesson' | 'caution';
105
- readonly agent: string;
106
- readonly keywords: string[];
107
- readonly confidence: number;
108
- readonly successes: number;
109
- readonly failures: number;
110
- readonly evidence: string;
111
- }
112
- export interface LessonsFile {
113
- version: 1;
114
- gate: number;
115
- generatedAt: number;
116
- lessons: Lesson[];
117
- /** Verified patterns still below the gate, for the cold-start progress line. */
118
- belowGate: number;
119
- /** Patterns at or above the gate that the anomaly gate kept out of the prompt (ADR-503 §2.4). */
120
- held: Array<{
121
- id: string;
122
- reason: HoldReason;
123
- }>;
124
- }
125
102
  export interface IngestResult {
126
103
  /** `tainted`: successes whose taint the project did not allow, counted and never trained (ADR-503 §2.2). */
127
104
  ingested: {
@@ -166,35 +143,20 @@ export interface SonaLike {
166
143
  }>;
167
144
  duration?: number;
168
145
  feedback?: string;
146
+ /** ADR-511: the edited files, which decide only which pattern the outcome reinforces. */
147
+ anchors?: string[];
169
148
  }): {
170
149
  learned: boolean;
171
150
  patternKey: string;
172
151
  confidence: number;
173
152
  };
174
- exportPatterns(): Record<string, {
175
- keywords: string[];
176
- agent: string;
177
- confidence: number;
178
- successCount: number;
179
- failureCount: number;
180
- }>;
153
+ exportPatterns(): PatternExport;
181
154
  }
182
155
  /** One line of the mod's file, validated; anything malformed is null. Unknown fields never survive. */
183
156
  export declare function parseRecord(line: string): VerifiedRecord | null;
184
157
  export declare function emptyLedger(): IngestLedger;
185
158
  /** A ledger file read back; a corrupt or hostile one starts over (re-ingest is bounded by the mod's 500-line cap). */
186
159
  export declare function readLedger(raw: string | null): IngestLedger;
187
- /** A lesson's citation: a short hash of the SONA pattern key, so the key's own text never reaches a prompt. */
188
- export declare const lessonId: (patternKey: string) => string;
189
- /**
190
- * Builds the lessons file from SONA's patterns, keeping only the ones verified outcomes trained. A would-be lesson the
191
- * anomaly gate holds (ADR-503 §2.4) is listed under `held` instead: it stays in SONA, never in a prompt.
192
- */
193
- export declare function buildLessons(patterns: ReturnType<SonaLike['exportPatterns']>, verified: Record<string, {
194
- evidence: string;
195
- at: number;
196
- stats?: PatternStats;
197
- }>, now: number): LessonsFile;
198
160
  export interface IngestOptions {
199
161
  /** Project root; defaults to process.cwd(), where SONA keeps `.swarm/`. */
200
162
  root?: string;
@@ -26,23 +26,19 @@
26
26
  * it trains, and an anomaly gate holds suspicious patterns back from the
27
27
  * prompt. Every one of these can only downgrade.
28
28
  */
29
- import { createHash } from 'node:crypto';
30
29
  import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
31
30
  import { dirname, join } from 'node:path';
32
- import { addOutcome, blockedTaints, emptyStats, holdReason, parseStats, parseTaint, readLoopConfig } from './verified-guards.js';
31
+ import { addOutcome, blockedTaints, emptyStats, parseStats, parseTaint, readLoopConfig } from './verified-guards.js';
33
32
  import { gitWorktreeRunner, ReverifySession } from './verified-reverify.js';
33
+ import { addFiles, buildLessons, parseFileCounts, recordFiles } from './verified-lessons.js';
34
+ export { buildLessons, CAUTION_MIN_FAILURES, LESSON_GATE, lessonFile, lessonId, MAX_LESSONS } from './verified-lessons.js';
34
35
  export const VERIFIED_PATH = join('.claude-flow', 'data', 'verified-trajectories.jsonl');
35
36
  export const LEDGER_PATH = join('.claude-flow', 'data', 'verified-ingest.json');
36
37
  export const LESSONS_PATH = join('.claude-flow', 'data', 'learned-lessons.json');
37
- /** The SONA routing gate (sona-optimizer.ts `SONA_ROUTING_GATE`); a lesson below it never influences routing. */
38
- export const LESSON_GATE = 0.6;
39
38
  /** Ingested ids remembered; the mod keeps at most 500 lines, so this always covers the whole file. */
40
39
  export const MAX_LEDGER_IDS = 2000;
41
40
  /** A file past this many bytes is not read (the mod caps its own at far less). */
42
41
  export const MAX_FILE_BYTES = 2 * 1024 * 1024;
43
- export const MAX_LESSONS = 20;
44
- /** Verified failures (and more failures than successes) before a pattern becomes a caution. */
45
- export const CAUTION_MIN_FAILURES = 2;
46
42
  export const MAX_REVERIFY_LOG = 50;
47
43
  const OUTCOMES = ['verified-success', 'verified-failure', 'unknown'];
48
44
  const KINDS = ['test', 'build', 'typecheck', 'lint'];
@@ -92,7 +88,8 @@ export function parseRecord(line) {
92
88
  : [];
93
89
  const taint = outcome === 'verified-success' ? parseTaint(r.taint) : [];
94
90
  const session = typeof r.session === 'string' && /^[A-Za-z0-9_-]{1,40}$/.test(r.session) ? r.session : null;
95
- return { id: r.id, task, agent, outcome, verifier, steps, edits: num(r.edits, 10_000), taint, session, startedAt: num(r.startedAt), endedAt: num(r.endedAt) };
91
+ const files = outcome === 'unknown' ? [] : recordFiles(r.files);
92
+ return { id: r.id, task, agent, outcome, verifier, steps, edits: num(r.edits, 10_000), taint, session, files, startedAt: num(r.startedAt), endedAt: num(r.endedAt) };
96
93
  }
97
94
  export function emptyLedger() {
98
95
  return { version: 1, ids: [], lastMtimeMs: 0, totals: { verifiedSuccess: 0, verifiedFailure: 0, unknown: 0, rejected: 0, tainted: 0 }, patterns: {}, reverify: { cursor: 0, log: [] } };
@@ -110,7 +107,7 @@ export function readLedger(raw) {
110
107
  for (const [k, v] of Object.entries(j.patterns && typeof j.patterns === 'object' ? j.patterns : {})) {
111
108
  if (k === '__proto__' || typeof k !== 'string' || k.length > 400 || !v || typeof v !== 'object')
112
109
  continue;
113
- patterns[k] = { evidence: text(v.evidence, 80), at: num(v.at), stats: parseStats(v.stats) };
110
+ patterns[k] = { evidence: text(v.evidence, 80), at: num(v.at), stats: parseStats(v.stats), files: parseFileCounts(v.files) };
114
111
  }
115
112
  const rv = (j.reverify && typeof j.reverify === 'object' ? j.reverify : {});
116
113
  const STATUS = ['pass', 'fail', 'timeout', 'error'];
@@ -141,42 +138,6 @@ async function redact(task) {
141
138
  return task;
142
139
  }
143
140
  }
144
- /** Keywords a lesson may show: SONA keeps any non-separator text, the prompt gets only plain words. */
145
- const SAFE_KEYWORD = /^[a-z0-9][a-z0-9 _./-]{0,40}$/;
146
- /** A lesson's citation: a short hash of the SONA pattern key, so the key's own text never reaches a prompt. */
147
- export const lessonId = (patternKey) => `L-${createHash('sha256').update(patternKey).digest('hex').slice(0, 10)}`;
148
- /**
149
- * Builds the lessons file from SONA's patterns, keeping only the ones verified outcomes trained. A would-be lesson the
150
- * anomaly gate holds (ADR-503 §2.4) is listed under `held` instead: it stays in SONA, never in a prompt.
151
- */
152
- export function buildLessons(patterns, verified, now) {
153
- const lessons = [];
154
- const held = [];
155
- let belowGate = 0;
156
- for (const [id, p] of Object.entries(patterns)) {
157
- const v = verified[id];
158
- if (!v || !p || !Array.isArray(p.keywords))
159
- continue;
160
- const keywords = p.keywords.filter(k => typeof k === 'string' && SAFE_KEYWORD.test(k)).slice(0, 10);
161
- if (keywords.length === 0 || !AGENT.test(p.agent))
162
- continue;
163
- const base = { id: lessonId(id), agent: p.agent, keywords, confidence: Math.round(p.confidence * 1000) / 1000, successes: p.successCount, failures: p.failureCount, evidence: v.evidence };
164
- if (p.failureCount >= CAUTION_MIN_FAILURES && p.failureCount > p.successCount)
165
- lessons.push({ ...base, kind: 'caution' });
166
- else if (p.confidence >= LESSON_GATE) {
167
- const reason = holdReason(v.stats ?? emptyStats(), p.successCount);
168
- if (reason)
169
- held.push({ id: base.id, reason });
170
- else
171
- lessons.push({ ...base, kind: 'lesson' });
172
- }
173
- else
174
- belowGate++;
175
- }
176
- lessons.sort((a, b) => b.confidence - a.confidence || b.successes + b.failures - (a.successes + a.failures));
177
- held.sort((a, b) => a.id.localeCompare(b.id));
178
- return { version: 1, gate: LESSON_GATE, generatedAt: now, lessons: lessons.slice(0, MAX_LESSONS), belowGate, held: held.slice(0, MAX_LESSONS) };
179
- }
180
141
  function writeAtomic(path, body) {
181
142
  mkdirSync(dirname(path), { recursive: true });
182
143
  const tmp = `${path}.${process.pid}.tmp`;
@@ -299,12 +260,16 @@ export async function ingestVerifiedOutcomes(options = {}) {
299
260
  steps: record.steps.map(s => ({ action: s.tool, result: s.ok ? 'ok' : 'error', quality: s.ok ? 1 : 0, timestamp: new Date(record.endedAt || now).toISOString() })),
300
261
  duration: record.endedAt > record.startedAt ? record.endedAt - record.startedAt : 0,
301
262
  feedback: `verified by ${record.verifier.kind}: ${record.verifier.command}`,
263
+ // ADR-511: a failure anchors too, so a reworded failure lowers the same lesson; only a success names files.
264
+ anchors: [...record.files],
302
265
  });
303
266
  if (result.learned && result.patternKey) {
304
- const entry = ledger.patterns[result.patternKey] ?? { evidence: '', at: now, stats: emptyStats() };
267
+ const entry = ledger.patterns[result.patternKey] ?? { evidence: '', at: now, stats: emptyStats(), files: {} };
305
268
  entry.evidence = record.verifier.command;
306
269
  entry.at = now;
307
270
  addOutcome(entry.stats, success, record.taint, record.session);
271
+ if (success)
272
+ addFiles(entry.files, record.files);
308
273
  ledger.patterns[result.patternKey] = entry;
309
274
  }
310
275
  if (success)