@tradejs/cli 3.1.21 → 3.1.22-beta.238
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/dist/cli.js +1643 -806
- package/dist/lib/runtimeEvidenceProducer.js +98 -0
- package/dist/lib/runtimeEvidenceSync.js +8 -3
- package/dist/lib/runtimeFeedbackArtifacts.js +566 -0
- package/dist/lib/runtimeFeedbackReplay.js +808 -0
- package/dist/lib/runtimeFeedbackSync.js +350 -0
- package/dist/scripts/replay.js +9 -6
- package/dist/scripts/replayRunner.js +9 -6
- package/dist/scripts/replayRuntimeEvidence.js +4 -0
- package/dist/scripts/runtimeEvidence.js +62 -7
- package/dist/scripts/runtimeFeedbackReplay.js +850 -0
- package/dist/scripts/runtimeFeedbackSync.js +411 -0
- package/dist/scripts/runtimeScorecard.js +217 -39
- package/package.json +9 -7
|
@@ -0,0 +1,808 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
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
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
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
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/lib/runtimeFeedbackReplay.ts
|
|
31
|
+
var runtimeFeedbackReplay_exports = {};
|
|
32
|
+
__export(runtimeFeedbackReplay_exports, {
|
|
33
|
+
assertRuntimeFeedbackReplaySafety: () => assertRuntimeFeedbackReplaySafety,
|
|
34
|
+
buildRuntimeFeedbackReplayCommands: () => buildRuntimeFeedbackReplayCommands,
|
|
35
|
+
runRuntimeFeedbackReplay: () => runRuntimeFeedbackReplay
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(runtimeFeedbackReplay_exports);
|
|
38
|
+
var import_node_child_process = require("child_process");
|
|
39
|
+
var import_promises4 = __toESM(require("fs/promises"));
|
|
40
|
+
var import_node_path4 = __toESM(require("path"));
|
|
41
|
+
|
|
42
|
+
// src/lib/runtimeEvidenceDeployment.ts
|
|
43
|
+
var import_runtimeStrategies = require("@tradejs/node/runtimeStrategies");
|
|
44
|
+
var isRecord = (value) => value != null && typeof value === "object" && !Array.isArray(value);
|
|
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
|
+
};
|
|
204
|
+
|
|
205
|
+
// src/lib/runtimeEvidenceArtifacts.ts
|
|
206
|
+
var import_node_crypto = require("crypto");
|
|
207
|
+
var import_promises = __toESM(require("fs/promises"));
|
|
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
|
+
};
|
|
290
|
+
|
|
291
|
+
// src/lib/runtimeEvidenceProducer.ts
|
|
292
|
+
var import_node_crypto2 = require("crypto");
|
|
293
|
+
var import_promises2 = __toESM(require("fs/promises"));
|
|
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
|
+
};
|
|
349
|
+
|
|
350
|
+
// src/lib/runtimeFeedbackArtifacts.ts
|
|
351
|
+
var import_node_crypto3 = require("crypto");
|
|
352
|
+
var import_promises3 = __toESM(require("fs/promises"));
|
|
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
|
+
};
|
|
557
|
+
|
|
558
|
+
// src/lib/runtimeFeedbackReplay.ts
|
|
559
|
+
var REQUIRED_ENV = {
|
|
560
|
+
RUNTIME_FEEDBACK_ISOLATED_REDIS: "true",
|
|
561
|
+
MAKE_ORDERS: "false",
|
|
562
|
+
TRADEJS_EXTERNAL_ORDER_PLACEMENT: "false"
|
|
563
|
+
};
|
|
564
|
+
var FORBIDDEN_SECRET_ENV = [
|
|
565
|
+
"AGENT_GITHUB_TOKEN",
|
|
566
|
+
"AI_API_KEY",
|
|
567
|
+
"ANTHROPIC_API_KEY",
|
|
568
|
+
"AUTH_SECRET",
|
|
569
|
+
"BYBIT_API_KEY",
|
|
570
|
+
"BYBIT_API_SECRET",
|
|
571
|
+
"COINALYZE_API_KEY",
|
|
572
|
+
"COINMARKETCAP_API_KEY",
|
|
573
|
+
"GITHUB_TOKEN",
|
|
574
|
+
"GIT_SSH_PRIVATE_KEY",
|
|
575
|
+
"NEXTAUTH_SECRET",
|
|
576
|
+
"NPM_TOKEN",
|
|
577
|
+
"OPENAI_API_KEY",
|
|
578
|
+
"OPENROUTER_API_KEY",
|
|
579
|
+
"SSH_PRIVATE_KEY",
|
|
580
|
+
"TG_BOT_TOKEN"
|
|
581
|
+
];
|
|
582
|
+
var unsafeRedisHosts = /* @__PURE__ */ new Set([
|
|
583
|
+
"127.0.0.1",
|
|
584
|
+
"::1",
|
|
585
|
+
"inv-redis",
|
|
586
|
+
"localhost",
|
|
587
|
+
"redis"
|
|
588
|
+
]);
|
|
589
|
+
var safeSegment2 = (value, fallback) => {
|
|
590
|
+
const normalized = value.trim().replace(/[^a-zA-Z0-9._-]+/g, "-");
|
|
591
|
+
return normalized && normalized !== "." && normalized !== ".." ? normalized : fallback;
|
|
592
|
+
};
|
|
593
|
+
var assertRuntimeFeedbackReplaySafety = (env) => {
|
|
594
|
+
for (const [name, expected] of Object.entries(REQUIRED_ENV)) {
|
|
595
|
+
if (env[name] !== expected) {
|
|
596
|
+
throw new Error(`Runtime feedback replay requires ${name}=${expected}`);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
const redisHost = String(env.REDIS_HOST ?? "").trim().toLowerCase();
|
|
600
|
+
if (!redisHost || unsafeRedisHosts.has(redisHost)) {
|
|
601
|
+
throw new Error(
|
|
602
|
+
`Runtime feedback replay requires a named isolated Redis host, received ${redisHost || "[missing]"}`
|
|
603
|
+
);
|
|
604
|
+
}
|
|
605
|
+
const pgOptions = String(env.PGOPTIONS ?? "");
|
|
606
|
+
if (!/default_transaction_read_only\s*=\s*on/i.test(pgOptions)) {
|
|
607
|
+
throw new Error(
|
|
608
|
+
"Runtime feedback replay requires PGOPTIONS with default_transaction_read_only=on"
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
if (env.TRADEJS_TIMESCALE_READ_ONLY !== "true") {
|
|
612
|
+
throw new Error(
|
|
613
|
+
"Runtime feedback replay requires TRADEJS_TIMESCALE_READ_ONLY=true"
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
const exposedSecrets = FORBIDDEN_SECRET_ENV.filter(
|
|
617
|
+
(name) => String(env[name] ?? "").trim()
|
|
618
|
+
);
|
|
619
|
+
if (exposedSecrets.length) {
|
|
620
|
+
throw new Error(
|
|
621
|
+
`Runtime feedback replay received forbidden credentials: ${exposedSecrets.join(", ")}`
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
var buildRuntimeFeedbackReplayCommands = ({
|
|
626
|
+
cliPath,
|
|
627
|
+
runtimeEvidencePath,
|
|
628
|
+
replayEvidencePath,
|
|
629
|
+
userName,
|
|
630
|
+
connectorName,
|
|
631
|
+
deploymentId,
|
|
632
|
+
interval,
|
|
633
|
+
startTime,
|
|
634
|
+
endTime
|
|
635
|
+
}) => [
|
|
636
|
+
[
|
|
637
|
+
cliPath,
|
|
638
|
+
"replay",
|
|
639
|
+
"--user",
|
|
640
|
+
userName,
|
|
641
|
+
"--connector",
|
|
642
|
+
connectorName,
|
|
643
|
+
"--deployment",
|
|
644
|
+
deploymentId,
|
|
645
|
+
"--timeframe",
|
|
646
|
+
interval,
|
|
647
|
+
"--startTime",
|
|
648
|
+
String(startTime),
|
|
649
|
+
"--endTime",
|
|
650
|
+
String(endTime),
|
|
651
|
+
"--runtimeEvidence",
|
|
652
|
+
runtimeEvidencePath,
|
|
653
|
+
"--cacheOnly"
|
|
654
|
+
],
|
|
655
|
+
[
|
|
656
|
+
cliPath,
|
|
657
|
+
"replay-runtime-evidence",
|
|
658
|
+
"--user",
|
|
659
|
+
userName,
|
|
660
|
+
"--runtimeEvidence",
|
|
661
|
+
runtimeEvidencePath,
|
|
662
|
+
"--startTime",
|
|
663
|
+
String(startTime),
|
|
664
|
+
"--endTime",
|
|
665
|
+
String(endTime),
|
|
666
|
+
"--out",
|
|
667
|
+
replayEvidencePath
|
|
668
|
+
]
|
|
669
|
+
];
|
|
670
|
+
var runLoggedCommand = async ({
|
|
671
|
+
executable,
|
|
672
|
+
command,
|
|
673
|
+
cwd,
|
|
674
|
+
env,
|
|
675
|
+
logHandle
|
|
676
|
+
}) => {
|
|
677
|
+
await logHandle.write(`
|
|
678
|
+
$ ${[executable, ...command].join(" ")}
|
|
679
|
+
`);
|
|
680
|
+
await new Promise((resolve, reject) => {
|
|
681
|
+
let pendingLogWrite = Promise.resolve();
|
|
682
|
+
const appendLog = (chunk) => {
|
|
683
|
+
pendingLogWrite = pendingLogWrite.then(async () => {
|
|
684
|
+
await logHandle.write(chunk);
|
|
685
|
+
});
|
|
686
|
+
};
|
|
687
|
+
const child = (0, import_node_child_process.spawn)(executable, command, {
|
|
688
|
+
cwd,
|
|
689
|
+
env,
|
|
690
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
691
|
+
});
|
|
692
|
+
child.stdout.on("data", (chunk) => {
|
|
693
|
+
process.stdout.write(chunk);
|
|
694
|
+
appendLog(chunk);
|
|
695
|
+
});
|
|
696
|
+
child.stderr.on("data", (chunk) => {
|
|
697
|
+
process.stderr.write(chunk);
|
|
698
|
+
appendLog(chunk);
|
|
699
|
+
});
|
|
700
|
+
child.once("error", reject);
|
|
701
|
+
child.once("close", (code, signal) => {
|
|
702
|
+
void pendingLogWrite.then(() => {
|
|
703
|
+
if (code === 0) {
|
|
704
|
+
resolve();
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
reject(
|
|
708
|
+
new Error(
|
|
709
|
+
`Runtime feedback command failed: exit=${String(code)} signal=${String(signal)}`
|
|
710
|
+
)
|
|
711
|
+
);
|
|
712
|
+
}, reject);
|
|
713
|
+
});
|
|
714
|
+
});
|
|
715
|
+
};
|
|
716
|
+
var runRuntimeFeedbackReplay = async ({
|
|
717
|
+
runtimeEvidencePath,
|
|
718
|
+
outDir,
|
|
719
|
+
runId = `${(/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-")}-${process.pid}`,
|
|
720
|
+
projectRoot,
|
|
721
|
+
env = process.env,
|
|
722
|
+
executable = process.execPath,
|
|
723
|
+
cliPath = process.argv[1]
|
|
724
|
+
}) => {
|
|
725
|
+
assertRuntimeFeedbackReplaySafety(env);
|
|
726
|
+
if (!cliPath) {
|
|
727
|
+
throw new Error("Cannot resolve the TradeJS CLI executable path");
|
|
728
|
+
}
|
|
729
|
+
const runtimeEvidenceBundle = await verifyRuntimeEvidenceBundle(
|
|
730
|
+
import_node_path4.default.dirname(runtimeEvidencePath)
|
|
731
|
+
);
|
|
732
|
+
if (import_node_path4.default.resolve(runtimeEvidenceBundle.payloadPath) !== runtimeEvidencePath) {
|
|
733
|
+
throw new Error(
|
|
734
|
+
`Runtime evidence must be the verified bundle payload: ${runtimeEvidenceBundle.payloadPath}`
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
const deployedProducer = parseRuntimeEvidenceProducer(
|
|
738
|
+
runtimeEvidenceBundle.artifact.producer
|
|
739
|
+
);
|
|
740
|
+
const currentProducer = await resolveRuntimeEvidenceProducer({
|
|
741
|
+
projectRoot,
|
|
742
|
+
required: true
|
|
743
|
+
});
|
|
744
|
+
if (!currentProducer || currentProducer.projectSha !== deployedProducer.projectSha || currentProducer.imageDigest !== deployedProducer.imageDigest || currentProducer.runtimePackageManifest.sha256 !== deployedProducer.runtimePackageManifest.sha256) {
|
|
745
|
+
throw new Error(
|
|
746
|
+
"Runtime feedback replay image identity does not match runtime evidence"
|
|
747
|
+
);
|
|
748
|
+
}
|
|
749
|
+
const deployment = parseRuntimeEvidenceDeploymentSnapshot(
|
|
750
|
+
runtimeEvidenceBundle.artifact.deployment
|
|
751
|
+
);
|
|
752
|
+
const activeStrategies = activeRuntimeEvidenceStrategies(deployment);
|
|
753
|
+
const intervals = new Set(
|
|
754
|
+
activeStrategies.map(({ interval }) => String(interval))
|
|
755
|
+
);
|
|
756
|
+
if (intervals.size !== 1) {
|
|
757
|
+
throw new Error(
|
|
758
|
+
`Runtime feedback replay requires one interval, received ${[...intervals].join(",")}`
|
|
759
|
+
);
|
|
760
|
+
}
|
|
761
|
+
await import_promises4.default.mkdir(outDir, { recursive: true });
|
|
762
|
+
const existing = await import_promises4.default.readdir(outDir);
|
|
763
|
+
if (existing.length) {
|
|
764
|
+
throw new Error(
|
|
765
|
+
`Runtime feedback output directory must be empty: ${outDir}`
|
|
766
|
+
);
|
|
767
|
+
}
|
|
768
|
+
const replayEvidencePath = import_node_path4.default.join(outDir, RUNTIME_FEEDBACK_REPLAY_FILE);
|
|
769
|
+
const logHandle = await import_promises4.default.open(
|
|
770
|
+
import_node_path4.default.join(outDir, RUNTIME_FEEDBACK_LOG_FILE),
|
|
771
|
+
"wx"
|
|
772
|
+
);
|
|
773
|
+
const commands = buildRuntimeFeedbackReplayCommands({
|
|
774
|
+
cliPath,
|
|
775
|
+
runtimeEvidencePath,
|
|
776
|
+
replayEvidencePath,
|
|
777
|
+
userName: runtimeEvidenceBundle.manifest.userName,
|
|
778
|
+
connectorName: deployment.connectorName,
|
|
779
|
+
deploymentId: deployment.id,
|
|
780
|
+
interval: [...intervals][0],
|
|
781
|
+
startTime: runtimeEvidenceBundle.manifest.window.startTime,
|
|
782
|
+
endTime: runtimeEvidenceBundle.manifest.window.endTime
|
|
783
|
+
});
|
|
784
|
+
try {
|
|
785
|
+
for (const command of commands) {
|
|
786
|
+
await runLoggedCommand({
|
|
787
|
+
executable,
|
|
788
|
+
command,
|
|
789
|
+
cwd: projectRoot,
|
|
790
|
+
env,
|
|
791
|
+
logHandle
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
} finally {
|
|
795
|
+
await logHandle.close();
|
|
796
|
+
}
|
|
797
|
+
return sealRuntimeFeedbackReplayBundle({
|
|
798
|
+
bundleDir: outDir,
|
|
799
|
+
runId: safeSegment2(runId, "run"),
|
|
800
|
+
runtimeEvidenceBundle
|
|
801
|
+
});
|
|
802
|
+
};
|
|
803
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
804
|
+
0 && (module.exports = {
|
|
805
|
+
assertRuntimeFeedbackReplaySafety,
|
|
806
|
+
buildRuntimeFeedbackReplayCommands,
|
|
807
|
+
runRuntimeFeedbackReplay
|
|
808
|
+
});
|