@claude-flow/cli 3.32.34 → 3.32.35

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 (31) hide show
  1. package/.claude/helpers/helpers.manifest.json +2 -2
  2. package/catalog-manifest.json +4 -4
  3. package/dist/src/commands/agent.js +49 -1
  4. package/dist/src/commands/hooks.js +24 -0
  5. package/dist/src/commands/metaharness.js +4 -0
  6. package/dist/src/commands/swarm.js +61 -5
  7. package/dist/src/config-adapter.js +1 -0
  8. package/dist/src/index.js +3 -2
  9. package/dist/src/mcp-tools/agent-tools.js +10 -0
  10. package/dist/src/mcp-tools/hooks-tools.js +86 -52
  11. package/dist/src/mcp-tools/memory-tools.js +14 -2
  12. package/dist/src/mcp-tools/metaharness-tools.js +7 -0
  13. package/dist/src/mcp-tools/swarm-tools.d.ts +16 -0
  14. package/dist/src/mcp-tools/swarm-tools.js +172 -7
  15. package/dist/src/memory/memory-initializer.d.ts +9 -1
  16. package/dist/src/memory/memory-initializer.js +66 -19
  17. package/dist/src/services/daemon-autostart.js +7 -4
  18. package/dist/src/services/flywheel-receipt.d.ts +3 -0
  19. package/dist/src/services/flywheel-receipt.js +1 -0
  20. package/dist/src/services/harness-flywheel-runtime.d.ts +6 -0
  21. package/dist/src/services/harness-flywheel-runtime.js +19 -19
  22. package/dist/src/services/harness-flywheel.d.ts +2 -0
  23. package/dist/src/services/harness-flywheel.js +1 -0
  24. package/dist/src/services/harness-project-anchor.d.ts +23 -0
  25. package/dist/src/services/harness-project-anchor.js +129 -0
  26. package/dist/src/services/learned-routing.d.ts +34 -0
  27. package/dist/src/services/learned-routing.js +85 -0
  28. package/dist/src/services/pheromone-adaptive.d.ts +71 -0
  29. package/dist/src/services/pheromone-adaptive.js +214 -0
  30. package/dist/src/services/worker-daemon.js +4 -1
  31. package/package.json +1 -1
@@ -4,13 +4,16 @@
4
4
  * Tool definitions for swarm coordination with file-based state persistence.
5
5
  * Replaces previous stub implementations with real state tracking.
6
6
  */
7
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
7
+ import { closeSync, existsSync, mkdirSync, openSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync, } from 'node:fs';
8
8
  import { join } from 'node:path';
9
9
  import { getProjectCwd } from './types.js';
10
10
  import { validateIdentifier } from './validate-input.js';
11
+ import { createApscState, isApscAgentEligible, normalizeApscConfig, recordApscSignal, } from '../services/pheromone-adaptive.js';
11
12
  // Swarm state persistence
12
13
  const SWARM_DIR = '.claude-flow/swarm';
13
14
  const SWARM_STATE_FILE = 'swarm-state.json';
15
+ const SWARM_STATE_LOCK = 'swarm-state.lock';
16
+ const SWARM_LOCK_STALE_MS = 10_000;
14
17
  function getSwarmDir() {
15
18
  return join(getProjectCwd(), SWARM_DIR);
16
19
  }
@@ -103,12 +106,101 @@ export function loadSwarmStore() {
103
106
  }
104
107
  export function saveSwarmStore(store) {
105
108
  ensureSwarmDir();
106
- writeFileSync(getSwarmStatePath(), JSON.stringify(store, null, 2), 'utf-8');
109
+ const target = getSwarmStatePath();
110
+ const temporary = `${target}.${process.pid}.${Math.random().toString(36).slice(2)}.tmp`;
111
+ try {
112
+ writeFileSync(temporary, JSON.stringify(store, null, 2), { encoding: 'utf-8', mode: 0o600 });
113
+ renameSync(temporary, target);
114
+ }
115
+ catch (error) {
116
+ try {
117
+ unlinkSync(temporary);
118
+ }
119
+ catch { /* already renamed or never created */ }
120
+ throw error;
121
+ }
122
+ }
123
+ function withSwarmStoreLock(operation) {
124
+ ensureSwarmDir();
125
+ const lockPath = join(getSwarmDir(), SWARM_STATE_LOCK);
126
+ let descriptor;
127
+ const waiter = new Int32Array(new SharedArrayBuffer(4));
128
+ for (let attempt = 0; attempt < 100; attempt++) {
129
+ try {
130
+ descriptor = openSync(lockPath, 'wx', 0o600);
131
+ break;
132
+ }
133
+ catch (error) {
134
+ if (error.code !== 'EEXIST')
135
+ throw error;
136
+ try {
137
+ if (Date.now() - statSync(lockPath).mtimeMs > SWARM_LOCK_STALE_MS) {
138
+ unlinkSync(lockPath);
139
+ continue;
140
+ }
141
+ }
142
+ catch { /* another writer released it */ }
143
+ Atomics.wait(waiter, 0, 0, 10);
144
+ }
145
+ }
146
+ if (descriptor === undefined)
147
+ throw new Error('swarm state is busy; retry the outcome update');
148
+ try {
149
+ return operation();
150
+ }
151
+ finally {
152
+ try {
153
+ closeSync(descriptor);
154
+ }
155
+ catch { /* best effort */ }
156
+ try {
157
+ unlinkSync(lockPath);
158
+ }
159
+ catch { /* best effort */ }
160
+ }
107
161
  }
108
162
  // Input validation
109
163
  const VALID_TOPOLOGIES = new Set([
110
- 'hierarchical', 'mesh', 'hierarchical-mesh', 'ring', 'star', 'hybrid', 'adaptive',
164
+ 'hierarchical', 'mesh', 'hierarchical-mesh', 'ring', 'star', 'hybrid', 'adaptive', 'pheromone-adaptive',
111
165
  ]);
