aiden-shared-calculations-unified 1.0.82 → 1.0.83

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.
package/README.MD CHANGED
@@ -1,78 +1,155 @@
1
- # Unified Calculations Package (`aiden-shared-calculations-unified`)
1
+ ---
2
2
 
3
- **Version:** 1.0.0
3
+ # Quantum Test Harness for Computation System
4
4
 
5
5
  ## Overview
6
6
 
7
- This package centralizes all data calculation logic for the BullTrackers project. It provides a standardized structure for calculations run by the unified Computation System and includes shared utility functions. Calculations are dynamically loaded and categorized.
7
+ The Quantum Test Harness is a **dynamic, dependency-aware testing framework** for running, validating, and profiling the entire computation system.
8
+
9
+ The new architecture is built around a `run-suite.js` orchestrator. It automatically builds a dependency graph of all calculations, ensuring they are tested in the correct order. This "ground-up" approach allows for robust, efficient, and realistic testing that mimics the production environment.
10
+
11
+ The harness still tests calculations **without modifying their source code**. It:
12
+
13
+ * Builds a dependency graph of all calculations.
14
+ * Dynamically generates mock data for a consistent "universe" of users and tickers.
15
+ * Executes calculations in the correct order, passing the results of one test as dependencies to the next.
16
+ * Monitors performance and validates output against each calculation's `getSchema()`.
17
+ * Generates rich, interactive HTML reports for each calculation run.
18
+
19
+ ---
20
+
21
+ ## Core Concepts
22
+
23
+ The harness is built on **four main components**:
24
+
25
+ ### 1. Test Suite Orchestrator (`run-suite.js`)
26
+
27
+ The main CLI entry point for running automated test suites. Responsibilities:
28
+
29
+ * Scans all calculation files and builds a computation graph.
30
+ * Topologically sorts the graph to determine the correct execution order.
31
+ * Parses CLI commands (`--all`, `--target`, `--product`) to decide which tests to run.
32
+ * Manages a results cache to efficiently pass outputs as dependencies.
33
+
34
+ ### 2. Test Worker (`test-worker.js`)
35
+
36
+ Executes the test for a single calculation. Responsibilities:
37
+
38
+ * Receives a calculation to test from the orchestrator, along with its pre-computed dependencies.
39
+ * Generates fresh, temporal mock data for the specific test.
40
+ * Instantiates the calculation and wraps it in the Quantum Monitor.
41
+ * Executes `process()` and `getResult()`.
42
+ * Triggers the report generation.
43
+
44
+ ### 3. Dynamic Data Generation (`data-generator.js`)
45
+
46
+ Generates **temporal mock data**:
47
+
48
+ 1. The orchestrator creates a consistent "universe" (tickers and userIds) for the entire test run.
49
+ 2. The test worker generates "today" and optional "yesterday" data snapshots using this universe, ensuring data is consistent and comparable across tests.
50
+
51
+ ### 4. Instrumentation & Validation (`test-harness.js`)
8
52
 
9
- ## Package Structure
53
+ Provides:
10
54
 
11
- ### `/utils`
55
+ * **Instrumentation**: Wraps the calculation instance in a Proxy via `createQuantumRecorder`. Logs performance, memory, and errors without changing the calculation logic.
56
+ * **Validation**:
57
+ * **Schema Check**: Uses Ajv to validate `getResult()` against `getSchema()`.
58
+ * **Sanity Check**: Flags "valid but empty" results like `0`, `null`, `[]`, or `{}` as warnings.
12
59
 
13
- Shared utility functions used by calculations or the systems consuming them.
60
+ ---
61
+
62
+ ## Usage: Automated Test Suite
63
+
64
+ To run automated, dependency-aware test suites, use the `run-suite.js` orchestrator. This is the recommended method for all testing.
65
+
66
+ ### Prerequisites
67
+
68
+ Graphviz is required to render data-flow diagrams. Ensure `dot` is available in your PATH.
69
+
70
+ | OS | Installation |
71
+ | ------- | ------------------------------- |
72
+ | macOS | `brew install graphviz` |
73
+ | Windows | `choco install graphviz` |
74
+ | Linux | `sudo apt-get install graphviz` |
75
+
76
+ ---
77
+
78
+ ### Commands
14
79
 
15
- * `firestore_utils.js`: Provides a resilient `withRetry` wrapper for Firestore operations using exponential backoff.
16
- * `sector_mapping_provider.js`: Provides functions (`loadInstrumentMappings`, `getInstrumentSectorMap`) to fetch and cache instrument-to-ticker and instrument-to-sector mappings from Firestore.
80
+ #### 1. Run ALL Computations
17
81
 
18
- ### `/calculations`
82
+ Tests every non-legacy computation, ordered by complexity (dependency-free calculations first).
19
83
 
