@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
|
@@ -17,6 +17,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
}
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
20
21
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
22
|
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
23
|
// file that has been converted to a CommonJS file using a Babel-
|
|
@@ -36,524 +37,24 @@ __export(runtimeFeedbackReplay_exports, {
|
|
|
36
37
|
});
|
|
37
38
|
module.exports = __toCommonJS(runtimeFeedbackReplay_exports);
|
|
38
39
|
var import_node_child_process = require("child_process");
|
|
39
|
-
var
|
|
40
|
-
var
|
|
40
|
+
var import_promises = __toESM(require("fs/promises"));
|
|
41
|
+
var import_node_path = __toESM(require("path"));
|
|
41
42
|
|
|
42
43
|
// src/lib/runtimeEvidenceDeployment.ts
|
|
43
|
-
var
|
|
44
|
-
|
|
45
|
-
var isNonEmptyString = (value) => typeof value === "string" && value.trim().length > 0;
|
|
46
|
-
var isPackageVersionMap = (value) => isRecord(value) && Object.keys(value).length > 0 && Object.entries(value).every(
|
|
47
|
-
([name, version]) => name.startsWith("@tradejs/") && isNonEmptyString(version)
|
|
48
|
-
);
|
|
49
|
-
var INTERVALS = /* @__PURE__ */ new Set([
|
|
50
|
-
"1",
|
|
51
|
-
"3",
|
|
52
|
-
"5",
|
|
53
|
-
"15",
|
|
54
|
-
"30",
|
|
55
|
-
"60",
|
|
56
|
-
"120",
|
|
57
|
-
"240",
|
|
58
|
-
"360",
|
|
59
|
-
"720",
|
|
60
|
-
"D",
|
|
61
|
-
"W",
|
|
62
|
-
"M"
|
|
63
|
-
]);
|
|
64
|
-
var UNIVERSES = /* @__PURE__ */ new Set(["crypto", "tradfi"]);
|
|
65
|
-
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));
|
|
66
|
-
var samePackageVersions = (left, right) => {
|
|
67
|
-
if (!left || !right) return false;
|
|
68
|
-
const leftEntries = Object.entries(left).sort(
|
|
69
|
-
([a], [b]) => a.localeCompare(b)
|
|
70
|
-
);
|
|
71
|
-
const rightEntries = Object.entries(right).sort(
|
|
72
|
-
([a], [b]) => a.localeCompare(b)
|
|
73
|
-
);
|
|
74
|
-
return JSON.stringify(leftEntries) === JSON.stringify(rightEntries);
|
|
75
|
-
};
|
|
76
|
-
var runtimeLineageMatchesStrategySnapshot = ({
|
|
77
|
-
lineage,
|
|
78
|
-
deployment,
|
|
79
|
-
strategy
|
|
80
|
-
}) => lineage?.schemaVersion === 3 && lineage.strategyRevision === strategy.strategyRevision && lineage.deploymentCompositionId === deployment.deploymentCompositionId && lineage.strategyPackageVersion === strategy.strategyPackageVersion && lineage.runtimePackageVersion === strategy.runtimePackageVersion && samePackageVersions(
|
|
81
|
-
lineage.strategyDependencyVersions,
|
|
82
|
-
strategy.strategyDependencyVersions
|
|
83
|
-
);
|
|
84
|
-
var parseRuntimeEvidenceDeploymentSnapshot = (value) => {
|
|
85
|
-
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)) {
|
|
86
|
-
throw new Error(
|
|
87
|
-
"Runtime evidence deployment snapshot is missing or invalid"
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
const strategies = value.strategies.map((candidate) => {
|
|
91
|
-
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)) {
|
|
92
|
-
throw new Error(
|
|
93
|
-
"Runtime evidence deployment strategy snapshot is invalid"
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
if (String(candidate.strategyConfig.INTERVAL) !== String(candidate.interval) || String(candidate.strategyConfig.UNIVERSE) !== String(candidate.universe)) {
|
|
97
|
-
throw new Error(
|
|
98
|
-
`Runtime evidence strategy config does not match its scope: ${candidate.strategyName}`
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
if (candidate.accountId != null && (!isNonEmptyString(candidate.accountId) || candidate.accountId !== value.accountId)) {
|
|
102
|
-
throw new Error(
|
|
103
|
-
`Runtime evidence strategy account does not match deployment: ${candidate.strategyName}`
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
return candidate;
|
|
107
|
-
});
|
|
108
|
-
const identities = /* @__PURE__ */ new Set();
|
|
109
|
-
for (const strategy of strategies) {
|
|
110
|
-
if (identities.has(strategy.strategyName)) {
|
|
111
|
-
throw new Error(
|
|
112
|
-
`Duplicate runtime evidence strategy: ${strategy.strategyName}`
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
identities.add(strategy.strategyName);
|
|
116
|
-
}
|
|
117
|
-
return {
|
|
118
|
-
...value,
|
|
119
|
-
strategies
|
|
120
|
-
};
|
|
121
|
-
};
|
|
122
|
-
var activeRuntimeEvidenceStrategies = (deployment) => deployment.enabled ? deployment.strategies.filter((strategy) => strategy.enabled) : [];
|
|
123
|
-
var unwrapRuntimeEvidenceRows = (value, field) => (Array.isArray(value) ? value : []).map((candidate) => {
|
|
124
|
-
const record = isRecord(candidate) ? candidate : null;
|
|
125
|
-
const nested = record && isRecord(record[field]) ? record[field] : null;
|
|
126
|
-
return nested ?? record;
|
|
127
|
-
}).filter((record) => record != null);
|
|
128
|
-
var assertCurrentRuntimeEvidenceArtifact = (artifact) => {
|
|
129
|
-
if (!isRecord(artifact) || artifact.reportType !== "runtime-evidence") {
|
|
130
|
-
throw new Error("Invalid runtime evidence artifact");
|
|
131
|
-
}
|
|
132
|
-
const deployment = parseRuntimeEvidenceDeploymentSnapshot(
|
|
133
|
-
artifact.deployment
|
|
134
|
-
);
|
|
135
|
-
const activeStrategies = activeRuntimeEvidenceStrategies(deployment);
|
|
136
|
-
if (activeStrategies.length === 0) {
|
|
137
|
-
throw new Error("Runtime evidence deployment has no enabled strategies");
|
|
138
|
-
}
|
|
139
|
-
const runtime = isRecord(artifact.runtime) ? artifact.runtime : null;
|
|
140
|
-
if (!runtime) {
|
|
141
|
-
throw new Error("Runtime evidence payload is missing");
|
|
142
|
-
}
|
|
143
|
-
if ("strategyConfigs" in runtime) {
|
|
144
|
-
throw new Error(
|
|
145
|
-
"Runtime evidence payload contains removed strategyConfigs data"
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
if ("evaluationStatsBuckets" in runtime) {
|
|
149
|
-
throw new Error(
|
|
150
|
-
"Runtime evidence payload contains removed lineage-less evaluationStatsBuckets data"
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
const rowFields = [
|
|
154
|
-
"trades",
|
|
155
|
-
"signals",
|
|
156
|
-
"evaluations",
|
|
157
|
-
"lineageScopes"
|
|
158
|
-
];
|
|
159
|
-
for (const field of rowFields) {
|
|
160
|
-
if (!Array.isArray(runtime[field])) {
|
|
161
|
-
throw new Error(`Runtime evidence payload ${field} must be an array`);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
const snapshots = new Map(
|
|
165
|
-
activeStrategies.map((strategy) => [strategy.strategyName, strategy])
|
|
166
|
-
);
|
|
167
|
-
const assertRows = (label, rows, lineageField) => {
|
|
168
|
-
for (const row of rows) {
|
|
169
|
-
const strategyName = String(row.strategy ?? "").trim();
|
|
170
|
-
const strategy = snapshots.get(strategyName);
|
|
171
|
-
if (!strategy || row.deploymentId !== deployment.id || row.accountId !== deployment.accountId || !runtimeLineageMatchesStrategySnapshot({
|
|
172
|
-
lineage: isRecord(row[lineageField]) ? row[lineageField] : void 0,
|
|
173
|
-
deployment,
|
|
174
|
-
strategy
|
|
175
|
-
})) {
|
|
176
|
-
throw new Error(
|
|
177
|
-
`Runtime evidence ${label} row is outside the current embedded deployment lineage`
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
};
|
|
182
|
-
assertRows(
|
|
183
|
-
"trade",
|
|
184
|
-
unwrapRuntimeEvidenceRows(runtime.trades, "trade"),
|
|
185
|
-
"runtimeLineage"
|
|
186
|
-
);
|
|
187
|
-
assertRows(
|
|
188
|
-
"signal",
|
|
189
|
-
unwrapRuntimeEvidenceRows(runtime.signals, "signal"),
|
|
190
|
-
"runtimeLineage"
|
|
191
|
-
);
|
|
192
|
-
assertRows(
|
|
193
|
-
"evaluation",
|
|
194
|
-
unwrapRuntimeEvidenceRows(runtime.evaluations, "evaluation"),
|
|
195
|
-
"runtimeLineage"
|
|
196
|
-
);
|
|
197
|
-
assertRows(
|
|
198
|
-
"lineage scope",
|
|
199
|
-
unwrapRuntimeEvidenceRows(runtime.lineageScopes, "lineageScope"),
|
|
200
|
-
"lineage"
|
|
201
|
-
);
|
|
202
|
-
return deployment;
|
|
203
|
-
};
|
|
44
|
+
var runtimeEvidenceDeployment_exports = {};
|
|
45
|
+
__reExport(runtimeEvidenceDeployment_exports, require("@tradejs/node/diagnostics"));
|
|
204
46
|
|
|
205
47
|
// src/lib/runtimeEvidenceArtifacts.ts
|
|
206
|
-
var
|
|
207
|
-
|
|
208
|
-
var import_node_path = __toESM(require("path"));
|
|
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 assertFiniteTimestamp = (value, label) => {
|
|
215
|
-
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
216
|
-
throw new Error(`Invalid runtime evidence ${label}`);
|
|
217
|
-
}
|
|
218
|
-
return value;
|
|
219
|
-
};
|
|
220
|
-
var parseManifest = (value) => {
|
|
221
|
-
const manifest = asRecord(value);
|
|
222
|
-
const window = asRecord(manifest?.window);
|
|
223
|
-
const payload = asRecord(manifest?.payload);
|
|
224
|
-
const counts = asRecord(manifest?.counts);
|
|
225
|
-
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)) {
|
|
226
|
-
throw new Error("Invalid runtime evidence manifest");
|
|
227
|
-
}
|
|
228
|
-
return {
|
|
229
|
-
schemaVersion: 1,
|
|
230
|
-
artifactId: manifest.artifactId,
|
|
231
|
-
reportType: "runtime-evidence",
|
|
232
|
-
deploymentId: manifest.deploymentId,
|
|
233
|
-
userName: manifest.userName,
|
|
234
|
-
createdAt: manifest.createdAt,
|
|
235
|
-
window: {
|
|
236
|
-
startTime: assertFiniteTimestamp(window?.startTime, "window.startTime"),
|
|
237
|
-
endTime: assertFiniteTimestamp(window?.endTime, "window.endTime")
|
|
238
|
-
},
|
|
239
|
-
payload: {
|
|
240
|
-
file: RUNTIME_EVIDENCE_PAYLOAD_FILE,
|
|
241
|
-
sha256: payload.sha256,
|
|
242
|
-
bytes: payload.bytes
|
|
243
|
-
},
|
|
244
|
-
counts: Object.fromEntries(
|
|
245
|
-
Object.entries(counts ?? {}).filter(
|
|
246
|
-
(entry) => typeof entry[1] === "number" && Number.isFinite(entry[1])
|
|
247
|
-
)
|
|
248
|
-
),
|
|
249
|
-
lineageKeys: manifest.lineageKeys.filter(
|
|
250
|
-
(value2) => typeof value2 === "string" && value2.length > 0
|
|
251
|
-
)
|
|
252
|
-
};
|
|
253
|
-
};
|
|
254
|
-
var verifyRuntimeEvidenceBundle = async (bundleDir) => {
|
|
255
|
-
const manifestPath = import_node_path.default.join(bundleDir, RUNTIME_EVIDENCE_MANIFEST_FILE);
|
|
256
|
-
const payloadPath = import_node_path.default.join(bundleDir, RUNTIME_EVIDENCE_PAYLOAD_FILE);
|
|
257
|
-
const completePath = import_node_path.default.join(bundleDir, RUNTIME_EVIDENCE_COMPLETE_FILE);
|
|
258
|
-
const [manifestText, payload, completeText] = await Promise.all([
|
|
259
|
-
import_promises.default.readFile(manifestPath, "utf8"),
|
|
260
|
-
import_promises.default.readFile(payloadPath),
|
|
261
|
-
import_promises.default.readFile(completePath, "utf8")
|
|
262
|
-
]);
|
|
263
|
-
const manifest = parseManifest(JSON.parse(manifestText));
|
|
264
|
-
if (completeText.trim() !== manifest.artifactId) {
|
|
265
|
-
throw new Error(`Invalid completion marker in ${bundleDir}`);
|
|
266
|
-
}
|
|
267
|
-
if (payload.byteLength !== manifest.payload.bytes) {
|
|
268
|
-
throw new Error(`Runtime evidence size mismatch in ${bundleDir}`);
|
|
269
|
-
}
|
|
270
|
-
if (sha256(payload) !== manifest.payload.sha256) {
|
|
271
|
-
throw new Error(`Runtime evidence checksum mismatch in ${bundleDir}`);
|
|
272
|
-
}
|
|
273
|
-
const artifact = asRecord(JSON.parse(payload.toString("utf8")));
|
|
274
|
-
const artifactWindow = asRecord(artifact?.window);
|
|
275
|
-
if (artifact?.reportType !== "runtime-evidence" || artifactWindow?.startTime !== manifest.window.startTime || artifactWindow.endTime !== manifest.window.endTime) {
|
|
276
|
-
throw new Error(`Runtime evidence payload mismatch in ${bundleDir}`);
|
|
277
|
-
}
|
|
278
|
-
const deployment = assertCurrentRuntimeEvidenceArtifact(artifact);
|
|
279
|
-
if (deployment.id !== manifest.deploymentId) {
|
|
280
|
-
throw new Error(`Runtime evidence deployment mismatch in ${bundleDir}`);
|
|
281
|
-
}
|
|
282
|
-
return {
|
|
283
|
-
bundleDir,
|
|
284
|
-
manifestPath,
|
|
285
|
-
payloadPath,
|
|
286
|
-
manifest,
|
|
287
|
-
artifact
|
|
288
|
-
};
|
|
289
|
-
};
|
|
48
|
+
var runtimeEvidenceArtifacts_exports = {};
|
|
49
|
+
__reExport(runtimeEvidenceArtifacts_exports, require("@tradejs/node/diagnostics"));
|
|
290
50
|
|
|
291
51
|
// src/lib/runtimeEvidenceProducer.ts
|
|
292
|
-
var
|
|
293
|
-
|
|
294
|
-
var import_node_path2 = __toESM(require("path"));
|
|
295
|
-
var isRecord2 = (value) => value != null && typeof value === "object" && !Array.isArray(value);
|
|
296
|
-
var sha2562 = (value) => (0, import_node_crypto2.createHash)("sha256").update(value).digest("hex");
|
|
297
|
-
var parseRuntimeEvidenceProducer = (value) => {
|
|
298
|
-
const producer = isRecord2(value) ? value : null;
|
|
299
|
-
const runtimePackageManifest = isRecord2(producer?.runtimePackageManifest) ? producer.runtimePackageManifest : null;
|
|
300
|
-
if (producer?.schemaVersion !== 1 || typeof producer.projectSha !== "string" || !/^[a-f0-9]{40}$/.test(producer.projectSha) || typeof producer.imageDigest !== "string" || !/^sha256:[a-f0-9]{64}$/.test(producer.imageDigest) || runtimePackageManifest?.file !== "runtime-package-manifest.json" || typeof runtimePackageManifest.sha256 !== "string" || !/^[a-f0-9]{64}$/.test(runtimePackageManifest.sha256)) {
|
|
301
|
-
throw new Error("Runtime evidence producer image identity is invalid");
|
|
302
|
-
}
|
|
303
|
-
return producer;
|
|
304
|
-
};
|
|
305
|
-
var resolveRuntimeEvidenceProducer = async ({
|
|
306
|
-
projectRoot,
|
|
307
|
-
required
|
|
308
|
-
}) => {
|
|
309
|
-
const projectSha = String(process.env.TRADEJS_PROJECT_SHA ?? "").trim();
|
|
310
|
-
const imageDigest = String(
|
|
311
|
-
process.env.TRADEJS_PROJECT_IMAGE_DIGEST ?? ""
|
|
312
|
-
).trim();
|
|
313
|
-
if (!projectSha && !imageDigest && !required) {
|
|
314
|
-
return null;
|
|
315
|
-
}
|
|
316
|
-
if (!/^[a-f0-9]{40}$/.test(projectSha)) {
|
|
317
|
-
throw new Error(
|
|
318
|
-
"TRADEJS_PROJECT_SHA must be a full lowercase Git SHA before publishing runtime evidence"
|
|
319
|
-
);
|
|
320
|
-
}
|
|
321
|
-
if (!/^sha256:[a-f0-9]{64}$/.test(imageDigest)) {
|
|
322
|
-
throw new Error(
|
|
323
|
-
"TRADEJS_PROJECT_IMAGE_DIGEST must be an immutable sha256 digest before publishing runtime evidence"
|
|
324
|
-
);
|
|
325
|
-
}
|
|
326
|
-
const manifestPath = import_node_path2.default.join(projectRoot, "runtime-package-manifest.json");
|
|
327
|
-
let manifest;
|
|
328
|
-
try {
|
|
329
|
-
manifest = await import_promises2.default.readFile(manifestPath);
|
|
330
|
-
} catch (error) {
|
|
331
|
-
if (!required && error.code === "ENOENT") {
|
|
332
|
-
return null;
|
|
333
|
-
}
|
|
334
|
-
throw new Error(
|
|
335
|
-
`Runtime package manifest is required for runtime evidence: ${manifestPath}`,
|
|
336
|
-
{ cause: error }
|
|
337
|
-
);
|
|
338
|
-
}
|
|
339
|
-
return {
|
|
340
|
-
schemaVersion: 1,
|
|
341
|
-
projectSha,
|
|
342
|
-
imageDigest,
|
|
343
|
-
runtimePackageManifest: {
|
|
344
|
-
file: "runtime-package-manifest.json",
|
|
345
|
-
sha256: sha2562(manifest)
|
|
346
|
-
}
|
|
347
|
-
};
|
|
348
|
-
};
|
|
52
|
+
var runtimeEvidenceProducer_exports = {};
|
|
53
|
+
__reExport(runtimeEvidenceProducer_exports, require("@tradejs/node/diagnostics"));
|
|
349
54
|
|
|
350
55
|
// src/lib/runtimeFeedbackArtifacts.ts
|
|
351
|
-
var
|
|
352
|
-
|
|
353
|
-
var import_node_path3 = __toESM(require("path"));
|
|
354
|
-
var RUNTIME_FEEDBACK_MANIFEST_FILE = "manifest.json";
|
|
355
|
-
var RUNTIME_FEEDBACK_REPLAY_FILE = "replay-runtime-evidence.json";
|
|
356
|
-
var RUNTIME_FEEDBACK_LOG_FILE = "replay.log";
|
|
357
|
-
var RUNTIME_FEEDBACK_COMPLETE_FILE = ".complete";
|
|
358
|
-
var isRecord3 = (value) => value != null && typeof value === "object" && !Array.isArray(value);
|
|
359
|
-
var sha2563 = (value) => (0, import_node_crypto3.createHash)("sha256").update(value).digest("hex");
|
|
360
|
-
var safeSegment = (value, fallback) => {
|
|
361
|
-
const normalized = value.trim().replace(/[^a-zA-Z0-9._-]+/g, "-");
|
|
362
|
-
return normalized && normalized !== "." && normalized !== ".." ? normalized : fallback;
|
|
363
|
-
};
|
|
364
|
-
var parsePayload = (value, expectedFile) => {
|
|
365
|
-
const payload = isRecord3(value) ? value : null;
|
|
366
|
-
if (payload?.file !== expectedFile || typeof payload.sha256 !== "string" || !/^[a-f0-9]{64}$/.test(payload.sha256) || typeof payload.bytes !== "number" || !Number.isFinite(payload.bytes) || payload.bytes < 0) {
|
|
367
|
-
throw new Error(`Invalid runtime feedback payload: ${expectedFile}`);
|
|
368
|
-
}
|
|
369
|
-
return payload;
|
|
370
|
-
};
|
|
371
|
-
var parseManifest2 = (value) => {
|
|
372
|
-
const manifest = isRecord3(value) ? value : null;
|
|
373
|
-
const window = isRecord3(manifest?.window) ? manifest.window : null;
|
|
374
|
-
const runtimeEvidence = isRecord3(manifest?.runtimeEvidence) ? manifest.runtimeEvidence : null;
|
|
375
|
-
const safety = isRecord3(manifest?.safety) ? manifest.safety : null;
|
|
376
|
-
const payloads = isRecord3(manifest?.payloads) ? manifest.payloads : null;
|
|
377
|
-
if (manifest?.schemaVersion !== 1 || manifest.reportType !== "runtime-feedback-replay" || typeof manifest.artifactId !== "string" || !manifest.artifactId.trim() || typeof manifest.runId !== "string" || !manifest.runId.trim() || typeof manifest.deploymentId !== "string" || !manifest.deploymentId.trim() || typeof manifest.deploymentCompositionId !== "string" || !/^dc1:[a-f0-9]{16}$/.test(manifest.deploymentCompositionId) || typeof manifest.userName !== "string" || !manifest.userName.trim() || typeof manifest.createdAt !== "number" || typeof window?.startTime !== "number" || typeof window.endTime !== "number" || typeof runtimeEvidence?.artifactId !== "string" || typeof runtimeEvidence.payloadSha256 !== "string" || !/^[a-f0-9]{64}$/.test(runtimeEvidence.payloadSha256) || !Array.isArray(manifest.activeStrategies) || manifest.activeStrategies.length === 0 || !manifest.activeStrategies.every(
|
|
378
|
-
(strategy) => typeof strategy === "string" && strategy.trim()
|
|
379
|
-
) || typeof manifest.cycleCount !== "number" || !Number.isFinite(manifest.cycleCount) || manifest.cycleCount <= 0 || safety?.cacheOnly !== true || safety.externalOrderPlacement !== false || safety.redis !== "isolated" || safety.timescale !== "read_only") {
|
|
380
|
-
throw new Error("Invalid runtime feedback replay manifest");
|
|
381
|
-
}
|
|
382
|
-
const replayEvidence = parsePayload(
|
|
383
|
-
payloads?.replayEvidence,
|
|
384
|
-
RUNTIME_FEEDBACK_REPLAY_FILE
|
|
385
|
-
);
|
|
386
|
-
const log = parsePayload(payloads?.log, RUNTIME_FEEDBACK_LOG_FILE);
|
|
387
|
-
return {
|
|
388
|
-
...manifest,
|
|
389
|
-
producer: parseRuntimeEvidenceProducer(manifest.producer),
|
|
390
|
-
payloads: {
|
|
391
|
-
replayEvidence: {
|
|
392
|
-
...replayEvidence,
|
|
393
|
-
file: RUNTIME_FEEDBACK_REPLAY_FILE
|
|
394
|
-
},
|
|
395
|
-
log: { ...log, file: RUNTIME_FEEDBACK_LOG_FILE }
|
|
396
|
-
}
|
|
397
|
-
};
|
|
398
|
-
};
|
|
399
|
-
var assertReplayMatchesRuntimeEvidence = ({
|
|
400
|
-
replayEvidence,
|
|
401
|
-
runtimeEvidence
|
|
402
|
-
}) => {
|
|
403
|
-
if (replayEvidence.reportType !== "replay-runtime-evidence") {
|
|
404
|
-
throw new Error("Runtime feedback replay payload has the wrong reportType");
|
|
405
|
-
}
|
|
406
|
-
const replayWindow = isRecord3(replayEvidence.window) ? replayEvidence.window : null;
|
|
407
|
-
if (replayWindow?.startTime !== runtimeEvidence.manifest.window.startTime || replayWindow.endTime !== runtimeEvidence.manifest.window.endTime) {
|
|
408
|
-
throw new Error("Runtime feedback replay window does not match evidence");
|
|
409
|
-
}
|
|
410
|
-
const runtimeDeployment = parseRuntimeEvidenceDeploymentSnapshot(
|
|
411
|
-
runtimeEvidence.artifact.deployment
|
|
412
|
-
);
|
|
413
|
-
const replayDeployment = parseRuntimeEvidenceDeploymentSnapshot(
|
|
414
|
-
replayEvidence.deployment
|
|
415
|
-
);
|
|
416
|
-
if (replayDeployment.id !== runtimeDeployment.id || replayDeployment.deploymentCompositionId !== runtimeDeployment.deploymentCompositionId) {
|
|
417
|
-
throw new Error(
|
|
418
|
-
"Runtime feedback replay deployment does not match evidence"
|
|
419
|
-
);
|
|
420
|
-
}
|
|
421
|
-
};
|
|
422
|
-
var sealRuntimeFeedbackReplayBundle = async ({
|
|
423
|
-
bundleDir,
|
|
424
|
-
runId,
|
|
425
|
-
runtimeEvidenceBundle
|
|
426
|
-
}) => {
|
|
427
|
-
const replayEvidencePath = import_node_path3.default.join(bundleDir, RUNTIME_FEEDBACK_REPLAY_FILE);
|
|
428
|
-
const logPath = import_node_path3.default.join(bundleDir, RUNTIME_FEEDBACK_LOG_FILE);
|
|
429
|
-
const [replayPayload, logPayload] = await Promise.all([
|
|
430
|
-
import_promises3.default.readFile(replayEvidencePath),
|
|
431
|
-
import_promises3.default.readFile(logPath)
|
|
432
|
-
]);
|
|
433
|
-
const replayEvidence = JSON.parse(replayPayload.toString("utf8"));
|
|
434
|
-
assertReplayMatchesRuntimeEvidence({
|
|
435
|
-
replayEvidence,
|
|
436
|
-
runtimeEvidence: runtimeEvidenceBundle
|
|
437
|
-
});
|
|
438
|
-
const replay = isRecord3(replayEvidence.replay) ? replayEvidence.replay : null;
|
|
439
|
-
const cycleCount = Number(replay?.cycleCount);
|
|
440
|
-
if (!Number.isFinite(cycleCount) || cycleCount <= 0) {
|
|
441
|
-
throw new Error(
|
|
442
|
-
`Runtime feedback replay produced no comparable cycles: ${String(replay?.cycleCount ?? "missing")}`
|
|
443
|
-
);
|
|
444
|
-
}
|
|
445
|
-
const deployment = parseRuntimeEvidenceDeploymentSnapshot(
|
|
446
|
-
runtimeEvidenceBundle.artifact.deployment
|
|
447
|
-
);
|
|
448
|
-
const producer = parseRuntimeEvidenceProducer(
|
|
449
|
-
runtimeEvidenceBundle.artifact.producer
|
|
450
|
-
);
|
|
451
|
-
const safeRunId = safeSegment(runId, "run");
|
|
452
|
-
const artifactId = `${runtimeEvidenceBundle.manifest.artifactId}_${safeRunId}`;
|
|
453
|
-
const manifest = {
|
|
454
|
-
schemaVersion: 1,
|
|
455
|
-
reportType: "runtime-feedback-replay",
|
|
456
|
-
artifactId,
|
|
457
|
-
runId: safeRunId,
|
|
458
|
-
deploymentId: deployment.id,
|
|
459
|
-
deploymentCompositionId: deployment.deploymentCompositionId,
|
|
460
|
-
userName: runtimeEvidenceBundle.manifest.userName,
|
|
461
|
-
createdAt: Date.now(),
|
|
462
|
-
window: { ...runtimeEvidenceBundle.manifest.window },
|
|
463
|
-
runtimeEvidence: {
|
|
464
|
-
artifactId: runtimeEvidenceBundle.manifest.artifactId,
|
|
465
|
-
payloadSha256: runtimeEvidenceBundle.manifest.payload.sha256
|
|
466
|
-
},
|
|
467
|
-
producer,
|
|
468
|
-
activeStrategies: activeRuntimeEvidenceStrategies(deployment).map(
|
|
469
|
-
({ strategyName }) => strategyName
|
|
470
|
-
),
|
|
471
|
-
cycleCount,
|
|
472
|
-
safety: {
|
|
473
|
-
cacheOnly: true,
|
|
474
|
-
externalOrderPlacement: false,
|
|
475
|
-
redis: "isolated",
|
|
476
|
-
timescale: "read_only"
|
|
477
|
-
},
|
|
478
|
-
payloads: {
|
|
479
|
-
replayEvidence: {
|
|
480
|
-
file: RUNTIME_FEEDBACK_REPLAY_FILE,
|
|
481
|
-
sha256: sha2563(replayPayload),
|
|
482
|
-
bytes: replayPayload.byteLength
|
|
483
|
-
},
|
|
484
|
-
log: {
|
|
485
|
-
file: RUNTIME_FEEDBACK_LOG_FILE,
|
|
486
|
-
sha256: sha2563(logPayload),
|
|
487
|
-
bytes: logPayload.byteLength
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
};
|
|
491
|
-
await import_promises3.default.writeFile(
|
|
492
|
-
import_node_path3.default.join(bundleDir, RUNTIME_FEEDBACK_MANIFEST_FILE),
|
|
493
|
-
`${JSON.stringify(manifest, null, 2)}
|
|
494
|
-
`,
|
|
495
|
-
"utf8"
|
|
496
|
-
);
|
|
497
|
-
await import_promises3.default.writeFile(
|
|
498
|
-
import_node_path3.default.join(bundleDir, RUNTIME_FEEDBACK_COMPLETE_FILE),
|
|
499
|
-
`${artifactId}
|
|
500
|
-
`,
|
|
501
|
-
"utf8"
|
|
502
|
-
);
|
|
503
|
-
return verifyRuntimeFeedbackReplayBundle(bundleDir);
|
|
504
|
-
};
|
|
505
|
-
var verifyRuntimeFeedbackReplayBundle = async (bundleDir) => {
|
|
506
|
-
const manifestPath = import_node_path3.default.join(bundleDir, RUNTIME_FEEDBACK_MANIFEST_FILE);
|
|
507
|
-
const replayEvidencePath = import_node_path3.default.join(bundleDir, RUNTIME_FEEDBACK_REPLAY_FILE);
|
|
508
|
-
const logPath = import_node_path3.default.join(bundleDir, RUNTIME_FEEDBACK_LOG_FILE);
|
|
509
|
-
const completePath = import_node_path3.default.join(bundleDir, RUNTIME_FEEDBACK_COMPLETE_FILE);
|
|
510
|
-
const [manifestText, replayPayload, logPayload, completeText] = await Promise.all([
|
|
511
|
-
import_promises3.default.readFile(manifestPath, "utf8"),
|
|
512
|
-
import_promises3.default.readFile(replayEvidencePath),
|
|
513
|
-
import_promises3.default.readFile(logPath),
|
|
514
|
-
import_promises3.default.readFile(completePath, "utf8")
|
|
515
|
-
]);
|
|
516
|
-
const manifest = parseManifest2(JSON.parse(manifestText));
|
|
517
|
-
if (completeText.trim() !== manifest.artifactId) {
|
|
518
|
-
throw new Error(`Invalid runtime feedback completion marker: ${bundleDir}`);
|
|
519
|
-
}
|
|
520
|
-
for (const [payload, descriptor] of [
|
|
521
|
-
[replayPayload, manifest.payloads.replayEvidence],
|
|
522
|
-
[logPayload, manifest.payloads.log]
|
|
523
|
-
]) {
|
|
524
|
-
if (payload.byteLength !== descriptor.bytes) {
|
|
525
|
-
throw new Error(
|
|
526
|
-
`Runtime feedback payload size mismatch: ${descriptor.file}`
|
|
527
|
-
);
|
|
528
|
-
}
|
|
529
|
-
if (sha2563(payload) !== descriptor.sha256) {
|
|
530
|
-
throw new Error(
|
|
531
|
-
`Runtime feedback payload checksum mismatch: ${descriptor.file}`
|
|
532
|
-
);
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
const replayEvidence = JSON.parse(replayPayload.toString("utf8"));
|
|
536
|
-
if (replayEvidence.reportType !== "replay-runtime-evidence") {
|
|
537
|
-
throw new Error("Invalid runtime feedback replay evidence payload");
|
|
538
|
-
}
|
|
539
|
-
const replayWindow = isRecord3(replayEvidence.window) ? replayEvidence.window : null;
|
|
540
|
-
const replay = isRecord3(replayEvidence.replay) ? replayEvidence.replay : null;
|
|
541
|
-
const replayDeployment = parseRuntimeEvidenceDeploymentSnapshot(
|
|
542
|
-
replayEvidence.deployment
|
|
543
|
-
);
|
|
544
|
-
const replayStrategies = activeRuntimeEvidenceStrategies(replayDeployment).map(({ strategyName }) => strategyName).sort();
|
|
545
|
-
if (replayEvidence.userName !== manifest.userName || replayWindow?.startTime !== manifest.window.startTime || replayWindow.endTime !== manifest.window.endTime || replayDeployment.id !== manifest.deploymentId || replayDeployment.deploymentCompositionId !== manifest.deploymentCompositionId || Number(replay?.cycleCount) !== manifest.cycleCount || JSON.stringify(replayStrategies) !== JSON.stringify([...manifest.activeStrategies].sort())) {
|
|
546
|
-
throw new Error("Runtime feedback replay payload does not match manifest");
|
|
547
|
-
}
|
|
548
|
-
return {
|
|
549
|
-
bundleDir,
|
|
550
|
-
manifestPath,
|
|
551
|
-
replayEvidencePath,
|
|
552
|
-
logPath,
|
|
553
|
-
manifest,
|
|
554
|
-
replayEvidence
|
|
555
|
-
};
|
|
556
|
-
};
|
|
56
|
+
var runtimeFeedbackArtifacts_exports = {};
|
|
57
|
+
__reExport(runtimeFeedbackArtifacts_exports, require("@tradejs/node/diagnostics"));
|
|
557
58
|
|
|
558
59
|
// src/lib/runtimeFeedbackReplay.ts
|
|
559
60
|
var REQUIRED_ENV = {
|
|
@@ -586,7 +87,7 @@ var unsafeRedisHosts = /* @__PURE__ */ new Set([
|
|
|
586
87
|
"localhost",
|
|
587
88
|
"redis"
|
|
588
89
|
]);
|
|
589
|
-
var
|
|
90
|
+
var safeSegment = (value, fallback) => {
|
|
590
91
|
const normalized = value.trim().replace(/[^a-zA-Z0-9._-]+/g, "-");
|
|
591
92
|
return normalized && normalized !== "." && normalized !== ".." ? normalized : fallback;
|
|
592
93
|
};
|
|
@@ -726,18 +227,18 @@ var runRuntimeFeedbackReplay = async ({
|
|
|
726
227
|
if (!cliPath) {
|
|
727
228
|
throw new Error("Cannot resolve the TradeJS CLI executable path");
|
|
728
229
|
}
|
|
729
|
-
const runtimeEvidenceBundle = await verifyRuntimeEvidenceBundle(
|
|
730
|
-
|
|
230
|
+
const runtimeEvidenceBundle = await (0, runtimeEvidenceArtifacts_exports.verifyRuntimeEvidenceBundle)(
|
|
231
|
+
import_node_path.default.dirname(runtimeEvidencePath)
|
|
731
232
|
);
|
|
732
|
-
if (
|
|
233
|
+
if (import_node_path.default.resolve(runtimeEvidenceBundle.payloadPath) !== runtimeEvidencePath) {
|
|
733
234
|
throw new Error(
|
|
734
235
|
`Runtime evidence must be the verified bundle payload: ${runtimeEvidenceBundle.payloadPath}`
|
|
735
236
|
);
|
|
736
237
|
}
|
|
737
|
-
const deployedProducer = parseRuntimeEvidenceProducer(
|
|
238
|
+
const deployedProducer = (0, runtimeEvidenceProducer_exports.parseRuntimeEvidenceProducer)(
|
|
738
239
|
runtimeEvidenceBundle.artifact.producer
|
|
739
240
|
);
|
|
740
|
-
const currentProducer = await resolveRuntimeEvidenceProducer({
|
|
241
|
+
const currentProducer = await (0, runtimeEvidenceProducer_exports.resolveRuntimeEvidenceProducer)({
|
|
741
242
|
projectRoot,
|
|
742
243
|
required: true
|
|
743
244
|
});
|
|
@@ -746,10 +247,10 @@ var runRuntimeFeedbackReplay = async ({
|
|
|
746
247
|
"Runtime feedback replay image identity does not match runtime evidence"
|
|
747
248
|
);
|
|
748
249
|
}
|
|
749
|
-
const deployment = parseRuntimeEvidenceDeploymentSnapshot(
|
|
250
|
+
const deployment = (0, runtimeEvidenceDeployment_exports.parseRuntimeEvidenceDeploymentSnapshot)(
|
|
750
251
|
runtimeEvidenceBundle.artifact.deployment
|
|
751
252
|
);
|
|
752
|
-
const activeStrategies = activeRuntimeEvidenceStrategies(deployment);
|
|
253
|
+
const activeStrategies = (0, runtimeEvidenceDeployment_exports.activeRuntimeEvidenceStrategies)(deployment);
|
|
753
254
|
const intervals = new Set(
|
|
754
255
|
activeStrategies.map(({ interval }) => String(interval))
|
|
755
256
|
);
|
|
@@ -758,16 +259,16 @@ var runRuntimeFeedbackReplay = async ({
|
|
|
758
259
|
`Runtime feedback replay requires one interval, received ${[...intervals].join(",")}`
|
|
759
260
|
);
|
|
760
261
|
}
|
|
761
|
-
await
|
|
762
|
-
const existing = await
|
|
262
|
+
await import_promises.default.mkdir(outDir, { recursive: true });
|
|
263
|
+
const existing = await import_promises.default.readdir(outDir);
|
|
763
264
|
if (existing.length) {
|
|
764
265
|
throw new Error(
|
|
765
266
|
`Runtime feedback output directory must be empty: ${outDir}`
|
|
766
267
|
);
|
|
767
268
|
}
|
|
768
|
-
const replayEvidencePath =
|
|
769
|
-
const logHandle = await
|
|
770
|
-
|
|
269
|
+
const replayEvidencePath = import_node_path.default.join(outDir, runtimeFeedbackArtifacts_exports.RUNTIME_FEEDBACK_REPLAY_FILE);
|
|
270
|
+
const logHandle = await import_promises.default.open(
|
|
271
|
+
import_node_path.default.join(outDir, runtimeFeedbackArtifacts_exports.RUNTIME_FEEDBACK_LOG_FILE),
|
|
771
272
|
"wx"
|
|
772
273
|
);
|
|
773
274
|
const commands = buildRuntimeFeedbackReplayCommands({
|
|
@@ -794,9 +295,9 @@ var runRuntimeFeedbackReplay = async ({
|
|
|
794
295
|
} finally {
|
|
795
296
|
await logHandle.close();
|
|
796
297
|
}
|
|
797
|
-
return sealRuntimeFeedbackReplayBundle({
|
|
298
|
+
return (0, runtimeFeedbackArtifacts_exports.sealRuntimeFeedbackReplayBundle)({
|
|
798
299
|
bundleDir: outDir,
|
|
799
|
-
runId:
|
|
300
|
+
runId: safeSegment(runId, "run"),
|
|
800
301
|
runtimeEvidenceBundle
|
|
801
302
|
});
|
|
802
303
|
};
|