From d9bd4e6adbae700c2fb042c591d365c791a4cb2b Mon Sep 17 00:00:00 2001 From: ruv Date: Tue, 19 May 2026 21:59:44 -0400 Subject: [PATCH] =?UTF-8?q?feat(neural-trader):=20#2068=20ADR-126=20Phase?= =?UTF-8?q?=202=20=E2=80=94=20ADR-125=20memory=20lifecycle=20(TTL=20+=20de?= =?UTF-8?q?dup=20+=20warm=20HNSW=20doc)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wires the plugin into the ADR-125 lifecycle guarantees shipped in @claude-flow/memory@3.0.0-alpha.18 (already published). No new code, just three lifecycle hooks: 1. trader-signal/SKILL.md - Adds `expiresAt: Date.now() + 24h` to the signal memory_store call. Rationale: intraday signals shouldn't pollute long-running memory; the MemoryConsolidator.sweepExpired() pass (ADR-125 Phase 4) clears them from all indexes — including HNSW — after they expire. - Adds `mcp__claude-flow__memory_delete` to allowed-tools (for symmetry with the dedup path; trader-signal itself doesn't delete today, but the allowed-tools surface is shared across the lifecycle story so consumers don't have to mentally re-derive permissions). 2. trader-backtest/SKILL.md - New step 5: search for prior entries with the same (strategyId, paramsHash) and memory_delete them before writing the fresh result. Inline dedup keeps memory_search deterministic right after a re-run. - Documents that the MemoryConsolidator.dedup('keep-newest') pass achieves the same convergence asynchronously every 6h, so the inline step is an optimization not a correctness requirement. - Adds `mcp__claude-flow__memory_delete` to allowed-tools. - Renumbers subsequent steps. 3. README.md — new "Memory lifecycle (ADR-125 integration)" subsection under "Namespace coordination". Covers: - Warm HNSW restart (Phase 3 — automatic via MemoryService) - Hybrid retrieval RRF+MMR (Phase 5 — automatic, graceful FTS5 degrade) - Signal TTL (24h) - Backtest dedup (inline + 6h consolidator) - Consolidator schedule Smoke: 11 passed, 0 failed. Refs #2068 Co-Authored-By: RuFlo --- plugins/ruflo-neural-trader/README.md | 10 ++++++++++ .../skills/trader-backtest/SKILL.md | 12 ++++++++---- .../skills/trader-signal/SKILL.md | 6 +++--- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/plugins/ruflo-neural-trader/README.md b/plugins/ruflo-neural-trader/README.md index e3ea1eb538..d26f656faa 100644 --- a/plugins/ruflo-neural-trader/README.md +++ b/plugins/ruflo-neural-trader/README.md @@ -206,6 +206,16 @@ Note: the namespace prefix is `trading-` (the actual intent) rather than `neural All access via `memory_*` (namespace-routed). No `agentdb_hierarchical-*` or `agentdb_pattern-store` with namespace arguments — the plugin uses the correct routing throughout. +### Memory lifecycle (ADR-125 integration) + +This plugin relies on `@claude-flow/memory@3.0.0-alpha.18` for the lifecycle guarantees defined in [ADR-125](../../v3/docs/adr/ADR-125-memory-consolidation.md) and wired by [ADR-126 Phase 2](../../v3/docs/adr/ADR-126-neural-trader-substrate-integration.md): + +- **Warm HNSW restart** — `@claude-flow/memory@3.0.0-alpha.18` (ADR-125 Phase 3) snapshots the HNSW index to a `.hnsw` sidecar file, so neural-trader process restarts no longer rebuild the strategy / regime similarity index from scratch. No plugin-side change is required to benefit; routing is automatic through `MemoryService.search()`. +- **Hybrid retrieval (RRF + MMR)** — `market-analyst` regime-similarity queries automatically become hybrid (dense ANN + sparse FTS5 keyword, reciprocal-rank-fused and MMR-diversified) via the same `MemoryService.search()` path (ADR-125 Phase 5). When the embedding generator is unavailable, retrieval gracefully degrades to keyword-only rather than throwing. +- **Signal TTL (24h)** — `trader-signal` writes to `trading-signals` with `expiresAt: now + 24h`. The `MemoryConsolidator.sweepExpired()` pass (ADR-125 Phase 4) removes them from all indexes — including HNSW — when they expire. Long-running ruflo sessions no longer accumulate stale intraday signals. +- **Backtest dedup** — `trader-backtest` proactively deletes prior entries for the same `(strategyId, paramsHash)` before storing a fresh one. The same outcome is also produced asynchronously by the `MemoryConsolidator.dedup('keep-newest')` background pass that runs every 6 hours. +- **Consolidator schedule** — the consolidator runs every 6 hours by default (`sweepExpired` + `dedup` + `compactHnsw`), and also on `MemoryService.close()`. No plugin-side wiring is required. + ## Verification ```bash diff --git a/plugins/ruflo-neural-trader/skills/trader-backtest/SKILL.md b/plugins/ruflo-neural-trader/skills/trader-backtest/SKILL.md index 38e3ddb4f2..02b6aae6af 100644 --- a/plugins/ruflo-neural-trader/skills/trader-backtest/SKILL.md +++ b/plugins/ruflo-neural-trader/skills/trader-backtest/SKILL.md @@ -1,7 +1,7 @@ --- name: trader-backtest description: Run a historical backtest using npx neural-trader with Rust/NAPI engine (8-19x faster) and walk-forward validation -allowed-tools: Bash Read mcp__claude-flow__memory_store mcp__claude-flow__memory_retrieve mcp__claude-flow__memory_search mcp__claude-flow__neural_train mcp__claude-flow__agentdb_pattern-store +allowed-tools: Bash Read mcp__claude-flow__memory_store mcp__claude-flow__memory_retrieve mcp__claude-flow__memory_search mcp__claude-flow__memory_delete mcp__claude-flow__neural_train mcp__claude-flow__agentdb_pattern-store argument-hint: " --symbol [--period 2020-2024]" --- Run a historical backtest using the `neural-trader` Rust/NAPI engine. @@ -21,9 +21,13 @@ Steps: npx neural-trader --backtest --strategy multi-indicator --position-sizing kelly --symbol SPY --period 2020-2024 ``` 4. Capture performance metrics from output: total return, annualized return, Sharpe ratio, Sortino ratio, max drawdown, win rate, profit factor, number of trades -5. Store backtest results: +5. Dedup prior backtests for the same `(strategyId, paramsHash)` before storing the fresh one (ADR-125 lifecycle / ADR-126 Phase 2 — `keep-newest` semantics): + - Search: `mcp__claude-flow__memory_search({ query: "backtest STRATEGY paramsHash:PARAMS_HASH", namespace: "trading-backtests", limit: 10 })` + - For each hit whose key matches `backtest-STRATEGY-*` AND whose stored `paramsHash` equals the current run's hash, delete it: `mcp__claude-flow__memory_delete({ key: "OLD_KEY", namespace: "trading-backtests" })` + - (Note: even without this proactive step, the `MemoryConsolidator.dedup('keep-newest')` background pass introduced in `@claude-flow/memory@3.0.0-alpha.18` runs every 6h and will eventually converge. Doing it inline keeps `memory_search` results deterministic immediately after a re-run.) +6. Store backtest results (long-lived; no TTL — backtest history is the audit trail that ADR-126 Phase 4 will Ed25519-sign): `mcp__claude-flow__memory_store({ key: "backtest-STRATEGY-TIMESTAMP", value: "RESULTS_JSON", namespace: "trading-backtests" })` -6. If Sharpe > 1.5, store as successful pattern: +7. If Sharpe > 1.5, store as successful pattern: `mcp__claude-flow__agentdb_pattern-store({ pattern: "profitable-STRATEGY_TYPE", data: "PARAMS_AND_RESULTS" })` -7. Train SONA on the outcome: +8. Train SONA on the outcome: `mcp__claude-flow__neural_train({ patternType: "trading-strategy", epochs: 10 })` diff --git a/plugins/ruflo-neural-trader/skills/trader-signal/SKILL.md b/plugins/ruflo-neural-trader/skills/trader-signal/SKILL.md index a59612e996..f5f4b0ac88 100644 --- a/plugins/ruflo-neural-trader/skills/trader-signal/SKILL.md +++ b/plugins/ruflo-neural-trader/skills/trader-signal/SKILL.md @@ -1,7 +1,7 @@ --- name: trader-signal description: Generate trading signals using npx neural-trader anomaly detection engine with Z-score scoring and neural prediction -allowed-tools: Bash Read mcp__claude-flow__memory_store mcp__claude-flow__memory_retrieve mcp__claude-flow__memory_search mcp__claude-flow__neural_predict mcp__claude-flow__agentdb_pattern-search +allowed-tools: Bash Read mcp__claude-flow__memory_store mcp__claude-flow__memory_retrieve mcp__claude-flow__memory_search mcp__claude-flow__memory_delete mcp__claude-flow__neural_predict mcp__claude-flow__agentdb_pattern-search argument-hint: "[--strategy NAME] [--symbols AAPL,MSFT]" --- Generate trading signals using neural-trader's anomaly detection engine. @@ -31,5 +31,5 @@ Steps: 6. Search historical pattern matches: `mcp__claude-flow__agentdb_pattern-search({ query: "ANOMALY_TYPE score RANGE", namespace: "trading-signals" })` 7. Present ranked signals: instrument, direction, confidence, anomaly type, entry/stop/target -8. Store signals: - `mcp__claude-flow__memory_store({ key: "signal-TIMESTAMP", value: "SIGNALS_JSON", namespace: "trading-signals" })` +8. Store signals with a 24-hour TTL (intraday signals shouldn't pollute long-running memory; the `MemoryConsolidator.sweepExpired()` pass introduced in ADR-125 Phase 4 — shipped in `@claude-flow/memory@3.0.0-alpha.18` — sweeps them out after they expire): + `mcp__claude-flow__memory_store({ key: "signal-TIMESTAMP", value: "SIGNALS_JSON", namespace: "trading-signals", expiresAt: Date.now() + 24 * 60 * 60 * 1000 })`