Explain a trading signal by building a feature-contribution graph and running single-entry forward-push PageRank from the signal output node. Top-K ranked features are returned as a markdown table AND persisted to trading-analysis as a SignedAttributionArtifact (ADR-126 Phase 6).
Why this skill matters:
- EU AI Act + SEC Reg-AI guidance require interpretable model output for any algorithmic trading system that touches retail capital. This is the regulator-grade attribution path the rest of the substrate has been waiting for.
- The same call site picks up the full native-WASM PageRank from
mcp__ruflo-sublinear__page-rank-entry once that tool is registered in the runtime — until then, the local power-iteration kernel ships in signed-attribution.mjs and produces the same ordering (seeded mulberry32).
Steps:
Retrieve the signal from the canonical trading-signals namespace (ADR-126 Phase 1 + Phase 2 lifecycle):
mcp__plugin_ruflo-core_ruflo__memory_retrieve({
key: "SIGNAL_ID",
namespace: "trading-signals"
})
The signal entry includes modelId, prediction, and the feature vector at the time of inference.
Extract per-feature contribution scores from the model:
npx neural-trader --predict --signal "$SIGNAL_ID" --explain --json
The expected output shape:
{
features: Array<{ name: string; contribution: number }>;
// for Transformers, also includes per-head attention co-occurrence:
attention?: Array<{ head: string; cooccur: Array<[number, number, number]> }>;
}
Fallback path — if --explain is not shipped on the installed neural-trader build (older versions; the flag was scoped for a follow-up upstream PR), the skill degrades to a deterministic feature-importance heuristic over the signal's input vector: contribution_i = |input_i - μ_i| / σ_i (z-score magnitude). This is a known proxy — not as faithful as attention/SHAP — and the resulting artifact is tagged attribution_method: "input-zscore-fallback" so downstream consumers can filter it out for regulator filings. Document the fallback path in the resulting markdown summary so the agent surfaces it to the user.
Build the feature-contribution graph:
- Nodes: one node per feature + one source node
__signal_output__ for the prediction.
- Edges: outgoing edges from
__signal_output__ to each feature node, weighted by contribution_i. When attention co-occurrence data is available, also add edges between feature nodes weighted by cooccur — this is what makes the PageRank single-entry rather than degenerating to plain top-K.
- Source:
__signal_output__ (index 0 by convention so the smoke can assert reproducibility).
Run single-entry PageRank — preferred path when mcp__ruflo-sublinear__page-rank-entry is registered:
mcp__ruflo-sublinear__page-rank-entry({
nodes: GRAPH_NODES,
edges: GRAPH_EDGES,
sourceIndex: 0,
damping: 0.85,
maxIterations: 100,
tolerance: 1e-8,
seed: 42
})
The local fallback (localSingleEntryPageRank in plugins/ruflo-neural-trader/src/signed-attribution.mjs) runs ~30 LOC of seeded power-iteration when the MCP tool is not available — same math, same result up to floating-point tolerance, same ordering for the same seed (the Phase 6 smoke asserts this).
Build the top-K AttributionFeature[] via topKFeatures(graph, scores, k=10, excludeIndex=0) — excludes the source node from the ranked output. Ties broken by node index (lower index wins) so the ranking is deterministic.
Sign the artifact (reuses the Phase 4 signing primitives — same Ed25519 + canonicalization):
- Build the
SignedAttributionArtifact body:{
signalId: SIGNAL_ID,
modelId: SIGNAL.modelId,
features: TOP_K_FEATURES, // from step 5
graphMetadata: {
nodeCount: GRAPH.nodes.length,
edgeCount: COUNT_EDGES,
pageRankIterations: PR_RESULT.iterations,
seed: SEED // load-bearing for reproducibility
},
generatedAt: NEW_DATE_ISO
}
- Resolve the witness signing key — same lookup order as Phase 4:
RUFLO_WITNESS_KEY_PATH env var — JSON file with { "privateKey": "<hex>" }.
verification/witness-key.json (the ADR-103 default path).
- If a key resolves:
signAttributionArtifact(body, privateKeyHex) from plugins/ruflo-neural-trader/src/signed-attribution.mjs.
- If NEITHER path resolves: log
"[WARN] ruflo-neural-trader: no witness signing key found — storing attribution artifact in UNSIGNED degraded mode. Regulator filings will reject UNSIGNED artifacts." and store the body unsigned. NEVER silently fall back.
Store the (possibly signed) artifact to the canonical trading-analysis namespace (ADR-126 Phase 1):
mcp__plugin_ruflo-core_ruflo__memory_store({
key: "attribution-SIGNAL_ID-TIMESTAMP",
namespace: "trading-analysis",
value: JSON.stringify(signedArtifact)
})
The trading-analysis namespace is the canonical home for model-analysis output (regime classifications, technical-indicator summaries, model-training results — and now attribution rankings). Long-lived — no TTL — because the audit trail is the deliverable.
Return the markdown summary to the agent. Suggested format:
## Feature attribution for signal `SIGNAL_ID` (model: MODEL_ID)
| Rank | Feature | Score |
|------|---------|-------|
| 1 | NAME | 0.42 |
| 2 | NAME | 0.18 |
| … | … | … |
- PageRank iterations: N
- Graph: nodeCount nodes, edgeCount edges
- Seed: 42 (reproducible — same seed → same ordering)
- Path: mcp | local
- Signature: ed25519:abcd… (or UNSIGNED — degraded warning above)
Verification
Downstream consumers verify the artifact before any regulator-facing report or paper→live promotion:
import { verifyAttributionArtifact } from 'plugins/ruflo-neural-trader/src/signed-attribution.mjs';
const ok = await verifyAttributionArtifact(artifact, trustedPublicKey);
if (!ok) {
// [ERROR] attribution verification failed — refuse to publish.
// Pin to trustedPublicKey from project config; do NOT trust the
// artifact.witnessPublicKey field (CWE-347 / #1922 — attacker-controllable).
return;
}
Acceptance criteria (ADR-126 Phase 6):
trader-explain <signalId> returns a ranked feature list whose top-3 features overlap the model's attention argmax (when --explain available; documented tolerance).
- Reproducibility: two runs with the same
signalId + same --seed produce byte-identical rank ordering (asserted by scripts/smoke-neural-trader-feature-attribution.mjs).
- Signed artifact verifies under the trusted pubkey; tampering any feature score or
graphMetadata.seed invalidates the signature.
- Fallback paths engage cleanly: when MCP unavailable, local kernel runs; when
--explain flag missing, z-score heuristic runs and the artifact is tagged.
Refs:
- ADR-126 Phase 6 (this skill's authoring ADR)
- ADR-126 Phase 4 (the signing scheme this reuses)
- ADR-123 (single-entry PageRank substrate; the same family that Phase 3 leverages for portfolio CG)
plugins/ruflo-neural-trader/src/signed-attribution.ts (the typed contract)
plugins/ruflo-neural-trader/src/signed-attribution.mjs (the runtime mirror)
scripts/smoke-neural-trader-feature-attribution.mjs (the regression smoke)
Source: ruvnet/ruflo → plugins/ruflo-neural-trader/skills/trader-explain/SKILL.md
1---2name: trader-explain3description: Regulator-grade feature attribution for any LSTM/Transformer signal — single-entry PageRank ranks the top-K features that drove the prediction (ADR-126 Phase 6, ADR-123 single-entry PR)4---5
6Explain a trading signal by building a feature-contribution graph and running single-entry forward-push PageRank from the signal output node. Top-K ranked features are returned as a markdown table AND persisted to `trading-analysis` as a `SignedAttributionArtifact` (ADR-126 Phase 6).
7
8**Why this skill matters:**
9- EU AI Act + SEC Reg-AI guidance require interpretable model output for any algorithmic trading system that touches retail capital. This is the regulator-grade attribution path the rest of the substrate has been waiting for.
10- The same call site picks up the full native-WASM PageRank from `mcp__ruflo-sublinear__page-rank-entry` once that tool is registered in the runtime — until then, the local power-iteration kernel ships in `signed-attribution.mjs` and produces the same ordering (seeded mulberry32).
11
12Steps:
13
141. **Retrieve the signal** from the canonical `trading-signals` namespace (ADR-126 Phase 1 + Phase 2 lifecycle):
15 ```text
16 mcp__plugin_ruflo-core_ruflo__memory_retrieve({
17 key: "SIGNAL_ID",
18 namespace: "trading-signals"
19 })
20 ```
21 The signal entry includes `modelId`, `prediction`, and the feature vector at the time of inference.
22
232. **Extract per-feature contribution scores** from the model:
24 ```bash
25 npx neural-trader --predict --signal "$SIGNAL_ID" --explain --json
26 ```
27 The expected output shape:
28 ```ts
29 {
30 features: Array<{ name: string; contribution: number }>;
31 // for Transformers, also includes per-head attention co-occurrence:
32 attention?: Array<{ head: string; cooccur: Array<[number, number, number]> }>;
33 }
34 ```
35
36 **Fallback path** — if `--explain` is not shipped on the installed `neural-trader` build (older versions; the flag was scoped for a follow-up upstream PR), the skill degrades to a deterministic feature-importance heuristic over the signal's input vector: `contribution_i = |input_i - μ_i| / σ_i` (z-score magnitude). This is a known proxy — not as faithful as attention/SHAP — and the resulting artifact is tagged `attribution_method: "input-zscore-fallback"` so downstream consumers can filter it out for regulator filings. Document the fallback path in the resulting markdown summary so the agent surfaces it to the user.
37
383. **Build the feature-contribution graph**:
39 - **Nodes**: one node per feature + one source node `__signal_output__` for the prediction.
40 - **Edges**: outgoing edges from `__signal_output__` to each feature node, weighted by `contribution_i`. When attention co-occurrence data is available, also add edges between feature nodes weighted by `cooccur` — this is what makes the PageRank single-entry rather than degenerating to plain top-K.
41 - **Source**: `__signal_output__` (index 0 by convention so the smoke can assert reproducibility).
42
434. **Run single-entry PageRank** — preferred path when `mcp__ruflo-sublinear__page-rank-entry` is registered:
44 ```text
45 mcp__ruflo-sublinear__page-rank-entry({
46 nodes: GRAPH_NODES,
47 edges: GRAPH_EDGES,
48 sourceIndex: 0,
49 damping: 0.85,
50 maxIterations: 100,
51 tolerance: 1e-8,
52 seed: 42
53 })
54 ```
55 The local fallback (`localSingleEntryPageRank` in `plugins/ruflo-neural-trader/src/signed-attribution.mjs`) runs ~30 LOC of seeded power-iteration when the MCP tool is not available — same math, same result up to floating-point tolerance, same ordering for the same seed (the Phase 6 smoke asserts this).
56
575. **Build the top-K `AttributionFeature[]`** via `topKFeatures(graph, scores, k=10, excludeIndex=0)` — excludes the source node from the ranked output. Ties broken by node index (lower index wins) so the ranking is deterministic.
58
596. **Sign the artifact** (reuses the Phase 4 signing primitives — same Ed25519 + canonicalization):
60 - Build the `SignedAttributionArtifact` body:
61 ```ts
62 {
63 signalId: SIGNAL_ID,
64 modelId: SIGNAL.modelId,
65 features: TOP_K_FEATURES, // from step 5
66 graphMetadata: {
67 nodeCount: GRAPH.nodes.length,
68 edgeCount: COUNT_EDGES,
69 pageRankIterations: PR_RESULT.iterations,
70 seed: SEED // load-bearing for reproducibility
71 },
72 generatedAt: NEW_DATE_ISO
73 }
74 ```
75 - Resolve the witness signing key — same lookup order as Phase 4:
76 1. `RUFLO_WITNESS_KEY_PATH` env var — JSON file with `{ "privateKey": "<hex>" }`.
77 2. `verification/witness-key.json` (the ADR-103 default path).
78 - If a key resolves: `signAttributionArtifact(body, privateKeyHex)` from `plugins/ruflo-neural-trader/src/signed-attribution.mjs`.
79 - If NEITHER path resolves: log `"[WARN] ruflo-neural-trader: no witness signing key found — storing attribution artifact in UNSIGNED degraded mode. Regulator filings will reject UNSIGNED artifacts."` and store the body unsigned. NEVER silently fall back.
80
817. **Store the (possibly signed) artifact** to the canonical `trading-analysis` namespace (ADR-126 Phase 1):
82 ```text
83 mcp__plugin_ruflo-core_ruflo__memory_store({
84 key: "attribution-SIGNAL_ID-TIMESTAMP",
85 namespace: "trading-analysis",
86 value: JSON.stringify(signedArtifact)
87 })
88 ```
89 The `trading-analysis` namespace is the canonical home for model-analysis output (regime classifications, technical-indicator summaries, model-training results — and now attribution rankings). Long-lived — no TTL — because the audit trail is the deliverable.
90
918. **Return the markdown summary** to the agent. Suggested format:
92 ```
93 ## Feature attribution for signal `SIGNAL_ID` (model: MODEL_ID)
94
95 | Rank | Feature | Score |
96 |------|---------|-------|
97 | 1 | NAME | 0.42 |
98 | 2 | NAME | 0.18 |
99 | … | … | … |
100
101 - PageRank iterations: N
102 - Graph: nodeCount nodes, edgeCount edges
103 - Seed: 42 (reproducible — same seed → same ordering)
104 - Path: mcp | local
105 - Signature: ed25519:abcd… (or UNSIGNED — degraded warning above)
106 ```
107
108### Verification
109
110Downstream consumers verify the artifact before any regulator-facing report or paper→live promotion:
111
112```ts
113import { verifyAttributionArtifact } from 'plugins/ruflo-neural-trader/src/signed-attribution.mjs';
114
115const ok = await verifyAttributionArtifact(artifact, trustedPublicKey);
116if (!ok) {
117 // [ERROR] attribution verification failed — refuse to publish.
118 // Pin to trustedPublicKey from project config; do NOT trust the
119 // artifact.witnessPublicKey field (CWE-347 / #1922 — attacker-controllable).
120 return;
121}
122```
123
124**Acceptance criteria (ADR-126 Phase 6):**
125- `trader-explain <signalId>` returns a ranked feature list whose top-3 features overlap the model's attention argmax (when `--explain` available; documented tolerance).
126- Reproducibility: two runs with the same `signalId` + same `--seed` produce byte-identical rank ordering (asserted by `scripts/smoke-neural-trader-feature-attribution.mjs`).
127- Signed artifact verifies under the trusted pubkey; tampering any feature score or `graphMetadata.seed` invalidates the signature.
128- Fallback paths engage cleanly: when MCP unavailable, local kernel runs; when `--explain` flag missing, z-score heuristic runs and the artifact is tagged.
129
130**Refs:**
131- ADR-126 Phase 6 (this skill's authoring ADR)
132- ADR-126 Phase 4 (the signing scheme this reuses)
133- ADR-123 (single-entry PageRank substrate; the same family that Phase 3 leverages for portfolio CG)
134- `plugins/ruflo-neural-trader/src/signed-attribution.ts` (the typed contract)
135- `plugins/ruflo-neural-trader/src/signed-attribution.mjs` (the runtime mirror)
136- `scripts/smoke-neural-trader-feature-attribution.mjs` (the regression smoke)
137
138---
139
140**Source:** [`ruvnet/ruflo`](https://github.com/ruvnet/ruflo) → `plugins/ruflo-neural-trader/skills/trader-explain/SKILL.md`