20
- Contains the core calculation logic, organized into subdirectories representing categories.
84
+ ```bash
85
+ node run-suite.js --all
86
+ ```
21
87
 
22
- * **Calculation Class Structure:** Each `.js` file defines a class responsible for a specific metric. Every class **must** implement:
23
- * `constructor()`: Initializes any internal state needed for aggregation.
24
- * `process(portfolioData, userId, context)` OR `process(todayPortfolio, yesterdayPortfolio, userId, context)`: Processes a single user's data. The signature depends on whether the calculation requires historical comparison. `context` provides shared data like mappings.
25
- * `getResult()`: Returns the final, calculated result for the aggregation period. **Crucially, this method must perform any final averaging (e.g., sum/count) itself.** It should return the final value or object ready for storage, not raw components.
26
- * `reset()`: (Optional but recommended) Resets the internal state, often used by the calling system between processing batches or days.
88
+ #### 2. Run a Specific Target
27
89
 
28
- * **Categories (Examples based on your files):**
29
- * `/asset_metrics`: Calculations focused on individual assets (e.g., `asset_dollar_metrics`, `asset_position_size`).
30
- * `/behavioural`: Calculations analyzing user trading patterns (e.g., `drawdown_response`, `gain_response`, `paper_vs_diamond_hands`, `position_count_pnl`, `smart_money_flow`).
31
- * `/pnl`: Profit and Loss related calculations (e.g., `asset_pnl_status`, `average_daily_pnl_all_users`, `average_daily_pnl_per_sector`, `average_daily_pnl_per_stock`, `average_daily_position_pnl`, `pnl_distribution_per_stock`, `profitability_migration`, `profitability_ratio_per_stock`, `profitability_skew_per_stock`, `user_profitability_tracker`).
32
- * `/sanity`: Basic checks and counts (e.g., `users_processed`).
33
- * `/sectors`: Calculations aggregated by market sector (e.g., `diversification_pnl`, `sector_dollar_metrics`, `sector_rotation`, `total_long_per_sector`, `total_short_per_sector`).
34
- * `/sentiment`: Calculations related to market sentiment (e.g., `crowd_conviction_score`).
35
- * `/short_and_long_stats`: Specific counts for short and long positions (e.g., `long_position_per_stock`, `sentiment_per_stock`, `short_position_per_stock`, `total_long_figures`, `total_short_figures`).
36
- * `/speculators`: Calculations **specifically** for the 'speculator' user type, often involving leverage, stop-loss, or take-profit data (e.g., `distance_to_stop_loss_per_leverage`, `distance_to_tp_per_leverage`, `entry_distance_to_sl_per_leverage`, `entry_distance_to_tp_per_leverage`, `holding_duration_per_asset`, `leverage_per_asset`, `leverage_per_sector`, `risk_appetite_change`, `risk_reward_ratio_per_asset`, `speculator_asset_sentiment`, `speculator_danger_zone`, `stop_loss_distance_by_sector_short_long_breakdown`, `stop_loss_distance_by_ticker_short_long_breakdown`, `stop_loss_per_asset`, `take_profit_per_asset`, `tsl_effectiveness`, `tsl_per_asset`).
90
+ Tests a single computation and all of its dependencies in the correct order.
37
91
 
38
- * **Output Formats:** Calculations should adhere to the standardized output formats defined in `docs/Notes/output_formats.md`.
92
+ ```bash
93
+ # Syntax
94
+ node run-suite.js --target [computation-name]
39
95
 
40
- ## Usage
96
+ # Example (note: uses the name, not the file path)
97
+ node run-suite.js --target asset-pnl-status
98
+ ```
41
99
 
42
- This package is intended to be consumed primarily by the unified Computation System.
100
+ #### 3. Run a Full Product Line
43
101
 
44
- ```javascript
45
- // Example usage within Computation System
46
- const { calculations, utils } = require('aiden-shared-calculations-unified');
102
+ Tests all computations belonging to a specific product category (defined in `getMetadata`) and all of their dependencies.
47
103
 
48
- const CalculationClass = calculations.pnl.average_daily_pnl_per_stock;
49
- const calculator = new CalculationClass();
104
+ ```bash
105
+ # Syntax
106
+ node run-suite.js --product [product-name]
50
107
 
