@tradejs/cli 3.1.29-beta.261 → 3.1.29-beta.263
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 +47 -8
- package/dist/cli.js +990 -1448
- package/dist/lib/aiPocketSearch.js +16 -0
- package/dist/lib/aiPocketSearchCli.js +3 -3
- package/dist/lib/jevMicroReferences.js +73 -0
- package/dist/lib/jevResearch.js +11 -0
- package/dist/lib/runtimeEvidenceArtifacts.js +3 -416
- package/dist/lib/runtimeEvidenceCompositionSnapshots.js +42 -211
- package/dist/lib/runtimeEvidenceCompositions.js +33 -310
- package/dist/lib/runtimeEvidenceDeployment.js +3 -315
- package/dist/lib/runtimeEvidenceProducer.js +3 -77
- package/dist/lib/runtimeEvidenceSync.js +32 -299
- package/dist/lib/runtimeFeedbackArtifacts.js +3 -545
- package/dist/lib/runtimeFeedbackNotification.js +7 -195
- package/dist/lib/runtimeFeedbackReplay.js +26 -525
- package/dist/lib/runtimeFeedbackSync.js +26 -219
- package/dist/lib/runtimeScorecard.js +5 -163
- package/dist/scripts/aiPocketSearch.js +101 -23
- package/dist/scripts/backtest.js +10 -1
- package/dist/scripts/jev.js +11 -0
- package/dist/scripts/mcpWorker.js +273 -0
- package/dist/scripts/replay.js +14 -250
- package/dist/scripts/replayRunner.js +14 -250
- package/dist/scripts/runtimeEvidence.js +47 -597
- package/dist/scripts/runtimeEvidenceSync.js +29 -296
- package/dist/scripts/runtimeFeedbackNotify.js +10 -188
- package/dist/scripts/runtimeFeedbackReplay.js +29 -528
- package/dist/scripts/runtimeFeedbackSync.js +28 -221
- package/dist/scripts/runtimeScorecard.js +88 -503
- package/dist/scripts/signals.js +55 -224
- package/dist/scripts/signalsDaemon.js +55 -224
- package/package.json +9 -8
|
@@ -2158,6 +2158,22 @@ var buildAiPocketMarkdownReport = ({
|
|
|
2158
2158
|
"",
|
|
2159
2159
|
markdownTable(pocketTableHeaders, pocketRows(pocketSearch.positivePockets)),
|
|
2160
2160
|
"",
|
|
2161
|
+
"## Development Stability (inside discovery, not independent validation)",
|
|
2162
|
+
"",
|
|
2163
|
+
markdownTable(
|
|
2164
|
+
["Condition", "Status", "Block", "Events", "PnL", "PF"],
|
|
2165
|
+
pocketSearch.positivePockets.flatMap(
|
|
2166
|
+
(pocket) => (pocket.stability?.folds ?? []).map((fold, index) => [
|
|
2167
|
+
pocket.condition,
|
|
2168
|
+
pocket.stability?.status,
|
|
2169
|
+
index + 1,
|
|
2170
|
+
fold.summary.events,
|
|
2171
|
+
formatMdNumber(fold.summary.totalProfit),
|
|
2172
|
+
formatMdNumber(fold.summary.profitFactor)
|
|
2173
|
+
])
|
|
2174
|
+
)
|
|
2175
|
+
),
|
|
2176
|
+
"",
|
|
2161
2177
|
"## Top Loss Pockets",
|
|
2162
2178
|
"",
|
|
2163
2179
|
markdownTable(pocketTableHeaders, pocketRows(pocketSearch.negativePockets)),
|
|
@@ -66,7 +66,7 @@ var resolveAiPocketCadenceProfile = ({
|
|
|
66
66
|
validationRows = [],
|
|
67
67
|
testRows = [],
|
|
68
68
|
mode = "auto",
|
|
69
|
-
validationSplit = 0
|
|
69
|
+
validationSplit = 0,
|
|
70
70
|
minSupport,
|
|
71
71
|
minEvents,
|
|
72
72
|
minValidationSupport,
|
|
@@ -116,8 +116,8 @@ var AI_POCKET_SEARCH_CLI_DECIMAL_DEFAULTS = {
|
|
|
116
116
|
minProfitFactor: "1.2",
|
|
117
117
|
minWinRate: "0",
|
|
118
118
|
minTotalProfit: "0",
|
|
119
|
-
validationSplit: "0
|
|
120
|
-
testSplit: "0"
|
|
119
|
+
validationSplit: "0",
|
|
120
|
+
testSplit: "0.4"
|
|
121
121
|
};
|
|
122
122
|
var splitAiPocketResearchRowsByTimestamp = (rows, validationSplit, testSplit = 0) => {
|
|
123
123
|
const timestamps = [
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/lib/jevMicroReferences.ts
|
|
21
|
+
var jevMicroReferences_exports = {};
|
|
22
|
+
__export(jevMicroReferences_exports, {
|
|
23
|
+
microReferenceFeatures: () => microReferenceFeatures
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(jevMicroReferences_exports);
|
|
26
|
+
var microReferenceFeatures = (input) => {
|
|
27
|
+
const number = (...keys) => {
|
|
28
|
+
for (const key of keys) {
|
|
29
|
+
const value = input.features[key];
|
|
30
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
};
|
|
34
|
+
const bands = (value, cuts) => {
|
|
35
|
+
if (value == null || value < 0) return null;
|
|
36
|
+
const level = cuts.filter((cut) => value >= cut).length;
|
|
37
|
+
return level / 4;
|
|
38
|
+
};
|
|
39
|
+
const ratio = number("setup.retracementRatio");
|
|
40
|
+
const contraction = number(
|
|
41
|
+
"setup.consolidationToImpulseVolumeRatio",
|
|
42
|
+
"setup.flagToPoleVolumeRatio"
|
|
43
|
+
);
|
|
44
|
+
const extension = number("setup.breakoutDistanceAtr");
|
|
45
|
+
const efficiency = number(
|
|
46
|
+
"setup.impulseEfficiencyRatio",
|
|
47
|
+
"setup.poleEfficiencyRatio"
|
|
48
|
+
);
|
|
49
|
+
const body = number("entry.directionalBodyAtr");
|
|
50
|
+
const close = number("entry.directionalCloseLocation");
|
|
51
|
+
const upper = number("geometry.upperR2"), lower = number("geometry.lowerR2"), divergence = number("geometry.slopeDivergenceRatio");
|
|
52
|
+
const fit = upper != null && lower != null ? Math.min(upper, lower) : null;
|
|
53
|
+
const bias = (key) => {
|
|
54
|
+
const value = input.features[key];
|
|
55
|
+
if (!["bull", "bear"].includes(String(value))) return 0;
|
|
56
|
+
return value === (input.direction === "LONG" ? "bull" : "bear") ? 1 : -1;
|
|
57
|
+
};
|
|
58
|
+
const trend = bias("market.trendBias"), swing = bias("market.swingBias");
|
|
59
|
+
return {
|
|
60
|
+
directionalAgreement: input.features["market.trendBias"] == null && input.features["market.swingBias"] == null ? null : (trend + swing + 2) / 4,
|
|
61
|
+
correctionContainment: ratio == null || ratio < 0 ? null : (ratio <= 0.2 ? 4 : ratio <= 0.4 ? 3 : ratio <= 0.65 ? 2 : ratio <= 1 ? 1 : 0) / 4,
|
|
62
|
+
volumeExpansion: bands(number("market.volumeRel20"), [0.5, 0.8, 1.2, 2]),
|
|
63
|
+
volumeContraction: contraction == null || contraction < 0 ? null : (contraction <= 0.4 ? 4 : contraction <= 0.7 ? 3 : contraction <= 0.9 ? 2 : contraction <= 1.2 ? 1 : 0) / 4,
|
|
64
|
+
entryProximity: extension == null || extension < 0 ? null : (extension <= 0.2 ? 4 : extension <= 0.5 ? 3 : extension <= 1 ? 2 : extension <= 2 ? 1 : 0) / 4,
|
|
65
|
+
entryConviction: body == null || close == null || close < 0 || close > 1 ? null : (body < 0 && close < 0.25 ? 0 : body < 0 || close < 0.5 ? 1 : body < 0.2 ? 2 : body >= 0.5 && close >= 0.75 ? 4 : 3) / 4,
|
|
66
|
+
impulsePersistence: efficiency == null || efficiency > 1 ? null : bands(efficiency, [0.2, 0.4, 0.6, 0.8]),
|
|
67
|
+
boundaryRegularity: fit == null || divergence == null || fit < 0 || fit > 1 || divergence < 0 ? null : (fit < 0.5 || divergence > 0.5 ? 0 : fit < 0.7 || divergence > 0.3 ? 1 : fit < 0.85 || divergence > 0.15 ? 2 : fit >= 0.95 && divergence <= 0.05 ? 4 : 3) / 4
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
71
|
+
0 && (module.exports = {
|
|
72
|
+
microReferenceFeatures
|
|
73
|
+
});
|
package/dist/lib/jevResearch.js
CHANGED
|
@@ -170,6 +170,17 @@ var runJevResearch = async (options) => {
|
|
|
170
170
|
throw new Error(
|
|
171
171
|
"Export exceeds 20000 samples; select a bounded recording directory"
|
|
172
172
|
);
|
|
173
|
+
await (0, import_jev2.evaluateJevInput)({
|
|
174
|
+
input: record.input,
|
|
175
|
+
config: {
|
|
176
|
+
source: "recorded",
|
|
177
|
+
mode: "observe",
|
|
178
|
+
provider: record.provider,
|
|
179
|
+
recordsDir: options.recordsDir
|
|
180
|
+
},
|
|
181
|
+
userName: options.userName,
|
|
182
|
+
projectRoot: options.projectRoot
|
|
183
|
+
});
|
|
173
184
|
const outcome = byRecord.get(record.id);
|
|
174
185
|
rows2.push({
|
|
175
186
|
schema: "tradejs-jev-study/v4",
|
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
6
|
var __copyProps = (to, from, except, desc) => {
|
|
13
7
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
8
|
for (let key of __getOwnPropNames(from))
|
|
@@ -17,421 +11,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
11
|
}
|
|
18
12
|
return to;
|
|
19
13
|
};
|
|
20
|
-
var
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
28
15
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
16
|
|
|
30
17
|
// src/lib/runtimeEvidenceArtifacts.ts
|
|
31
18
|
var runtimeEvidenceArtifacts_exports = {};
|
|
32
|
-
__export(runtimeEvidenceArtifacts_exports, {
|
|
33
|
-
RUNTIME_EVIDENCE_COMPLETE_FILE: () => RUNTIME_EVIDENCE_COMPLETE_FILE,
|
|
34
|
-
RUNTIME_EVIDENCE_MANIFEST_FILE: () => RUNTIME_EVIDENCE_MANIFEST_FILE,
|
|
35
|
-
RUNTIME_EVIDENCE_PAYLOAD_FILE: () => RUNTIME_EVIDENCE_PAYLOAD_FILE,
|
|
36
|
-
discoverRuntimeEvidenceBundles: () => discoverRuntimeEvidenceBundles,
|
|
37
|
-
publishRuntimeEvidenceBundle: () => publishRuntimeEvidenceBundle,
|
|
38
|
-
verifyRuntimeEvidenceBundle: () => verifyRuntimeEvidenceBundle
|
|
39
|
-
});
|
|
40
19
|
module.exports = __toCommonJS(runtimeEvidenceArtifacts_exports);
|
|
41
|
-
|
|
42
|
-
var import_promises = __toESM(require("fs/promises"));
|
|
43
|
-
var import_node_path = __toESM(require("path"));
|
|
44
|
-
|
|
45
|
-
// src/lib/runtimeEvidenceDeployment.ts
|
|
46
|
-
var import_runtimeStrategies = require("@tradejs/node/runtimeStrategies");
|
|
47
|
-
var isRecord = (value) => value != null && typeof value === "object" && !Array.isArray(value);
|
|
48
|
-
var isNonEmptyString = (value) => typeof value === "string" && value.trim().length > 0;
|
|
49
|
-
var isPackageVersionMap = (value) => isRecord(value) && Object.keys(value).length > 0 && Object.entries(value).every(
|
|
50
|
-
([name, version]) => name.startsWith("@tradejs/") && isNonEmptyString(version)
|
|
51
|
-
);
|
|
52
|
-
var INTERVALS = /* @__PURE__ */ new Set([
|
|
53
|
-
"1",
|
|
54
|
-
"3",
|
|
55
|
-
"5",
|
|
56
|
-
"15",
|
|
57
|
-
"30",
|
|
58
|
-
"60",
|
|
59
|
-
"120",
|
|
60
|
-
"240",
|
|
61
|
-
"360",
|
|
62
|
-
"720",
|
|
63
|
-
"D",
|
|
64
|
-
"W",
|
|
65
|
-
"M"
|
|
66
|
-
]);
|
|
67
|
-
var UNIVERSES = /* @__PURE__ */ new Set(["crypto", "tradfi"]);
|
|
68
|
-
var isRuntimeStrategySelection = (value) => value === void 0 || isRecord(value) && Object.keys(value).length === 1 && Object.keys(value).every((key) => key === "tickers") && Array.isArray(value.tickers) && value.tickers.length > 0 && value.tickers.every((ticker) => isNonEmptyString(ticker));
|
|
69
|
-
var samePackageVersions = (left, right) => {
|
|
70
|
-
if (!left || !right) return false;
|
|
71
|
-
const leftEntries = Object.entries(left).sort(
|
|
72
|
-
([a], [b]) => a.localeCompare(b)
|
|
73
|
-
);
|
|
74
|
-
const rightEntries = Object.entries(right).sort(
|
|
75
|
-
([a], [b]) => a.localeCompare(b)
|
|
76
|
-
);
|
|
77
|
-
return JSON.stringify(leftEntries) === JSON.stringify(rightEntries);
|
|
78
|
-
};
|
|
79
|
-
var runtimeLineageMatchesStrategySnapshot = ({
|
|
80
|
-
lineage,
|
|
81
|
-
deployment,
|
|
82
|
-
strategy
|
|
83
|
-
}) => lineage?.schemaVersion === 3 && lineage.strategyRevision === strategy.strategyRevision && lineage.deploymentCompositionId === deployment.deploymentCompositionId && lineage.strategyPackageVersion === strategy.strategyPackageVersion && lineage.runtimePackageVersion === strategy.runtimePackageVersion && samePackageVersions(
|
|
84
|
-
lineage.strategyDependencyVersions,
|
|
85
|
-
strategy.strategyDependencyVersions
|
|
86
|
-
);
|
|
87
|
-
var parseRuntimeEvidenceDeploymentSnapshot = (value) => {
|
|
88
|
-
if (!isRecord(value) || value.schemaVersion !== 2 || !isNonEmptyString(value.id) || !isNonEmptyString(value.deploymentCompositionId) || !/^dc1:[a-f0-9]{16}$/.test(value.deploymentCompositionId) || !isNonEmptyString(value.label) || !isNonEmptyString(value.connectorName) || !isNonEmptyString(value.provider) || !isNonEmptyString(value.accountId) || typeof value.enabled !== "boolean" || !Array.isArray(value.strategies)) {
|
|
89
|
-
throw new Error(
|
|
90
|
-
"Runtime evidence deployment snapshot is missing or invalid"
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
const strategies = value.strategies.map((candidate) => {
|
|
94
|
-
if (!isRecord(candidate) || !isNonEmptyString(candidate.strategyName) || !isNonEmptyString(candidate.strategyRevision) || !/^sr1:[a-f0-9]{16}$/.test(candidate.strategyRevision) || typeof candidate.enabled !== "boolean" || !["active", "entries_paused"].includes(String(candidate.controlState)) || !INTERVALS.has(String(candidate.interval)) || !UNIVERSES.has(String(candidate.universe)) || !isNonEmptyString(candidate.strategyPackage) || !isNonEmptyString(candidate.strategyPackageVersion) || !isPackageVersionMap(candidate.strategyDependencyVersions) || !isNonEmptyString(candidate.runtimePackageVersion) || !isRecord(candidate.strategyConfig) || !isRuntimeStrategySelection(candidate.selection)) {
|
|
95
|
-
throw new Error(
|
|
96
|
-
"Runtime evidence deployment strategy snapshot is invalid"
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
if (String(candidate.strategyConfig.INTERVAL) !== String(candidate.interval) || String(candidate.strategyConfig.UNIVERSE) !== String(candidate.universe)) {
|
|
100
|
-
throw new Error(
|
|
101
|
-
`Runtime evidence strategy config does not match its scope: ${candidate.strategyName}`
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
if (candidate.accountId != null && (!isNonEmptyString(candidate.accountId) || candidate.accountId !== value.accountId)) {
|
|
105
|
-
throw new Error(
|
|
106
|
-
`Runtime evidence strategy account does not match deployment: ${candidate.strategyName}`
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
return candidate;
|
|
110
|
-
});
|
|
111
|
-
const identities = /* @__PURE__ */ new Set();
|
|
112
|
-
for (const strategy of strategies) {
|
|
113
|
-
if (identities.has(strategy.strategyName)) {
|
|
114
|
-
throw new Error(
|
|
115
|
-
`Duplicate runtime evidence strategy: ${strategy.strategyName}`
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
identities.add(strategy.strategyName);
|
|
119
|
-
}
|
|
120
|
-
return {
|
|
121
|
-
...value,
|
|
122
|
-
strategies
|
|
123
|
-
};
|
|
124
|
-
};
|
|
125
|
-
var activeRuntimeEvidenceStrategies = (deployment) => deployment.enabled ? deployment.strategies.filter((strategy) => strategy.enabled) : [];
|
|
126
|
-
var unwrapRuntimeEvidenceRows = (value, field) => (Array.isArray(value) ? value : []).map((candidate) => {
|
|
127
|
-
const record = isRecord(candidate) ? candidate : null;
|
|
128
|
-
const nested = record && isRecord(record[field]) ? record[field] : null;
|
|
129
|
-
return nested ?? record;
|
|
130
|
-
}).filter((record) => record != null);
|
|
131
|
-
var assertCurrentRuntimeEvidenceArtifact = (artifact) => {
|
|
132
|
-
if (!isRecord(artifact) || artifact.reportType !== "runtime-evidence") {
|
|
133
|
-
throw new Error("Invalid runtime evidence artifact");
|
|
134
|
-
}
|
|
135
|
-
const deployment = parseRuntimeEvidenceDeploymentSnapshot(
|
|
136
|
-
artifact.deployment
|
|
137
|
-
);
|
|
138
|
-
const activeStrategies = activeRuntimeEvidenceStrategies(deployment);
|
|
139
|
-
if (activeStrategies.length === 0) {
|
|
140
|
-
throw new Error("Runtime evidence deployment has no enabled strategies");
|
|
141
|
-
}
|
|
142
|
-
const runtime = isRecord(artifact.runtime) ? artifact.runtime : null;
|
|
143
|
-
if (!runtime) {
|
|
144
|
-
throw new Error("Runtime evidence payload is missing");
|
|
145
|
-
}
|
|
146
|
-
if ("strategyConfigs" in runtime) {
|
|
147
|
-
throw new Error(
|
|
148
|
-
"Runtime evidence payload contains removed strategyConfigs data"
|
|
149
|
-
);
|
|
150
|
-
}
|
|
151
|
-
if ("evaluationStatsBuckets" in runtime) {
|
|
152
|
-
throw new Error(
|
|
153
|
-
"Runtime evidence payload contains removed lineage-less evaluationStatsBuckets data"
|
|
154
|
-
);
|
|
155
|
-
}
|
|
156
|
-
const rowFields = [
|
|
157
|
-
"trades",
|
|
158
|
-
"signals",
|
|
159
|
-
"evaluations",
|
|
160
|
-
"lineageScopes"
|
|
161
|
-
];
|
|
162
|
-
for (const field of rowFields) {
|
|
163
|
-
if (!Array.isArray(runtime[field])) {
|
|
164
|
-
throw new Error(`Runtime evidence payload ${field} must be an array`);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
const snapshots = new Map(
|
|
168
|
-
activeStrategies.map((strategy) => [strategy.strategyName, strategy])
|
|
169
|
-
);
|
|
170
|
-
const assertRows = (label, rows, lineageField) => {
|
|
171
|
-
for (const row of rows) {
|
|
172
|
-
const strategyName = String(row.strategy ?? "").trim();
|
|
173
|
-
const strategy = snapshots.get(strategyName);
|
|
174
|
-
if (!strategy || row.deploymentId !== deployment.id || row.accountId !== deployment.accountId || !runtimeLineageMatchesStrategySnapshot({
|
|
175
|
-
lineage: isRecord(row[lineageField]) ? row[lineageField] : void 0,
|
|
176
|
-
deployment,
|
|
177
|
-
strategy
|
|
178
|
-
})) {
|
|
179
|
-
throw new Error(
|
|
180
|
-
`Runtime evidence ${label} row is outside the current embedded deployment lineage`
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
assertRows(
|
|
186
|
-
"trade",
|
|
187
|
-
unwrapRuntimeEvidenceRows(runtime.trades, "trade"),
|
|
188
|
-
"runtimeLineage"
|
|
189
|
-
);
|
|
190
|
-
assertRows(
|
|
191
|
-
"signal",
|
|
192
|
-
unwrapRuntimeEvidenceRows(runtime.signals, "signal"),
|
|
193
|
-
"runtimeLineage"
|
|
194
|
-
);
|
|
195
|
-
assertRows(
|
|
196
|
-
"evaluation",
|
|
197
|
-
unwrapRuntimeEvidenceRows(runtime.evaluations, "evaluation"),
|
|
198
|
-
"runtimeLineage"
|
|
199
|
-
);
|
|
200
|
-
assertRows(
|
|
201
|
-
"lineage scope",
|
|
202
|
-
unwrapRuntimeEvidenceRows(runtime.lineageScopes, "lineageScope"),
|
|
203
|
-
"lineage"
|
|
204
|
-
);
|
|
205
|
-
return deployment;
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
// src/lib/runtimeEvidenceArtifacts.ts
|
|
209
|
-
var RUNTIME_EVIDENCE_MANIFEST_FILE = "manifest.json";
|
|
210
|
-
var RUNTIME_EVIDENCE_PAYLOAD_FILE = "runtime-evidence.json";
|
|
211
|
-
var RUNTIME_EVIDENCE_COMPLETE_FILE = ".complete";
|
|
212
|
-
var asRecord = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
213
|
-
var sha256 = (value) => (0, import_node_crypto.createHash)("sha256").update(value).digest("hex");
|
|
214
|
-
var runtimeEvidenceIdentitySha256 = (artifact) => {
|
|
215
|
-
const identityArtifact = { ...artifact };
|
|
216
|
-
delete identityArtifact.generatedAt;
|
|
217
|
-
const runtime = asRecord(identityArtifact.runtime);
|
|
218
|
-
if (runtime) {
|
|
219
|
-
const identityRuntime = { ...runtime };
|
|
220
|
-
delete identityRuntime.generatedAt;
|
|
221
|
-
identityArtifact.runtime = identityRuntime;
|
|
222
|
-
}
|
|
223
|
-
return sha256(JSON.stringify(identityArtifact));
|
|
224
|
-
};
|
|
225
|
-
var isExistingDestinationError = (error) => {
|
|
226
|
-
const code = error.code;
|
|
227
|
-
return code === "EEXIST" || code === "ENOTEMPTY";
|
|
228
|
-
};
|
|
229
|
-
var safeSegment = (value, fallback) => {
|
|
230
|
-
const normalized = value.trim().replace(/[^a-zA-Z0-9._-]+/g, "-");
|
|
231
|
-
return normalized && normalized !== "." && normalized !== ".." ? normalized : fallback;
|
|
232
|
-
};
|
|
233
|
-
var compactTimestamp = (timestamp) => new Date(timestamp).toISOString().replace(/[-:]/g, "").replace(/\.000Z$/, "Z");
|
|
234
|
-
var dateParts = (timestamp) => {
|
|
235
|
-
const date = new Date(timestamp);
|
|
236
|
-
return [
|
|
237
|
-
String(date.getUTCFullYear()),
|
|
238
|
-
String(date.getUTCMonth() + 1).padStart(2, "0"),
|
|
239
|
-
String(date.getUTCDate()).padStart(2, "0")
|
|
240
|
-
];
|
|
241
|
-
};
|
|
242
|
-
var assertFiniteTimestamp = (value, label) => {
|
|
243
|
-
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
244
|
-
throw new Error(`Invalid runtime evidence ${label}`);
|
|
245
|
-
}
|
|
246
|
-
return value;
|
|
247
|
-
};
|
|
248
|
-
var parseManifest = (value) => {
|
|
249
|
-
const manifest = asRecord(value);
|
|
250
|
-
const window = asRecord(manifest?.window);
|
|
251
|
-
const payload = asRecord(manifest?.payload);
|
|
252
|
-
const counts = asRecord(manifest?.counts);
|
|
253
|
-
if (manifest?.schemaVersion !== 1 || manifest.reportType !== "runtime-evidence" || typeof manifest.artifactId !== "string" || typeof manifest.deploymentId !== "string" || typeof manifest.userName !== "string" || typeof manifest.createdAt !== "number" || payload?.file !== RUNTIME_EVIDENCE_PAYLOAD_FILE || typeof payload.sha256 !== "string" || !/^[a-f0-9]{64}$/.test(payload.sha256) || typeof payload.bytes !== "number" || !Array.isArray(manifest.lineageKeys)) {
|
|
254
|
-
throw new Error("Invalid runtime evidence manifest");
|
|
255
|
-
}
|
|
256
|
-
return {
|
|
257
|
-
schemaVersion: 1,
|
|
258
|
-
artifactId: manifest.artifactId,
|
|
259
|
-
reportType: "runtime-evidence",
|
|
260
|
-
deploymentId: manifest.deploymentId,
|
|
261
|
-
userName: manifest.userName,
|
|
262
|
-
createdAt: manifest.createdAt,
|
|
263
|
-
window: {
|
|
264
|
-
startTime: assertFiniteTimestamp(window?.startTime, "window.startTime"),
|
|
265
|
-
endTime: assertFiniteTimestamp(window?.endTime, "window.endTime")
|
|
266
|
-
},
|
|
267
|
-
payload: {
|
|
268
|
-
file: RUNTIME_EVIDENCE_PAYLOAD_FILE,
|
|
269
|
-
sha256: payload.sha256,
|
|
270
|
-
bytes: payload.bytes
|
|
271
|
-
},
|
|
272
|
-
counts: Object.fromEntries(
|
|
273
|
-
Object.entries(counts ?? {}).filter(
|
|
274
|
-
(entry) => typeof entry[1] === "number" && Number.isFinite(entry[1])
|
|
275
|
-
)
|
|
276
|
-
),
|
|
277
|
-
lineageKeys: manifest.lineageKeys.filter(
|
|
278
|
-
(value2) => typeof value2 === "string" && value2.length > 0
|
|
279
|
-
)
|
|
280
|
-
};
|
|
281
|
-
};
|
|
282
|
-
var publishRuntimeEvidenceBundle = async ({
|
|
283
|
-
publishRoot,
|
|
284
|
-
deploymentId,
|
|
285
|
-
userName,
|
|
286
|
-
startTime,
|
|
287
|
-
endTime,
|
|
288
|
-
artifact,
|
|
289
|
-
counts,
|
|
290
|
-
lineageKeys
|
|
291
|
-
}) => {
|
|
292
|
-
assertCurrentRuntimeEvidenceArtifact(artifact);
|
|
293
|
-
const payload = `${JSON.stringify(artifact, null, 2)}
|
|
294
|
-
`;
|
|
295
|
-
const payloadSha256 = sha256(payload);
|
|
296
|
-
const identitySha256 = runtimeEvidenceIdentitySha256(artifact);
|
|
297
|
-
const safeDeploymentId = safeSegment(deploymentId, "default");
|
|
298
|
-
const artifactId = [
|
|
299
|
-
compactTimestamp(startTime),
|
|
300
|
-
compactTimestamp(endTime),
|
|
301
|
-
identitySha256.slice(0, 16)
|
|
302
|
-
].join("_");
|
|
303
|
-
const createdAt = Date.now();
|
|
304
|
-
const manifest = {
|
|
305
|
-
schemaVersion: 1,
|
|
306
|
-
artifactId,
|
|
307
|
-
reportType: "runtime-evidence",
|
|
308
|
-
deploymentId: safeDeploymentId,
|
|
309
|
-
userName,
|
|
310
|
-
createdAt,
|
|
311
|
-
window: { startTime, endTime },
|
|
312
|
-
payload: {
|
|
313
|
-
file: RUNTIME_EVIDENCE_PAYLOAD_FILE,
|
|
314
|
-
sha256: payloadSha256,
|
|
315
|
-
bytes: Buffer.byteLength(payload)
|
|
316
|
-
},
|
|
317
|
-
counts,
|
|
318
|
-
lineageKeys: [...new Set(lineageKeys)].sort()
|
|
319
|
-
};
|
|
320
|
-
const incomingRoot = import_node_path.default.join(publishRoot, "incoming");
|
|
321
|
-
const readyRoot = import_node_path.default.join(
|
|
322
|
-
publishRoot,
|
|
323
|
-
"ready",
|
|
324
|
-
safeDeploymentId,
|
|
325
|
-
...dateParts(endTime)
|
|
326
|
-
);
|
|
327
|
-
const bundleDir = import_node_path.default.join(readyRoot, artifactId);
|
|
328
|
-
const temporaryDir = import_node_path.default.join(
|
|
329
|
-
incomingRoot,
|
|
330
|
-
`${artifactId}.tmp-${(0, import_node_crypto.randomUUID)()}`
|
|
331
|
-
);
|
|
332
|
-
await import_promises.default.mkdir(temporaryDir, { recursive: true });
|
|
333
|
-
try {
|
|
334
|
-
await Promise.all([
|
|
335
|
-
import_promises.default.writeFile(
|
|
336
|
-
import_node_path.default.join(temporaryDir, RUNTIME_EVIDENCE_PAYLOAD_FILE),
|
|
337
|
-
payload,
|
|
338
|
-
"utf8"
|
|
339
|
-
),
|
|
340
|
-
import_promises.default.writeFile(
|
|
341
|
-
import_node_path.default.join(temporaryDir, RUNTIME_EVIDENCE_MANIFEST_FILE),
|
|
342
|
-
`${JSON.stringify(manifest, null, 2)}
|
|
343
|
-
`,
|
|
344
|
-
"utf8"
|
|
345
|
-
)
|
|
346
|
-
]);
|
|
347
|
-
await import_promises.default.writeFile(
|
|
348
|
-
import_node_path.default.join(temporaryDir, RUNTIME_EVIDENCE_COMPLETE_FILE),
|
|
349
|
-
`${artifactId}
|
|
350
|
-
`,
|
|
351
|
-
"utf8"
|
|
352
|
-
);
|
|
353
|
-
await import_promises.default.mkdir(readyRoot, { recursive: true });
|
|
354
|
-
try {
|
|
355
|
-
await import_promises.default.rename(temporaryDir, bundleDir);
|
|
356
|
-
} catch (error) {
|
|
357
|
-
if (!isExistingDestinationError(error)) {
|
|
358
|
-
throw error;
|
|
359
|
-
}
|
|
360
|
-
await import_promises.default.rm(temporaryDir, { recursive: true, force: true });
|
|
361
|
-
}
|
|
362
|
-
} catch (error) {
|
|
363
|
-
await import_promises.default.rm(temporaryDir, { recursive: true, force: true });
|
|
364
|
-
throw error;
|
|
365
|
-
}
|
|
366
|
-
return verifyRuntimeEvidenceBundle(bundleDir);
|
|
367
|
-
};
|
|
368
|
-
var verifyRuntimeEvidenceBundle = async (bundleDir) => {
|
|
369
|
-
const manifestPath = import_node_path.default.join(bundleDir, RUNTIME_EVIDENCE_MANIFEST_FILE);
|
|
370
|
-
const payloadPath = import_node_path.default.join(bundleDir, RUNTIME_EVIDENCE_PAYLOAD_FILE);
|
|
371
|
-
const completePath = import_node_path.default.join(bundleDir, RUNTIME_EVIDENCE_COMPLETE_FILE);
|
|
372
|
-
const [manifestText, payload, completeText] = await Promise.all([
|
|
373
|
-
import_promises.default.readFile(manifestPath, "utf8"),
|
|
374
|
-
import_promises.default.readFile(payloadPath),
|
|
375
|
-
import_promises.default.readFile(completePath, "utf8")
|
|
376
|
-
]);
|
|
377
|
-
const manifest = parseManifest(JSON.parse(manifestText));
|
|
378
|
-
if (completeText.trim() !== manifest.artifactId) {
|
|
379
|
-
throw new Error(`Invalid completion marker in ${bundleDir}`);
|
|
380
|
-
}
|
|
381
|
-
if (payload.byteLength !== manifest.payload.bytes) {
|
|
382
|
-
throw new Error(`Runtime evidence size mismatch in ${bundleDir}`);
|
|
383
|
-
}
|
|
384
|
-
if (sha256(payload) !== manifest.payload.sha256) {
|
|
385
|
-
throw new Error(`Runtime evidence checksum mismatch in ${bundleDir}`);
|
|
386
|
-
}
|
|
387
|
-
const artifact = asRecord(JSON.parse(payload.toString("utf8")));
|
|
388
|
-
const artifactWindow = asRecord(artifact?.window);
|
|
389
|
-
if (artifact?.reportType !== "runtime-evidence" || artifactWindow?.startTime !== manifest.window.startTime || artifactWindow.endTime !== manifest.window.endTime) {
|
|
390
|
-
throw new Error(`Runtime evidence payload mismatch in ${bundleDir}`);
|
|
391
|
-
}
|
|
392
|
-
const deployment = assertCurrentRuntimeEvidenceArtifact(artifact);
|
|
393
|
-
if (deployment.id !== manifest.deploymentId) {
|
|
394
|
-
throw new Error(`Runtime evidence deployment mismatch in ${bundleDir}`);
|
|
395
|
-
}
|
|
396
|
-
return {
|
|
397
|
-
bundleDir,
|
|
398
|
-
manifestPath,
|
|
399
|
-
payloadPath,
|
|
400
|
-
manifest,
|
|
401
|
-
artifact
|
|
402
|
-
};
|
|
403
|
-
};
|
|
404
|
-
var discoverRuntimeEvidenceBundles = async (rootDir) => {
|
|
405
|
-
const bundles = [];
|
|
406
|
-
const visit = async (directory, depth) => {
|
|
407
|
-
if (depth > 8) return;
|
|
408
|
-
let entries;
|
|
409
|
-
try {
|
|
410
|
-
entries = await import_promises.default.readdir(directory, { withFileTypes: true });
|
|
411
|
-
} catch (error) {
|
|
412
|
-
if (error.code === "ENOENT") return;
|
|
413
|
-
throw error;
|
|
414
|
-
}
|
|
415
|
-
const names = new Set(entries.map((entry) => entry.name));
|
|
416
|
-
if (names.has(RUNTIME_EVIDENCE_MANIFEST_FILE) && names.has(RUNTIME_EVIDENCE_PAYLOAD_FILE) && names.has(RUNTIME_EVIDENCE_COMPLETE_FILE)) {
|
|
417
|
-
bundles.push(directory);
|
|
418
|
-
return;
|
|
419
|
-
}
|
|
420
|
-
await Promise.all(
|
|
421
|
-
entries.filter(
|
|
422
|
-
(entry) => entry.isDirectory() && !entry.name.startsWith(".") && !entry.name.includes(".tmp-")
|
|
423
|
-
).map((entry) => visit(import_node_path.default.join(directory, entry.name), depth + 1))
|
|
424
|
-
);
|
|
425
|
-
};
|
|
426
|
-
await visit(rootDir, 0);
|
|
427
|
-
return bundles.sort();
|
|
428
|
-
};
|
|
20
|
+
__reExport(runtimeEvidenceArtifacts_exports, require("@tradejs/node/diagnostics"), module.exports);
|
|
429
21
|
// Annotate the CommonJS export names for ESM import in node:
|
|
430
22
|
0 && (module.exports = {
|
|
431
|
-
|
|
432
|
-
RUNTIME_EVIDENCE_MANIFEST_FILE,
|
|
433
|
-
RUNTIME_EVIDENCE_PAYLOAD_FILE,
|
|
434
|
-
discoverRuntimeEvidenceBundles,
|
|
435
|
-
publishRuntimeEvidenceBundle,
|
|
436
|
-
verifyRuntimeEvidenceBundle
|
|
23
|
+
...require("@tradejs/node/diagnostics")
|
|
437
24
|
});
|