@tradejs/cli 3.1.29-beta.276 → 3.1.29-beta.277

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.
@@ -0,0 +1,132 @@
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 __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+
31
+ // src/lib/mcpReplayProcess.ts
32
+ var mcpReplayProcess_exports = {};
33
+ __export(mcpReplayProcess_exports, {
34
+ buildMcpReplayProcess: () => buildMcpReplayProcess
35
+ });
36
+ module.exports = __toCommonJS(mcpReplayProcess_exports);
37
+ var import_node_path = __toESM(require("path"));
38
+
39
+ // src/lib/runtimeEvidenceProducer.ts
40
+ var runtimeEvidenceProducer_exports = {};
41
+ __reExport(runtimeEvidenceProducer_exports, require("@tradejs/node/diagnostics"));
42
+
43
+ // src/lib/runtimeEvidenceArtifacts.ts
44
+ var runtimeEvidenceArtifacts_exports = {};
45
+ __reExport(runtimeEvidenceArtifacts_exports, require("@tradejs/node/diagnostics"));
46
+
47
+ // src/lib/mcpReplayProcess.ts
48
+ var buildMcpReplayProcess = async ({
49
+ evidencePath,
50
+ outDir,
51
+ runId,
52
+ projectRoot,
53
+ env
54
+ }) => {
55
+ if (!/^[a-zA-Z0-9_-]{1,100}$/.test(runId))
56
+ throw new Error("Invalid replay container run id");
57
+ const bundle = await (0, runtimeEvidenceArtifacts_exports.verifyRuntimeEvidenceBundle)(import_node_path.default.dirname(evidencePath));
58
+ if (import_node_path.default.resolve(evidencePath) !== import_node_path.default.resolve(bundle.payloadPath))
59
+ throw new Error("Replay requires the verified bundle payload");
60
+ const producer = (0, runtimeEvidenceProducer_exports.parseRuntimeEvidenceProducer)(bundle.artifact.producer);
61
+ if (!env.MCP_REPLAY_HOST_PROJECT_ROOT || !env.MCP_REPLAY_NETWORK || !env.MCP_REPLAY_IMAGE_REPOSITORY)
62
+ throw new Error(
63
+ "Image-bound replay requires MCP_REPLAY_HOST_PROJECT_ROOT MCP_REPLAY_NETWORK and MCP_REPLAY_IMAGE_REPOSITORY"
64
+ );
65
+ const hostPath = (file) => {
66
+ const relative = import_node_path.default.relative(projectRoot, file);
67
+ if (relative.startsWith("..") || import_node_path.default.isAbsolute(relative))
68
+ throw new Error("Replay mounts must be inside the Project");
69
+ return import_node_path.default.join(env.MCP_REPLAY_HOST_PROJECT_ROOT, relative);
70
+ };
71
+ const args = [
72
+ "run",
73
+ "--rm",
74
+ "--name",
75
+ `tradejs-replay-${runId}`,
76
+ "--memory=1536m",
77
+ "--cpus=1",
78
+ "--pids-limit=512",
79
+ "--read-only",
80
+ "--cap-drop=ALL",
81
+ "--security-opt=no-new-privileges",
82
+ "--network",
83
+ env.MCP_REPLAY_NETWORK,
84
+ "--tmpfs",
85
+ "/tmp",
86
+ "--tmpfs",
87
+ "/app/data",
88
+ "--entrypoint",
89
+ "/app/node_modules/.bin/tradejs",
90
+ "--mount",
91
+ `type=bind,src=${hostPath(import_node_path.default.dirname(evidencePath))},dst=/evidence,readonly`,
92
+ "--mount",
93
+ `type=bind,src=${hostPath(outDir)},dst=/replay-output`
94
+ ];
95
+ const safeEnv = {
96
+ PROJECT_CWD: "/app",
97
+ DOTENV_CONFIG_PATH: "/dev/null",
98
+ TRADEJS_PROJECT_SHA: producer.projectSha,
99
+ TRADEJS_PROJECT_IMAGE_DIGEST: producer.imageDigest,
100
+ NODE_OPTIONS: "--max-old-space-size=1024",
101
+ MAKE_ORDERS: "false",
102
+ TRADEJS_EXTERNAL_ORDER_PLACEMENT: "false",
103
+ AI_ENABLED: "false",
104
+ ML_ENABLED: "false",
105
+ RUNTIME_FEEDBACK_ISOLATED_REDIS: "true",
106
+ REDIS_HOST: env.MCP_REPLAY_REDIS_HOST,
107
+ REDIS_PORT: "6379",
108
+ REDIS_DB: "0",
109
+ PGOPTIONS: "-c default_transaction_read_only=on",
110
+ TRADEJS_TIMESCALE_READ_ONLY: "true"
111
+ };
112
+ for (const name of ["PG_HOST", "PG_PORT", "PG_USER", "PG_DATABASE", "PG_DB"])
113
+ if (env[name]) safeEnv[name] = env[name];
114
+ for (const [name, value] of Object.entries(safeEnv))
115
+ if (value !== void 0) args.push("--env", `${name}=${value}`);
116
+ if (env.PG_PASSWORD) args.push("--env", "PG_PASSWORD");
117
+ args.push(
118
+ `${env.MCP_REPLAY_IMAGE_REPOSITORY}@${producer.imageDigest}`,
119
+ "runtime-feedback-replay",
120
+ "--runtimeEvidence",
121
+ `/evidence/${import_node_path.default.basename(evidencePath)}`,
122
+ "--outDir",
123
+ "/replay-output",
124
+ "--runId",
125
+ runId
126
+ );
127
+ return { executable: "docker", args };
128
+ };
129
+ // Annotate the CommonJS export names for ESM import in node:
130
+ 0 && (module.exports = {
131
+ buildMcpReplayProcess
132
+ });
@@ -249,21 +249,38 @@ var buildStrategyMonitoringProfile = ({
249
249
 
250
250
  // src/lib/strategyRelease/evidenceRetention.ts
251
251
  var DAY_MS2 = 864e5;
252
+ var isResearchRetired = (research) => research?.status === "retired" && research.retirement?.policy === "discard-unrecoverable-without-rerun" && Boolean(research.retirement.reason.trim()) && Boolean(research.retirement.missingEvidence.length) && research.retirement.missingEvidence.every((item) => Boolean(item.trim()));
252
253
  var planStrategyEvidenceRetention = ({
253
254
  now,
254
255
  entries,
256
+ research,
255
257
  retentionDays = {
256
258
  operational_redis: 3,
257
259
  verbose_payload: 14,
260
+ research_payload: 0,
261
+ research_cache: 0,
258
262
  verified_runtime_bundle: 90,
259
263
  compact_ledger: null
260
264
  }
261
265
  }) => {
262
266
  const keep = [];
263
267
  const remove = [];
268
+ const retired = isResearchRetired(research);
269
+ const roles = new Set(
270
+ research?.permanentArtifacts.map((artifact) => artifact.role)
271
+ );
272
+ const terminalResearch = Boolean(
273
+ (research?.status === "completed" || retired) && Number.isFinite(research?.completedAt) && research.completedAt <= now && (retired ? ["note"] : ["note", "candidates", "metrics"]).every(
274
+ (role) => roles.has(role)
275
+ )
276
+ );
264
277
  for (const entry of entries) {
265
278
  const days = retentionDays[entry.kind];
266
- if (days == null || !entry.verified || !entry.aggregated || now - entry.createdAt <= days * DAY_MS2) {
279
+ const isResearch = entry.kind === "research_payload" || entry.kind === "research_cache";
280
+ const completedResearch = Boolean(
281
+ terminalResearch && research?.researchId === entry.researchId && /^[a-f0-9]{64}$/.test(entry.sha256 ?? "")
282
+ );
283
+ if (isResearch && !completedResearch || days == null || !entry.verified || !entry.aggregated && !(isResearch && retired) || !isResearch && now - entry.createdAt <= days * DAY_MS2) {
267
284
  keep.push(entry);
268
285
  } else {
269
286
  remove.push(entry);
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,15 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
18
29
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
30
 
20
31
  // src/scripts/mcpWorker.ts
@@ -28,6 +39,102 @@ var import_node_crypto = require("crypto");
28
39
  var import_promises = require("fs/promises");
29
40
  var import_redis = require("@tradejs/infra/redis");
30
41
  var import_mcp = require("@tradejs/node/mcp");
42
+
43
+ // src/lib/mcpReplayProcess.ts
44
+ var import_node_path = __toESM(require("path"));
45
+
46
+ // src/lib/runtimeEvidenceProducer.ts
47
+ var runtimeEvidenceProducer_exports = {};
48
+ __reExport(runtimeEvidenceProducer_exports, require("@tradejs/node/diagnostics"));
49
+
50
+ // src/lib/runtimeEvidenceArtifacts.ts
51
+ var runtimeEvidenceArtifacts_exports = {};
52
+ __reExport(runtimeEvidenceArtifacts_exports, require("@tradejs/node/diagnostics"));
53
+
54
+ // src/lib/mcpReplayProcess.ts
55
+ var buildMcpReplayProcess = async ({
56
+ evidencePath,
57
+ outDir,
58
+ runId,
59
+ projectRoot,
60
+ env
61
+ }) => {
62
+ if (!/^[a-zA-Z0-9_-]{1,100}$/.test(runId))
63
+ throw new Error("Invalid replay container run id");
64
+ const bundle = await (0, runtimeEvidenceArtifacts_exports.verifyRuntimeEvidenceBundle)(import_node_path.default.dirname(evidencePath));
65
+ if (import_node_path.default.resolve(evidencePath) !== import_node_path.default.resolve(bundle.payloadPath))
66
+ throw new Error("Replay requires the verified bundle payload");
67
+ const producer = (0, runtimeEvidenceProducer_exports.parseRuntimeEvidenceProducer)(bundle.artifact.producer);
68
+ if (!env.MCP_REPLAY_HOST_PROJECT_ROOT || !env.MCP_REPLAY_NETWORK || !env.MCP_REPLAY_IMAGE_REPOSITORY)
69
+ throw new Error(
70
+ "Image-bound replay requires MCP_REPLAY_HOST_PROJECT_ROOT MCP_REPLAY_NETWORK and MCP_REPLAY_IMAGE_REPOSITORY"
71
+ );
72
+ const hostPath = (file) => {
73
+ const relative = import_node_path.default.relative(projectRoot, file);
74
+ if (relative.startsWith("..") || import_node_path.default.isAbsolute(relative))
75
+ throw new Error("Replay mounts must be inside the Project");
76
+ return import_node_path.default.join(env.MCP_REPLAY_HOST_PROJECT_ROOT, relative);
77
+ };
78
+ const args = [
79
+ "run",
80
+ "--rm",
81
+ "--name",
82
+ `tradejs-replay-${runId}`,
83
+ "--memory=1536m",
84
+ "--cpus=1",
85
+ "--pids-limit=512",
86
+ "--read-only",
87
+ "--cap-drop=ALL",
88
+ "--security-opt=no-new-privileges",
89
+ "--network",
90
+ env.MCP_REPLAY_NETWORK,
91
+ "--tmpfs",
92
+ "/tmp",
93
+ "--tmpfs",
94
+ "/app/data",
95
+ "--entrypoint",
96
+ "/app/node_modules/.bin/tradejs",
97
+ "--mount",
98
+ `type=bind,src=${hostPath(import_node_path.default.dirname(evidencePath))},dst=/evidence,readonly`,
99
+ "--mount",
100
+ `type=bind,src=${hostPath(outDir)},dst=/replay-output`
101
+ ];
102
+ const safeEnv = {
103
+ PROJECT_CWD: "/app",
104
+ DOTENV_CONFIG_PATH: "/dev/null",
105
+ TRADEJS_PROJECT_SHA: producer.projectSha,
106
+ TRADEJS_PROJECT_IMAGE_DIGEST: producer.imageDigest,
107
+ NODE_OPTIONS: "--max-old-space-size=1024",
108
+ MAKE_ORDERS: "false",
109
+ TRADEJS_EXTERNAL_ORDER_PLACEMENT: "false",
110
+ AI_ENABLED: "false",
111
+ ML_ENABLED: "false",
112
+ RUNTIME_FEEDBACK_ISOLATED_REDIS: "true",
113
+ REDIS_HOST: env.MCP_REPLAY_REDIS_HOST,
114
+ REDIS_PORT: "6379",
115
+ REDIS_DB: "0",
116
+ PGOPTIONS: "-c default_transaction_read_only=on",
117
+ TRADEJS_TIMESCALE_READ_ONLY: "true"
118
+ };
119
+ for (const name of ["PG_HOST", "PG_PORT", "PG_USER", "PG_DATABASE", "PG_DB"])
120
+ if (env[name]) safeEnv[name] = env[name];
121
+ for (const [name, value] of Object.entries(safeEnv))
122
+ if (value !== void 0) args.push("--env", `${name}=${value}`);
123
+ if (env.PG_PASSWORD) args.push("--env", "PG_PASSWORD");
124
+ args.push(
125
+ `${env.MCP_REPLAY_IMAGE_REPOSITORY}@${producer.imageDigest}`,
126
+ "runtime-feedback-replay",
127
+ "--runtimeEvidence",
128
+ `/evidence/${import_node_path.default.basename(evidencePath)}`,
129
+ "--outDir",
130
+ "/replay-output",
131
+ "--runId",
132
+ runId
133
+ );
134
+ return { executable: "docker", args };
135
+ };
136
+
137
+ // src/scripts/mcpWorker.ts
31
138
  var pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
32
139
  var leaseKey = "worker:lease";
33
140
  var LEASE_SECONDS = 15;
@@ -54,7 +161,9 @@ var main = async (options = {}) => {
54
161
  const heartbeatValue = () => ({
55
162
  observedAt: Date.now(),
56
163
  packageManifestSha256: manifestHash,
57
- replayAvailable: Boolean(process.env.MCP_REPLAY_REDIS_HOST)
164
+ replayAvailable: Boolean(
165
+ process.env.MCP_REPLAY_REDIS_HOST && process.env.MCP_REPLAY_HOST_PROJECT_ROOT && process.env.MCP_REPLAY_NETWORK && process.env.MCP_REPLAY_IMAGE_REPOSITORY
166
+ )
58
167
  });
59
168
  const stop = () => {
60
169
  stopping = true;
@@ -180,7 +289,37 @@ var main = async (options = {}) => {
180
289
  TRADEJS_TIMESCALE_READ_ONLY: "true"
181
290
  });
182
291
  }
183
- child = (0, import_node_child_process.spawn)(process.execPath, [process.argv[1], ...started.args], {
292
+ let command = {
293
+ executable: process.execPath,
294
+ args: [process.argv[1], ...started.args]
295
+ };
296
+ try {
297
+ if (started.kind === "runtime-feedback-replay")
298
+ command = await buildMcpReplayProcess({
299
+ evidencePath: String(started.request.verifiedEvidencePath),
300
+ outDir: (0, import_mcp.mcpJobDirectory)(started),
301
+ runId: started.id,
302
+ projectRoot: process.env.PROJECT_CWD || process.cwd(),
303
+ env: process.env
304
+ });
305
+ } catch (error) {
306
+ await import_redis.mcpStorage.compare(
307
+ jobKey,
308
+ started,
309
+ {
310
+ ...started,
311
+ status: "failed",
312
+ finishedAt: Date.now(),
313
+ updatedAt: Date.now(),
314
+ error: error instanceof Error ? error.message : "Replay preparation failed"
315
+ },
316
+ import_mcp.MCP_JOB_TTL
317
+ );
318
+ continue;
319
+ }
320
+ if (started.kind === "runtime-feedback-replay" && process.env.PG_PASSWORD)
321
+ jobEnv.PG_PASSWORD = process.env.PG_PASSWORD;
322
+ child = (0, import_node_child_process.spawn)(command.executable, command.args, {
184
323
  cwd: process.env.PROJECT_CWD || process.cwd(),
185
324
  env: jobEnv,
186
325
  detached: process.platform !== "win32",
@@ -234,6 +373,20 @@ var main = async (options = {}) => {
234
373
  );
235
374
  currentChild.once("exit", (code) => resolve({ code }));
236
375
  });
376
+ if (started.kind === "runtime-feedback-replay") {
377
+ await new Promise((resolve) => {
378
+ const cleanup = (0, import_node_child_process.spawn)(
379
+ "docker",
380
+ ["rm", "--force", `tradejs-replay-${started.id}`],
381
+ {
382
+ stdio: "ignore",
383
+ timeout: 1e4
384
+ }
385
+ );
386
+ cleanup.once("error", () => resolve());
387
+ cleanup.once("close", () => resolve());
388
+ });
389
+ }
237
390
  clearTimeout(timeout);
238
391
  clearInterval(monitor);
239
392
  child = void 0;