51
- // ... load data ...
108
+ # Example
109
+ node run-suite.js --product gem
110
+ ```
52
111
 
53
- // In a loop for each user:
54
- calculator.process(portfolioData, userId, context);
112
+ ---
55
113
 
56
- // After processing all users:
57
- const results = await calculator.getResult();
114
+ ### Output
58
115
 
59
- // ... store results ...
60
- ````
116
+ Running a test produces:
61
117
 
62
- ## Contributing
118
+ * **Console Output**: Live logs of the orchestration and test progress.
119
+ * **Test Reports**: Generated for each executed calculation at `test_reports/[CalculationName]/`:
120
+ * `timeline.html` – Interactive performance report.
121
+ * `dataflow.svg` – Diagram of the calculation's data flow.
122
+ * `computation_result.json` – Raw JSON output from `getResult()`.
123
+ * `report.json` – Full metrics collected by the harness.
63
124
 
64
- *(Outline the process for adding new calculations)*
125
+ ---
65
126
 
66
- 1. **Determine Category:** Decide which subdirectory (`/calculations/<category>`) the new metric belongs to. Create a new category if necessary.
67
- 2. **Create File:** Create a new `.js` file using kebab-case (e.g., `my-new-metric.js`).
68
- 3. **Implement Class:** Define the calculation class, ensuring it has `constructor`, `process`, and `getResult` methods adhering to the standards. Remember `getResult` must return the *final* computed value.
69
- 4. **Add to Manifest:** The main `index.js` uses `require-all`, so the new calculation should be automatically included in the exports upon the next package build/publish, assuming the file naming and structure are correct.
70
- 5. **Publish:** Bump the package version (`npm version patch` or minor/major as appropriate) and publish (`npm publish --access public`).
71
- 6. **Update Consumer:** Update the version dependency in the Computation System's `package.json`.
127
+ ## Adding a New Calculation
72
128
 
73
- <!-- end list -->
129
+ The process remains the same.
74
130
 
131
+ 1. **Create Your File**: `calculations/my_product/my-new-calc.js`
132
+ 2. **Implement the Class**: Must include `constructor()`, `process()`, and `getResult()`.
133
+ 3. **Add `getDependencies()`**: Define the other calculations this one depends on.
134
+ 4. **Add `getMetadata()`**: Defines the contract for the harness.
135
+ 5. **Add `getSchema()`**: Enables automatic output validation.
136
+ 6. **Run the Test**: Use the `run-suite.js` commands.
137
+
138
+ ```bash
139
+ # Test your new calculation and its dependencies
140
+ node run-suite.js --target my-new-calc
75
141
  ```
76
142
 
77
143
  ---
78
- ```
144
+
145
+ ## Troubleshooting
146
+
147
+ | Issue | Solution |
148
+ | ------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------- |
149
+ | Graphviz rendering failed | Ensure Graphviz is installed and `dot` is in your system's PATH. |
150
+ | `SchemaValidation` errors in `report.json` | The calculation's output does not match its `getSchema()`. Check `computation_result.json` to see the actual output and fix the logic. |
151
+ | `SanityCheck` warnings in `report.json` | The output is valid according to the schema but is empty (`null`, `0`, `[]`, `{}`). This may be expected, but it's worth verifying. |
152
+ | `Error: Unknown computation dependency: [name]` | A calculation lists a dependency in `getDependencies()` that doesn't exist or has a typo. Check the name for errors. |
153
+ | `Error: Circular dependency detected!` | Two or more calculations depend on each other, creating an impossible loop. Review the `getDependencies()` lists for the involved calculations. |
154
+
155
+ ---
@@ -119,8 +119,10 @@ class WinnerLoserFlow {
119
119
  return;
120
120
  }
121
121
 
122
+
123
+
122
124
  if (!this.mappings) {
123
- this.mappings = context.mappings;
125
+ this.mappings = context.instrumentToTickerMap;
124
126
  }
125
127
 
126
128
  const yHoldings = this._getHoldingsWithPnl(yesterdayPortfolio); // Map<InstID, {pnl}>
@@ -132,7 +134,7 @@ class WinnerLoserFlow {
132
134
  }
133
135
 
134
136
  for (const instrumentId of allInstrumentIds) {
135
- const ticker = this.mappings.instrumentToTicker[instrumentId];
137
+ const ticker = this.mappings[instrumentId];
136
138
  if (!ticker) continue;
137
139
 
138
140
  // --- MODIFIED ---
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiden-shared-calculations-unified",
3
- "version": "1.0.82",
3
+ "version": "1.0.83",
4
4
  "description": "Shared calculation modules for the BullTrackers Computation System.",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -27,15 +27,15 @@
27
27
  ],
28
28
  "dependencies": {
29
29
  "@google-cloud/firestore": "^7.11.3",
30
- "sharedsetup": "latest",
31
30
  "require-all": "^3.0.0",
32
- "dotenv": "latest",
31
+ "sharedsetup": "latest",
33
32
  "viz.js": "^2.1.2"
34
33
  },
35
34
  "devDependencies": {
36
- "bulltracker-deployer": "file:../bulltracker-deployer",
37
35
  "@google-cloud/firestore": "^7.11.3",
38
- "dotenv": "latest",
36
+ "ajv": "^8.17.1",
37
+ "bulltracker-deployer": "file:../bulltracker-deployer",
38
+ "dotenv": "^17.2.3",
39
39
  "node-graphviz": "^0.1.1"
40
40
  },
41
41
  "engines": {