166
+ function latestRunningSwarm(store) {
167
+ return Object.values(store.swarms)
168
+ .filter((swarm) => swarm.status === 'running')
169
+ .sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime())[0];
170
+ }
171
+ function apscStateOf(swarm) {
172
+ if (!swarm || swarm.topology !== 'pheromone-adaptive')
173
+ return undefined;
174
+ const state = swarm.config.apscState;
175
+ return state?.schemaVersion ? state : undefined;
176
+ }
177
+ /** Shared by hooks_post-task so outcome labels immediately drive APSC. */
178
+ export function recordSwarmPheromoneSignal(signal) {
179
+ return withSwarmStoreLock(() => {
180
+ const store = loadSwarmStore();
181
+ const swarm = latestRunningSwarm(store);
182
+ const current = apscStateOf(swarm);
183
+ if (!swarm || !current)
184
+ return { active: false, reason: 'no running pheromone-adaptive swarm' };
185
+ const result = recordApscSignal(current, signal);
186
+ swarm.config.apscState = result.state;
187
+ swarm.updatedAt = new Date().toISOString();
188
+ saveSwarmStore(store);
189
+ return { active: true, swarmId: swarm.swarmId, decision: result.decision };
190
+ });
191
+ }
192
+ /** Scheduling gate used by agent_execute. Dry-run topologies remain eligible. */
193
+ export function pheromoneAgentEligibility(agentId) {
194
+ const current = apscStateOf(latestRunningSwarm(loadSwarmStore()));
195
+ if (!current)
196
+ return { eligible: true, active: false };
197
+ const eligible = isApscAgentEligible(current, agentId);
198
+ return {
199
+ eligible,
200
+ active: true,
201
+ reason: eligible ? undefined : 'agent suspended by pheromone-adaptive scheduling gate',
202
+ };
203
+ }
112
204
  export const swarmTools = [
113
205
  {
114
206
  name: 'swarm_init',
@@ -117,7 +209,7 @@ export const swarmTools = [
117
209
  inputSchema: {
118
210
  type: 'object',
119
211
  properties: {
120
- topology: { type: 'string', description: 'Swarm topology type (hierarchical, mesh, hierarchical-mesh, ring, star, hybrid, adaptive)' },
212
+ topology: { type: 'string', description: 'Swarm topology type (hierarchical, mesh, hierarchical-mesh, ring, star, hybrid, adaptive, pheromone-adaptive)' },
121
213
  maxAgents: { type: 'number', description: 'Maximum number of agents (1-50)' },
122
214
  strategy: { type: 'string', description: 'Agent strategy (specialized, balanced, adaptive)' },
123
215
  config: { type: 'object', description: 'Additional swarm configuration' },
@@ -145,6 +237,19 @@ export const swarmTools = [
145
237
  error: `Invalid topology: ${topology}. Valid: ${[...VALID_TOPOLOGIES].join(', ')}`,
146
238
  };
147
239
  }
240
+ let apscState;
241
+ if (topology === 'pheromone-adaptive') {
242
+ try {
243
+ const apscConfig = normalizeApscConfig((config.apsc ?? {}));
244
+ if (apscConfig.minActiveAgents > maxAgents) {
245
+ return { success: false, error: 'APSC minActiveAgents cannot exceed maxAgents' };
246
+ }
247
+ apscState = createApscState(apscConfig);
248
+ }
249
+ catch (error) {
250
+ return { success: false, error: `Invalid APSC config: ${error.message}` };
251
+ }
252
+ }
148
253
  const swarmId = `swarm-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
149
254
  const now = new Date().toISOString();
150
255
  const swarmState = {
@@ -161,12 +266,14 @@ export const swarmTools = [
161
266
  communicationProtocol: config.communicationProtocol || 'message-bus',
162
267
  autoScaling: config.autoScaling ?? true,
163
268
  consensusMechanism: config.consensusMechanism || 'majority',
269
+ ...(apscState ? { apscState } : {}),
164
270
  },
165
271
  createdAt: now,
166
272
  updatedAt: now,
167
- // #1799 record host PID so subsequent loads can detect orphans
168
- // when this process exits without a graceful swarm_shutdown.
169
- pid: process.pid,
273
+ // A normal CLI invocation exits immediately after creating this
274
+ // coordination record; its PID is therefore not the swarm lifetime.
275
+ // Long-lived hosts may opt into PID ownership explicitly.
276
+ pid: config.trackHostProcess === true ? process.pid : undefined,
170
277
  };
171
278
  const store = loadSwarmStore();
172
279
  store.swarms[swarmId] = swarmState;
@@ -180,6 +287,62 @@ export const swarmTools = [
180
287
  initializedAt: now,
181
288
  config: swarmState.config,
182
289
  persisted: true,
290
+ apsc: apscState ? {
291
+ dryRun: apscState.config.dryRun,
292
+ minActiveAgents: apscState.config.minActiveAgents,
293
+ minSamples: apscState.config.minSamples,
294
+ } : undefined,
295
+ };
296
+ },
297
+ },
298
+ {
299
+ name: 'swarm_pheromone_update',
300
+ description: 'Record a normalized per-agent task outcome for a running pheromone-adaptive swarm. Use when a static agent pool is wrong because repeated task evidence should update role-aware EMA fitness and produce a bounded keep/suspend/reactivate decision; native Task has no persistent swarm eligibility gate.',
301
+ category: 'swarm',
302
+ inputSchema: {
303
+ type: 'object',
304
+ properties: {
305
+ agentId: { type: 'string', description: 'Stable tracked agent identifier' },
306
+ role: { type: 'string', description: 'Agent role used for role-local normalization' },
307
+ taskSuccess: { type: 'number', description: 'Observed task success in [0,1]' },
308
+ normalizedLatency: { type: 'number', description: 'Latency divided by the configured budget, clamped to [0,1]' },
309
+ consensusAlignment: { type: 'number', description: 'Agreement with the accepted consensus in [0,1]' },
310
+ },
311
+ required: ['agentId', 'role', 'taskSuccess', 'normalizedLatency', 'consensusAlignment'],
312
+ },
313
+ handler: async (input) => {
314
+ try {
315
+ return recordSwarmPheromoneSignal({
316
+ agentId: String(input.agentId),
317
+ role: String(input.role),
318
+ taskSuccess: Number(input.taskSuccess),
319
+ normalizedLatency: Number(input.normalizedLatency),
320
+ consensusAlignment: Number(input.consensusAlignment),
321
+ });
322
+ }
323
+ catch (error) {
324
+ return { active: false, reason: error.message };
325
+ }
326
+ },
327
+ },
328
+ {
329
+ name: 'swarm_pheromone_status',
330
+ description: 'Inspect the threshold, safety configuration, per-agent EMA scores, and scheduling eligibility of the active pheromone-adaptive swarm. Use when aggregate swarm status is wrong because calibration requires the exact APSC threshold and per-agent evidence before switching from dry-run to live suspension.',
331
+ category: 'swarm',
332
+ inputSchema: { type: 'object', properties: {} },
333
+ handler: async () => {
334
+ const store = loadSwarmStore();
335
+ const swarm = latestRunningSwarm(store);
336
+ const state = apscStateOf(swarm);
337
+ if (!swarm || !state)
338
+ return { active: false, reason: 'no running pheromone-adaptive swarm' };
339
+ return {
340
+ active: true,
341
+ swarmId: swarm.swarmId,
342
+ threshold: state.threshold,
343
+ round: state.round,
344
+ config: state.config,
345
+ agents: Object.values(state.agents).sort((a, b) => a.agentId.localeCompare(b.agentId)),
183
346
  };
184
347
  },
185
348
  },
@@ -212,6 +375,7 @@ export const swarmTools = [
212
375
  agentCount: swarm.agents.length,
213
376
  taskCount: swarm.tasks.length,
214
377
  config: swarm.config,
378
+ apsc: apscStateOf(swarm),
215
379
  createdAt: swarm.createdAt,
216
380
  updatedAt: swarm.updatedAt,
217
381
  };
@@ -236,6 +400,7 @@ export const swarmTools = [
236
400
  agentCount: latest.agents.length,
237
401
  taskCount: latest.tasks.length,
238
402
  config: latest.config,
403
+ apsc: apscStateOf(latest),
239
404
  createdAt: latest.createdAt,
240
405
  updatedAt: latest.updatedAt,
241
406
  totalSwarms: swarmIds.length,
@@ -36,7 +36,7 @@ export declare function resolveDbPath(cliFlag?: string): string;
36
36
  * Vector embeddings enabled for semantic search
37
37
  */
38
38
  export declare const MEMORY_SCHEMA_V3 = "\n-- RuFlo V3 Memory Database\n-- Version: 3.0.0\n-- Features: Pattern learning, vector embeddings, temporal decay, migration tracking\n\nPRAGMA journal_mode = WAL;\nPRAGMA synchronous = NORMAL;\nPRAGMA foreign_keys = ON;\n\n-- ============================================\n-- CORE MEMORY TABLES\n-- ============================================\n\n-- Memory entries (main storage)\nCREATE TABLE IF NOT EXISTS memory_entries (\n id TEXT PRIMARY KEY,\n key TEXT NOT NULL,\n namespace TEXT DEFAULT 'default',\n content TEXT NOT NULL,\n type TEXT DEFAULT 'semantic' CHECK(type IN ('semantic', 'episodic', 'procedural', 'working', 'pattern')),\n\n -- Vector embedding for semantic search (stored as JSON array)\n embedding TEXT,\n embedding_model TEXT DEFAULT 'local',\n embedding_dimensions INTEGER,\n\n -- Metadata\n tags TEXT, -- JSON array\n metadata TEXT, -- JSON object\n owner_id TEXT,\n\n -- ADR-323: who/what produced this entry \u2014 lets shared-namespace retrieval\n -- filter by trust level instead of conflating a user's stated claim with\n -- an agent's own output or a raw tool/system observation.\n provenance_type TEXT DEFAULT 'unknown' CHECK(provenance_type IN (\n 'user_claim', 'agent_output', 'system_observation', 'tool_result', 'unknown'\n )),\n\n -- Timestamps\n created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),\n updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),\n expires_at INTEGER,\n last_accessed_at INTEGER,\n\n -- Access tracking for hot/cold detection\n access_count INTEGER DEFAULT 0,\n\n -- Status\n status TEXT DEFAULT 'active' CHECK(status IN ('active', 'archived', 'deleted')),\n\n UNIQUE(namespace, key)\n);\n\n-- Indexes for memory entries\nCREATE INDEX IF NOT EXISTS idx_memory_namespace ON memory_entries(namespace);\nCREATE INDEX IF NOT EXISTS idx_memory_key ON memory_entries(key);\nCREATE INDEX IF NOT EXISTS idx_memory_type ON memory_entries(type);\nCREATE INDEX IF NOT EXISTS idx_memory_status ON memory_entries(status);\nCREATE INDEX IF NOT EXISTS idx_memory_created ON memory_entries(created_at);\nCREATE INDEX IF NOT EXISTS idx_memory_accessed ON memory_entries(last_accessed_at);\nCREATE INDEX IF NOT EXISTS idx_memory_owner ON memory_entries(owner_id);\n\n-- ============================================\n-- PATTERN LEARNING TABLES\n-- ============================================\n\n-- Learned patterns with confidence scoring and versioning\nCREATE TABLE IF NOT EXISTS patterns (\n id TEXT PRIMARY KEY,\n\n -- Pattern identification\n name TEXT NOT NULL,\n pattern_type TEXT NOT NULL CHECK(pattern_type IN (\n 'task-routing', 'error-recovery', 'optimization', 'learning',\n 'coordination', 'prediction', 'code-pattern', 'workflow'\n )),\n\n -- Pattern definition\n condition TEXT NOT NULL, -- Regex or semantic match\n action TEXT NOT NULL, -- What to do when pattern matches\n description TEXT,\n\n -- Confidence scoring (0.0 - 1.0)\n confidence REAL DEFAULT 0.5,\n success_count INTEGER DEFAULT 0,\n failure_count INTEGER DEFAULT 0,\n\n -- Temporal decay\n decay_rate REAL DEFAULT 0.01, -- How fast confidence decays\n half_life_days INTEGER DEFAULT 30, -- Days until confidence halves without use\n\n -- Vector embedding for semantic pattern matching\n embedding TEXT,\n embedding_dimensions INTEGER,\n\n -- Versioning\n version INTEGER DEFAULT 1,\n parent_id TEXT REFERENCES patterns(id),\n\n -- Metadata\n tags TEXT, -- JSON array\n metadata TEXT, -- JSON object\n source TEXT, -- Where the pattern was learned from\n\n -- Timestamps\n created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),\n updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),\n last_matched_at INTEGER,\n last_success_at INTEGER,\n last_failure_at INTEGER,\n\n -- Status\n status TEXT DEFAULT 'active' CHECK(status IN ('active', 'archived', 'deprecated', 'experimental'))\n);\n\n-- Indexes for patterns\nCREATE INDEX IF NOT EXISTS idx_patterns_type ON patterns(pattern_type);\nCREATE INDEX IF NOT EXISTS idx_patterns_confidence ON patterns(confidence DESC);\nCREATE INDEX IF NOT EXISTS idx_patterns_status ON patterns(status);\nCREATE INDEX IF NOT EXISTS idx_patterns_last_matched ON patterns(last_matched_at);\n\n-- Pattern evolution history (for versioning)\nCREATE TABLE IF NOT EXISTS pattern_history (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n pattern_id TEXT NOT NULL REFERENCES patterns(id),\n version INTEGER NOT NULL,\n\n -- Snapshot of pattern state\n confidence REAL,\n success_count INTEGER,\n failure_count INTEGER,\n condition TEXT,\n action TEXT,\n\n -- What changed\n change_type TEXT CHECK(change_type IN ('created', 'updated', 'success', 'failure', 'decay', 'merged', 'split')),\n change_reason TEXT,\n\n created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)\n);\n\nCREATE INDEX IF NOT EXISTS idx_pattern_history_pattern ON pattern_history(pattern_id);\n\n-- ============================================\n-- LEARNING & TRAJECTORY TABLES\n-- ============================================\n\n-- Learning trajectories (SONA integration)\nCREATE TABLE IF NOT EXISTS trajectories (\n id TEXT PRIMARY KEY,\n session_id TEXT,\n\n -- Trajectory state\n status TEXT DEFAULT 'active' CHECK(status IN ('active', 'completed', 'failed', 'abandoned')),\n verdict TEXT CHECK(verdict IN ('success', 'failure', 'partial', NULL)),\n\n -- Context\n task TEXT,\n context TEXT, -- JSON object\n\n -- Metrics\n total_steps INTEGER DEFAULT 0,\n total_reward REAL DEFAULT 0,\n\n -- Timestamps\n started_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),\n ended_at INTEGER,\n\n -- Reference to extracted pattern (if any)\n extracted_pattern_id TEXT REFERENCES patterns(id)\n);\n\n-- Trajectory steps\nCREATE TABLE IF NOT EXISTS trajectory_steps (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n trajectory_id TEXT NOT NULL REFERENCES trajectories(id),\n step_number INTEGER NOT NULL,\n\n -- Step data\n action TEXT NOT NULL,\n observation TEXT,\n reward REAL DEFAULT 0,\n\n -- Metadata\n metadata TEXT, -- JSON object\n\n created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)\n);\n\nCREATE INDEX IF NOT EXISTS idx_steps_trajectory ON trajectory_steps(trajectory_id);\n\n-- ============================================\n-- MIGRATION STATE TRACKING\n-- ============================================\n\n-- Migration state (for resume capability)\nCREATE TABLE IF NOT EXISTS migration_state (\n id TEXT PRIMARY KEY,\n migration_type TEXT NOT NULL, -- 'v2-to-v3', 'pattern', 'memory', etc.\n\n -- Progress tracking\n status TEXT DEFAULT 'pending' CHECK(status IN ('pending', 'in_progress', 'completed', 'failed', 'rolled_back')),\n total_items INTEGER DEFAULT 0,\n processed_items INTEGER DEFAULT 0,\n failed_items INTEGER DEFAULT 0,\n skipped_items INTEGER DEFAULT 0,\n\n -- Current position (for resume)\n current_batch INTEGER DEFAULT 0,\n last_processed_id TEXT,\n\n -- Source/destination info\n source_path TEXT,\n source_type TEXT,\n destination_path TEXT,\n\n -- Backup info\n backup_path TEXT,\n backup_created_at INTEGER,\n\n -- Error tracking\n last_error TEXT,\n errors TEXT, -- JSON array of errors\n\n -- Timestamps\n started_at INTEGER,\n completed_at INTEGER,\n created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),\n updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)\n);\n\n-- ============================================\n-- SESSION MANAGEMENT\n-- ============================================\n\n-- Sessions for context persistence\nCREATE TABLE IF NOT EXISTS sessions (\n id TEXT PRIMARY KEY,\n\n -- Session state\n state TEXT NOT NULL, -- JSON object with full session state\n status TEXT DEFAULT 'active' CHECK(status IN ('active', 'paused', 'completed', 'expired')),\n\n -- Context\n project_path TEXT,\n branch TEXT,\n\n -- Metrics\n tasks_completed INTEGER DEFAULT 0,\n patterns_learned INTEGER DEFAULT 0,\n\n -- Timestamps\n created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),\n updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),\n expires_at INTEGER\n);\n\n-- ============================================\n-- VECTOR INDEX METADATA (for HNSW)\n-- ============================================\n\n-- Track HNSW index state\nCREATE TABLE IF NOT EXISTS vector_indexes (\n id TEXT PRIMARY KEY,\n name TEXT NOT NULL UNIQUE,\n\n -- Index configuration\n dimensions INTEGER NOT NULL,\n metric TEXT DEFAULT 'cosine' CHECK(metric IN ('cosine', 'euclidean', 'dot')),\n\n -- HNSW parameters\n hnsw_m INTEGER DEFAULT 16,\n hnsw_ef_construction INTEGER DEFAULT 200,\n hnsw_ef_search INTEGER DEFAULT 100,\n\n -- Quantization\n quantization_type TEXT CHECK(quantization_type IN ('none', 'scalar', 'product')),\n quantization_bits INTEGER DEFAULT 8,\n\n -- Statistics\n total_vectors INTEGER DEFAULT 0,\n last_rebuild_at INTEGER,\n\n created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),\n updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)\n);\n\n-- ============================================\n-- GRAPH EDGES (ADR-130 Phase 1)\n-- Unified knowledge graph backend \u2014 sql.js canonical store\n-- ============================================\n\n-- Unified graph edges table (ADR-130)\n-- Node IDs use domain-prefixed format: {domain}:{uuid}\n-- where domain in (mem, agent, task, entity, span, pattern)\nCREATE TABLE IF NOT EXISTS graph_edges (\n id TEXT PRIMARY KEY, -- edge-{uuid}\n source_id TEXT NOT NULL, -- domain-prefixed node ID\n target_id TEXT NOT NULL, -- domain-prefixed node ID\n relation TEXT NOT NULL, -- e.g. \"caused\", \"depends-on\", \"imports\"\n weight REAL DEFAULT 1.0,\n -- Temporal / reliability semantics (ADR-130 \u00A7\"graph that forgets\" property)\n confidence REAL DEFAULT 1.0, -- [0,1]; updated by JUDGE step\n decay_rate REAL DEFAULT 0.0, -- per-day exponential decay applied at read time\n last_reinforced TEXT, -- ISO-8601; set when CONSOLIDATE re-touches edge\n witness_id TEXT, -- FK to verification/witness-fixes.json (ADR-103)\n -- Embedding storage: \"inline:{base64}\" | \"vector_indexes:{id}\" | NULL\n embedding_ref TEXT,\n metadata TEXT, -- JSON blob for plugin-specific fields\n created_at TEXT NOT NULL\n);\n\nCREATE INDEX IF NOT EXISTS idx_graph_edges_source ON graph_edges (source_id);\nCREATE INDEX IF NOT EXISTS idx_graph_edges_target ON graph_edges (target_id);\nCREATE INDEX IF NOT EXISTS idx_graph_edges_relation ON graph_edges (relation);\nCREATE INDEX IF NOT EXISTS idx_graph_edges_reinforced ON graph_edges (last_reinforced);\n\n-- ============================================\n-- SYSTEM METADATA\n-- ============================================\n\nCREATE TABLE IF NOT EXISTS metadata (\n key TEXT PRIMARY KEY,\n value TEXT NOT NULL,\n updated_at INTEGER DEFAULT (strftime('%s', 'now') * 1000)\n);\n";
39
- interface HNSWEntry {
39
+ export interface HNSWEntry {
40
40
  id: string;
41
41
  key: string;
42
42
  namespace: string;
@@ -57,6 +57,14 @@ export declare function getHNSWIndex(options?: {
57
57
  dimensions?: number;
58
58
  forceRebuild?: boolean;
59
59
  }): Promise<HNSWIndex | null>;
60
+ export declare function removeHNSWEntriesByLogicalKey(entries: Map<string, HNSWEntry>, key: string, namespace: string): number;
61
+ /**
62
+ * Remove HNSW metadata whose authoritative SQLite row is no longer active.
63
+ * Persistent graph nodes may remain physically allocated, but without metadata
64
+ * they cannot resolve into search results; the next rebuild repopulates only
65
+ * active rows. Returns the number of searchable vectors invalidated.
66
+ */
67
+ export declare function reconcileHNSWIndex(dbPath?: string): Promise<number>;
60
68
  /**
61
69
  * Add entry to HNSW index (with automatic persistence)
62
70
  */
@@ -680,6 +680,64 @@ function saveHNSWMetadata() {
680
680
  // Silently fail - metadata save is best-effort
681
681
  }
682
682
  }
683
+ export function removeHNSWEntriesByLogicalKey(entries, key, namespace) {
684
+ let removed = 0;
685
+ for (const [id, entry] of entries) {
686
+ if (entry.key === key && (entry.namespace ?? 'default') === namespace) {
687
+ entries.delete(id);
688
+ removed++;
689
+ }
690
+ }
691
+ return removed;
692
+ }
693
+ function removeHNSWEntriesByKey(key, namespace) {
694
+ if (!hnswIndex?.entries)
695
+ return 0;
696
+ const removed = removeHNSWEntriesByLogicalKey(hnswIndex.entries, key, namespace);
697
+ if (removed > 0) {
698
+ saveHNSWMetadata();
699
+ rebuildSearchIndex();
700
+ }
701
+ return removed;
702
+ }
703
+ /**
704
+ * Remove HNSW metadata whose authoritative SQLite row is no longer active.
705
+ * Persistent graph nodes may remain physically allocated, but without metadata
706
+ * they cannot resolve into search results; the next rebuild repopulates only
707
+ * active rows. Returns the number of searchable vectors invalidated.
708
+ */
709
+ export async function reconcileHNSWIndex(dbPath) {
710
+ if (!hnswIndex?.entries)
711
+ return 0;
712
+ const effectivePath = dbPath
713
+ ? path.resolve(dbPath)
714
+ : path.join(getMemoryRoot(), 'memory.db');
715
+ if (!fs.existsSync(effectivePath))
716
+ return 0;
717
+ try {
718
+ const initSqlJs = (await import('sql.js')).default;
719
+ const SQL = await initSqlJs();
720
+ const db = new SQL.Database(readFileMaybeEncrypted(effectivePath, null));
721
+ const rows = db.exec(`SELECT id FROM memory_entries WHERE status = 'active'`);
722
+ const active = new Set((rows[0]?.values ?? []).map((row) => String(row[0])));
723
+ db.close();
724
+ let removed = 0;
725
+ for (const id of hnswIndex.entries.keys()) {
726
+ if (!active.has(id)) {
727
+ hnswIndex.entries.delete(id);
728
+ removed++;
729
+ }
730
+ }
731
+ if (removed > 0) {
732
+ saveHNSWMetadata();
733
+ rebuildSearchIndex();
734
+ }
735
+ return removed;
736
+ }
737
+ catch {
738
+ return 0;
739
+ }
740
+ }
683
741
  /**
684
742
  * Add entry to HNSW index (with automatic persistence)
685
743
  */
@@ -2380,6 +2438,9 @@ export async function storeEntry(options) {
2380
2438
  // Keep HNSW index in sync with bridge-stored entries
2381
2439
  if (bridgeResult.rawEmbedding && bridgeResult.success) {
2382
2440
  const ns = options.namespace || 'default';
2441
+ // Upsert/resurrection may allocate a new row id. Remove every older
2442
+ // vector for the logical (namespace,key), not merely the newest id.
2443
+ removeHNSWEntriesByKey(options.key, ns);
2383
2444
  await addToHNSWIndex(bridgeResult.id, bridgeResult.rawEmbedding, {
2384
2445
  id: bridgeResult.id,
2385
2446
  key: options.key,
@@ -2485,6 +2546,8 @@ export async function storeEntry(options) {
2485
2546
  // Add to HNSW index for faster future searches
2486
2547
  if (embeddingJson) {
2487
2548
  const embResult = JSON.parse(embeddingJson);
2549
+ if (upsert)
2550
+ removeHNSWEntriesByKey(key, namespace);
2488
2551
  await addToHNSWIndex(id, embResult, {
2489
2552
  id,
2490
2553
  key,
@@ -2977,15 +3040,7 @@ export async function deleteEntry(options) {
2977
3040
  // #1122: Bridge path must also invalidate the in-memory HNSW index.
2978
3041
  // Without this, deleted vectors remain as ghost entries in search results.
2979
3042
  if (bridgeResult.deleted && hnswIndex?.entries) {
2980
- // Remove the entry from the HNSW entries map by key+namespace composite
2981
- for (const [id, entry] of hnswIndex.entries) {
2982
- if (entry?.key === options.key && (entry?.namespace ?? 'default') === (options.namespace ?? 'default')) {
2983
- hnswIndex.entries.delete(id);
2984
- break;
2985
- }
2986
- }
2987
- saveHNSWMetadata();
2988
- rebuildSearchIndex();
3043
+ removeHNSWEntriesByKey(options.key, options.namespace ?? 'default');
2989
3044
  }
2990
3045
  return bridgeResult;
2991
3046
  }
@@ -3052,8 +3107,6 @@ export async function deleteEntry(options) {
3052
3107
  error: `Key '${key}' not found in namespace '${namespace}'`
3053
3108
  };
3054
3109
  }
3055
- // Capture the entry ID for HNSW cleanup
3056
- const entryId = String(checkResult[0].values[0][0]);
3057
3110
  // Delete the entry (soft delete by setting status to 'deleted')
3058
3111
  // Also null out the embedding to clean up vector data from SQLite
3059
3112
  db.run(`
@@ -3075,14 +3128,8 @@ export async function deleteEntry(options) {
3075
3128
  // Clean up in-memory HNSW index so ghost vectors don't appear in searches.
3076
3129
  // Remove the entry from the HNSW entries map and invalidate the index.
3077
3130
  // The next search will rebuild the HNSW index from the remaining DB rows.
3078
- if (hnswIndex?.entries) {
3079
- hnswIndex.entries.delete(entryId);
3080
- saveHNSWMetadata();
3081
- // Invalidate the HNSW index so it rebuilds from DB on next search.
3082
- // We can't surgically remove a vector from the HNSW graph, so we
3083
- // clear the entire index; it will be lazily rebuilt from SQLite.
3084
- rebuildSearchIndex();
3085
- }
3131
+ if (hnswIndex?.entries)
3132
+ removeHNSWEntriesByKey(key, namespace);
3086
3133
  return {
3087
3134
  success: true,
3088
3135
  deleted: true,
@@ -8,8 +8,9 @@
8
8
  * - single-instance: only starts when no live daemon holds the pidfile, and
9
9
  * the spawned `daemon start` independently enforces single-instance via its
10
10
  * own lock + checkExistingDaemon() — so a race spawns at most one survivor,
11
- * - bounded lifetime: the daemon self-terminates on TTL/idle (12h default,
12
- * RUFLO_DAEMON_TTL_SECS) auto-start never means "runs forever",
11
+ * - bounded lifetime: the daemon self-terminates on TTL/idle (12h hard TTL,
12
+ * 30m idle default; RUFLO_DAEMON_TTL_SECS / RUFLO_DAEMON_IDLE_SECS)
13
+ * auto-start never means "runs forever",
13
14
  * - opt-out: RUFLO_DAEMON_AUTOSTART=0|false|no disables it entirely, OR a
14
15
  * project-local `daemon.autostart: false` in claude-flow.config.json —
15
16
  * the file-based opt-out exists because the env var only reaches a
@@ -85,8 +86,10 @@ export function ensureDaemonRunning(projectRoot, opts = {}) {
85
86
  try {
86
87
  if (autostartDisabled(projectRoot))
87
88
  return { started: false, reason: 'disabled (RUFLO_DAEMON_AUTOSTART=0 or project config)' };
88
- // Only in an initialized project (avoid scaffolding a daemon in a random dir).
89
- if (!fs.existsSync(path.join(projectRoot, '.claude-flow')) && !fs.existsSync(path.join(projectRoot, '.claude'))) {
89
+ // `.claude/` belongs to Claude Code and is present in many repositories
90
+ // that have never initialized Ruflo. Only Ruflo's own state directory is
91
+ // an authorization signal for spawning a detached background process.
92
+ if (!fs.existsSync(path.join(projectRoot, '.claude-flow'))) {
90
93
  return { started: false, reason: 'not a ruflo project' };
91
94
  }
92
95
  const alive = (opts.isAlive ?? isDaemonAlive)(projectRoot);
@@ -56,6 +56,8 @@ export interface FlywheelReceiptPayload {
56
56
  gateVersion: string;
57
57
  policySchemaVersion: string;
58
58
  safetyEnvelopeRef: string;
59
+ /** Hash-pinned human relevance anchor used for this evaluation (#2840). */
60
+ anchorRef?: string;
59
61
  requestedProposer: 'auto' | ProposerName;
60
62
  effectiveProposer: ProposerName;
61
63
  proposerSubstitution?: string;
@@ -86,6 +88,7 @@ export interface CreateReceiptInput {
86
88
  gateVersion?: string;
87
89
  policySchemaVersion?: string;
88
90
  safetyEnvelopeRef: string;
91
+ anchorRef?: string;
89
92
  requestedProposer?: 'auto' | ProposerName;
90
93
  effectiveProposer?: ProposerName;
91
94
  proposerSubstitution?: string;
@@ -206,6 +206,7 @@ export function createFlywheelReceipt(input) {
206
206
  gateVersion: input.gateVersion ?? statistics.ruleVersion,
207
207
  policySchemaVersion: input.policySchemaVersion ?? 'ruflo.retrieval-policy/v1',
208
208
  safetyEnvelopeRef: input.safetyEnvelopeRef,
209
+ ...(input.anchorRef ? { anchorRef: input.anchorRef } : {}),
209
210
  requestedProposer: input.requestedProposer ?? 'local',
210
211
  effectiveProposer: input.effectiveProposer ?? 'local',
211
212
  ...(input.proposerSubstitution ? { proposerSubstitution: input.proposerSubstitution } : {}),
@@ -39,6 +39,9 @@ export declare function runFlywheelWorker(projectRoot: string, opts?: {
39
39
  allowSubstitutionPromotion?: boolean;
40
40
  maxConcurrency?: number;
41
41
  evaluationTimeoutMs?: number;
42
+ anchorPath?: string;
43
+ anchorHash?: string;
44
+ anchorManifestPath?: string;
42
45
  }): Promise<FlywheelResult>;
43
46
  /**
44
47
  * Run ONE live COMPOUNDING generation against the persisted lineage (ADR-176
@@ -52,5 +55,8 @@ export declare function runFlywheelGenerationWorker(projectRoot: string, opts?:
52
55
  sample?: number;
53
56
  optInOverride?: boolean;
54
57
  now?: number;
58
+ anchorPath?: string;
59
+ anchorHash?: string;
60
+ anchorManifestPath?: string;
55
61
  }): Promise<GenerationResult>;
56
62
  //# sourceMappingURL=harness-flywheel-runtime.d.ts.map
@@ -7,23 +7,10 @@
7
7
  import { harnessLoopOptedIn } from './harness-worker.js';
8
8
  import { DEFAULT_CONFIG, retrievalPolicyNeighbors, runFlywheelTick, } from './harness-flywheel.js';
9
9
  import { runFlywheelGeneration, checkServedChampionDrift } from './harness-flywheel-generations.js';
10
- import { loadFrozenHumanEval } from './harness-frozen-eval.js';
10
+ import { loadEffectiveFlywheelAnchor } from './harness-project-anchor.js';
11
11
  import { proposeFlywheelCandidates, } from './flywheel-proposer.js';
12
12
  import { sha256Ref } from './flywheel-receipt.js';
13
13
  import { evaluatePolicyRequest } from './policy-runtime.js';
14
- /** The human-labeled ADR-081 anchor — the never-regress relevance set. */
15
- const ANCHOR = [
16
- ['how was the Opus model alias fixed', ['opus 4.8', 'opus alias', 'opus model alias', '#2232']],
17
- ['self-learning wiring task-completed pretrain', ['self-learning', 'adr-074', 'self learning', '#2245', 'task-completed']],
18
- ['deterministic codemod engine var-to-const', ['deterministic tier-1 codemod', 'adr-143', 'codemod', 'var-to-const']],
19
- ['MCP server orphan leak parent-death', ['mcp orphan', 'mcp servers orphan', 'parent-death', '#2234', 'orphan on every claude']],
20
- ['unified learning stats aggregator', ['unified learning-stats', 'adr-075', 'unified learning stats']],
21
- ['structured distillation 4-field schema', ['structured distillation', 'adr-076', '4-field schema']],
22
- ['SQL injection migrate.ts table identifier', ['sql injection', 'shell injection', 'migrate.ts', 'agentdb', 'cve']],
23
- ['recall@k HNSW benchmark harness', ['hnsw', 'memory-recall', 'benchmark suite', 'recall@k', 'benchmark intelligence']],
24
- ['Q-learning encoder keyword block', ['q-state encoder', 'route q-state', 'keyword block', '#2239', 'q-encoder']],
25
- ['security hardening crypto random IDs', ['cwe-347', 'crypto.randomuuid', 'security fix', 'random id', 'crypto random']],
26
- ].map(([q, labels], i) => ({ id: `q${String(i).padStart(2, '0')}`, input: { id: `q${String(i).padStart(2, '0')}`, q: q }, expected: labels }));
27
14
  /**
28
15
  * The ADR-322 retrieval safety envelope.
29
16
  *
@@ -99,6 +86,11 @@ export async function runFlywheelWorker(projectRoot, opts = {}) {
99
86
  ...(applier.activeChampion(projectRoot)?.params ?? {}),
100
87
  };
101
88
  const safetyEnvelope = retrievalSafetyEnvelope(opts.safetyEnvelopeRef);
89
+ const anchor = loadEffectiveFlywheelAnchor(projectRoot, {
90
+ anchorPath: opts.anchorPath ?? process.env.RUFLO_FLYWHEEL_ANCHOR_PATH,
91
+ anchorHash: opts.anchorHash ?? process.env.RUFLO_FLYWHEEL_ANCHOR_HASH,
92
+ manifestPath: opts.anchorManifestPath ?? process.env.RUFLO_FLYWHEEL_ANCHOR_MANIFEST,
93
+ });
102
94
  // CLI flag opts.proposer takes precedence over RUFLO_FLYWHEEL_PROPOSER.
103
95
  const proposerMode = opts.proposer
104
96
  ?? (process.env.RUFLO_FLYWHEEL_PROPOSER ?? 'auto');
@@ -134,7 +126,8 @@ export async function runFlywheelWorker(projectRoot, opts = {}) {
134
126
  const r = await tool.handler({ action: 'search', query, mode: 'hybrid', limit: 5, rerank: false, ...cfg });
135
127
  return (r.results || []).slice(0, 5).map((m) => ({ id: m?.id ?? '', name: m?.name ?? '' }));
136
128
  },
137
- anchorTasks: ANCHOR,
129
+ anchorTasks: anchor.tasks,
130
+ anchorRef: anchor.anchorRef,
138
131
  activeParams: () => baseline,
139
132
  sample: opts.sample ?? 40,
140
133
  now: opts.now,
@@ -172,16 +165,23 @@ export async function runFlywheelGenerationWorker(projectRoot, opts = {}) {
172
165
  const tool = neural.neuralTools.find((t) => t.name === 'neural_patterns');
173
166
  if (!tool)
174
167
  return { ran: false, reason: 'neural_patterns tool unavailable', generation: 0 };
175
- // Load the FROZEN, hashed, public human eval set (throws if it has drifted).
176
- const frozen = loadFrozenHumanEval();
168
+ const anchor = loadEffectiveFlywheelAnchor(projectRoot, {
169
+ anchorPath: opts.anchorPath ?? process.env.RUFLO_FLYWHEEL_ANCHOR_PATH,
170
+ anchorHash: opts.anchorHash ?? process.env.RUFLO_FLYWHEEL_ANCHOR_HASH,
171
+ manifestPath: opts.anchorManifestPath ?? process.env.RUFLO_FLYWHEEL_ANCHOR_MANIFEST,
172
+ });
177
173
  const deps = {
178
174
  getPatterns: () => neural.getStorePatterns(),
179
175
  search: async (query, cfg) => {
180
176
  const r = await tool.handler({ action: 'search', query, mode: 'hybrid', limit: 5, rerank: false, ...cfg });
181
177
  return (r.results || []).slice(0, 5).map((m) => ({ id: m?.id ?? '', name: m?.name ?? '' }));
182
178
  },
183
- anchorTasks: frozen.tasks,
184
- humanEvalHash: frozen.corpusHash,
179
+ anchorTasks: anchor.tasks.map((task) => ({
180
+ id: task.id,
181
+ q: task.input.q,
182
+ labels: task.expected,
183
+ })),
184
+ humanEvalHash: anchor.anchorRef,
185
185
  sample: opts.sample ?? 120,
186
186
  now: opts.now ?? Date.now(),
187
187
  };
@@ -30,6 +30,8 @@ export interface FlywheelDeps {
30
30
  lineageId?: string;
31
31
  evaluationRunId?: string;
32
32
  safetyEnvelopeRef?: string;
33
+ /** Hash of the project-specific human-labelled objective. */
34
+ anchorRef?: string;
33
35
  requestedProposer?: 'auto' | 'local' | 'darwin';
34
36
  effectiveProposer?: 'local' | 'darwin';
35
37
  proposerSubstitution?: string;
@@ -210,6 +210,7 @@ export async function evaluateFlywheelCandidate(projectRoot, deps) {
210
210
  expectedLedgerHead: txState.ledgerHead,
211
211
  candidatePolicy: candidate,
212
212
  safetyEnvelopeRef,
213
+ anchorRef: deps.anchorRef,
213
214
  requestedProposer: deps.requestedProposer ?? 'local',
214
215
  effectiveProposer: deps.effectiveProposer ?? 'local',
215
216
  proposerSubstitution: deps.proposerSubstitution,
@@ -0,0 +1,23 @@
1
+ import type { AnchorTask } from './harness-flywheel.js';
2
+ export declare const PROJECT_ANCHOR_SCHEMA = "ruflo.flywheel-anchor/v1";
3
+ export declare const PROJECT_ANCHOR_MANIFEST_SCHEMA = "ruflo.flywheel-anchor-manifest/v1";
4
+ export declare const DEFAULT_PROJECT_ANCHOR_MANIFEST: string;
5
+ export interface ProjectAnchorManifest {
6
+ schemaVersion: typeof PROJECT_ANCHOR_MANIFEST_SCHEMA;
7
+ path: string;
8
+ sha256: string;
9
+ }
10
+ export interface FlywheelAnchorSelection {
11
+ version: string;
12
+ anchorRef: string;
13
+ tasks: AnchorTask[];
14
+ source: 'project' | 'ruflo-built-in';
15
+ path?: string;
16
+ }
17
+ export interface LoadFlywheelAnchorOptions {
18
+ anchorPath?: string;
19
+ anchorHash?: string;
20
+ manifestPath?: string;
21
+ }
22
+ export declare function loadEffectiveFlywheelAnchor(projectRoot: string, options?: LoadFlywheelAnchorOptions): FlywheelAnchorSelection;
23
+ //# sourceMappingURL=harness-project-anchor.d.ts.map