feat(neural-trader): #2068 ADR-126 Phase 2 — ADR-125 memory lifecycle (TTL + dedup + warm HNSW doc)

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 <ruv@ruv.net>
This commit is contained in:
ruv committed 2026-05-19 21:59:44 -04:00
1 parent 8d9e20f0c6
commit d9bd4e6adb
3 files changed
+21 -7

No files matched your search

+10
View File
@@ -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
@@ -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: "<strategy-name> --symbol <TICKER> [--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 })`
@@ -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 })`