create-tradejs 3.1.29-beta.269 → 3.1.29-beta.271
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/skill-bundle/.codex/skills/ai-train-local-research/scripts/decision-capture-join.mjs +50 -30
- package/dist/skill-bundle/.codex/skills/ai-train-local-research/scripts/decision-capture-join.test.mjs +42 -0
- package/dist/skill-bundle/.codex/skills/strategy-improvement-research/scripts/executed-composition-report.mjs +74 -9
- package/dist/skill-bundle/.codex/skills/strategy-improvement-research/scripts/executed-composition-report.test.mjs +279 -7
- package/dist/skill-bundle/.codex/tradejs-skill-bundle.json +5 -5
- package/package.json +1 -1
package/dist/skill-bundle/.codex/skills/ai-train-local-research/scripts/decision-capture-join.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import path from 'node:path';
|
|
|
3
3
|
import readline from 'node:readline';
|
|
4
4
|
import { createHash } from 'node:crypto';
|
|
5
5
|
import { pathToFileURL } from 'node:url';
|
|
6
|
+
import { createGunzip } from 'node:zlib';
|
|
6
7
|
|
|
7
8
|
export const sha256 = (bytes) =>
|
|
8
9
|
createHash('sha256').update(bytes).digest('hex');
|
|
@@ -77,48 +78,67 @@ export const readJsonLines = async function* (file) {
|
|
|
77
78
|
reader.close();
|
|
78
79
|
}
|
|
79
80
|
};
|
|
80
|
-
//
|
|
81
|
+
// Offsets refer to decoded canonical LF/UTF8 bytes. Gzip is sequential-only.
|
|
81
82
|
export const scanJsonLines = async function* (
|
|
82
83
|
file,
|
|
83
|
-
{
|
|
84
|
+
{
|
|
85
|
+
maxLineBytes = 1048576,
|
|
86
|
+
maxDecodedBytes = 16 * 1024 ** 3,
|
|
87
|
+
canonical = true,
|
|
88
|
+
} = {},
|
|
84
89
|
) {
|
|
85
90
|
if (!Number.isSafeInteger(maxLineBytes) || maxLineBytes <= 0)
|
|
86
91
|
throw new Error('Invalid JSON line byte bound');
|
|
92
|
+
if (!Number.isSafeInteger(maxDecodedBytes) || maxDecodedBytes <= 0)
|
|
93
|
+
throw new Error('Invalid decoded JSONL byte bound');
|
|
87
94
|
let pending = Buffer.alloc(0);
|
|
88
95
|
let offset = 0;
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
96
|
+
let decodedBytes = 0;
|
|
97
|
+
const source = fs.createReadStream(file, { highWaterMark: 65536 });
|
|
98
|
+
const decoder = file.endsWith('.gz') ? createGunzip() : null;
|
|
99
|
+
if (decoder) source.on('error', (error) => decoder.destroy(error));
|
|
100
|
+
const input = decoder ? source.pipe(decoder) : source;
|
|
101
|
+
try {
|
|
102
|
+
for await (const chunk of input) {
|
|
103
|
+
decodedBytes += chunk.length;
|
|
104
|
+
if (decodedBytes > maxDecodedBytes)
|
|
105
|
+
throw new Error('Decoded JSONL byte bound exceeded');
|
|
106
|
+
let start = 0;
|
|
107
|
+
for (let end = 0; end < chunk.length; end++) {
|
|
108
|
+
if (chunk[end] !== 10) continue;
|
|
109
|
+
const length = pending.length + end - start;
|
|
110
|
+
if (length > maxLineBytes)
|
|
111
|
+
throw new Error('JSON line byte bound exceeded');
|
|
112
|
+
const bytes = Buffer.concat([pending, chunk.subarray(start, end)]);
|
|
113
|
+
if (!length || bytes.includes(13))
|
|
114
|
+
throw new Error('Canonical LF JSONL required');
|
|
115
|
+
const text = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
116
|
+
const row = JSON.parse(text);
|
|
117
|
+
if (canonical && canonicalJson(row) !== text)
|
|
118
|
+
throw new Error('Noncanonical capture JSON');
|
|
119
|
+
yield {
|
|
120
|
+
row,
|
|
121
|
+
offset,
|
|
122
|
+
length: length + 1,
|
|
123
|
+
sha256: sha256(Buffer.concat([bytes, Buffer.from('\n')])),
|
|
124
|
+
};
|
|
125
|
+
offset += length + 1;
|
|
126
|
+
pending = Buffer.alloc(0);
|
|
127
|
+
start = end + 1;
|
|
128
|
+
}
|
|
129
|
+
if (pending.length + chunk.length - start > maxLineBytes)
|
|
97
130
|
throw new Error('JSON line byte bound exceeded');
|
|
98
|
-
|
|
99
|
-
if (!length || bytes.includes(13))
|
|
100
|
-
throw new Error('Canonical LF JSONL required');
|
|
101
|
-
const text = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
102
|
-
const row = JSON.parse(text);
|
|
103
|
-
if (canonical && canonicalJson(row) !== text)
|
|
104
|
-
throw new Error('Noncanonical capture JSON');
|
|
105
|
-
yield {
|
|
106
|
-
row,
|
|
107
|
-
offset,
|
|
108
|
-
length: length + 1,
|
|
109
|
-
sha256: sha256(Buffer.concat([bytes, Buffer.from('\n')])),
|
|
110
|
-
};
|
|
111
|
-
offset += length + 1;
|
|
112
|
-
pending = Buffer.alloc(0);
|
|
113
|
-
start = end + 1;
|
|
131
|
+
pending = Buffer.concat([pending, chunk.subarray(start)]);
|
|
114
132
|
}
|
|
115
|
-
if (pending.length
|
|
116
|
-
|
|
117
|
-
|
|
133
|
+
if (pending.length) throw new Error('Unterminated JSONL record');
|
|
134
|
+
} finally {
|
|
135
|
+
source.destroy();
|
|
136
|
+
decoder?.destroy();
|
|
118
137
|
}
|
|
119
|
-
if (pending.length) throw new Error('Unterminated JSONL record');
|
|
120
138
|
};
|
|
121
139
|
export const readIndexedCapture = (file, entry, maxLineBytes = 1048576) => {
|
|
140
|
+
if (file.endsWith('.gz'))
|
|
141
|
+
throw new Error('Compressed indexed capture is unsupported');
|
|
122
142
|
if (
|
|
123
143
|
!Number.isSafeInteger(entry.offset) ||
|
|
124
144
|
entry.offset < 0 ||
|
|
@@ -4,6 +4,7 @@ import fs from 'node:fs';
|
|
|
4
4
|
import os from 'node:os';
|
|
5
5
|
import path from 'node:path';
|
|
6
6
|
import { fileURLToPath } from 'node:url';
|
|
7
|
+
import { gzipSync } from 'node:zlib';
|
|
7
8
|
import {
|
|
8
9
|
createShardedCaptureSink,
|
|
9
10
|
readStorageManifest,
|
|
@@ -20,6 +21,47 @@ import {
|
|
|
20
21
|
readIndexedCapture,
|
|
21
22
|
} from './decision-capture-join.mjs';
|
|
22
23
|
|
|
24
|
+
test('bounded sequential gzip scan preserves exact decoded JSONL records', async (t) => {
|
|
25
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'capture-gzip-'));
|
|
26
|
+
t.after(() => fs.rmSync(dir, { recursive: true }));
|
|
27
|
+
const bytes = Buffer.from('{"a":1}\n{"a":2}\n');
|
|
28
|
+
const plain = path.join(dir, 'rows.jsonl');
|
|
29
|
+
const gzip = plain + '.gz';
|
|
30
|
+
fs.writeFileSync(plain, bytes);
|
|
31
|
+
fs.writeFileSync(gzip, gzipSync(bytes));
|
|
32
|
+
const collect = async (file, options) => {
|
|
33
|
+
const rows = [];
|
|
34
|
+
for await (const row of scanJsonLines(file, options)) rows.push(row);
|
|
35
|
+
return rows;
|
|
36
|
+
};
|
|
37
|
+
assert.deepEqual(await collect(gzip), await collect(plain));
|
|
38
|
+
await assert.rejects(
|
|
39
|
+
collect(gzip, { maxDecodedBytes: bytes.length - 1 }),
|
|
40
|
+
/Decoded JSONL byte bound/,
|
|
41
|
+
);
|
|
42
|
+
assert.throws(
|
|
43
|
+
() => readIndexedCapture(gzip, { offset: 0, length: 8 }),
|
|
44
|
+
/Compressed indexed capture/,
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('sequential gzip scan rejects truncated and noncanonical input', async (t) => {
|
|
49
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'capture-gzip-invalid-'));
|
|
50
|
+
t.after(() => fs.rmSync(dir, { recursive: true }));
|
|
51
|
+
const collect = async (file) => {
|
|
52
|
+
for await (const _row of scanJsonLines(file)) {
|
|
53
|
+
/* exhaust */
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
const broken = path.join(dir, 'broken.jsonl.gz');
|
|
57
|
+
const bytes = gzipSync(Buffer.from('{"a":1}\n'));
|
|
58
|
+
fs.writeFileSync(broken, bytes.subarray(0, bytes.length - 4));
|
|
59
|
+
await assert.rejects(collect(broken));
|
|
60
|
+
const invalid = path.join(dir, 'invalid.jsonl.gz');
|
|
61
|
+
fs.writeFileSync(invalid, gzipSync(Buffer.from('{"z":1,"a":2}\n')));
|
|
62
|
+
await assert.rejects(collect(invalid), /Noncanonical/);
|
|
63
|
+
});
|
|
64
|
+
|
|
23
65
|
const fixture = (t) => {
|
|
24
66
|
const projectRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'capture-join-'));
|
|
25
67
|
t.after(() => fs.rmSync(projectRoot, { recursive: true }));
|
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
} from '../../ai-train-local-research/scripts/decision-capture-storage.mjs';
|
|
24
24
|
import {
|
|
25
25
|
deriveFeeEntryStressNotional,
|
|
26
|
+
getEntryStressNotional,
|
|
26
27
|
ensureRuntimeBuild,
|
|
27
28
|
normalizeSymbolCohorts,
|
|
28
29
|
verifyEntryNotionalAuthority,
|
|
@@ -395,7 +396,19 @@ export const verifyExecutedCompositionAuthority = async ({
|
|
|
395
396
|
);
|
|
396
397
|
const expectedBySymbol = new Map();
|
|
397
398
|
for (const entry of checkpoint.value) {
|
|
398
|
-
const
|
|
399
|
+
const rawStat = entry.result.stat;
|
|
400
|
+
// Native TestConnector.getResult returns this exact short form when its
|
|
401
|
+
// closed position log is empty. Recognize that documented zero locally;
|
|
402
|
+
// never rewrite the checksum-bound checkpoint or infer absent records.
|
|
403
|
+
const nativeEmpty =
|
|
404
|
+
rawStat &&
|
|
405
|
+
same(Object.keys(rawStat).sort(), ['amount', 'orders', 'profit']) &&
|
|
406
|
+
rawStat.amount === 100 &&
|
|
407
|
+
rawStat.orders === 0 &&
|
|
408
|
+
rawStat.profit === 0;
|
|
409
|
+
const stat = nativeEmpty
|
|
410
|
+
? { orders: 0, wins: 0, losses: 0, netProfit: 0 }
|
|
411
|
+
: rawStat;
|
|
399
412
|
demand(
|
|
400
413
|
stat &&
|
|
401
414
|
['orders', 'wins', 'losses'].every(
|
|
@@ -404,6 +417,14 @@ export const verifyExecutedCompositionAuthority = async ({
|
|
|
404
417
|
finite(stat.netProfit),
|
|
405
418
|
'Native checkpoint economic stats unavailable',
|
|
406
419
|
);
|
|
420
|
+
demand(
|
|
421
|
+
stat.orders !== 0 ||
|
|
422
|
+
(stat.wins === 0 &&
|
|
423
|
+
stat.losses === 0 &&
|
|
424
|
+
stat.netProfit === 0 &&
|
|
425
|
+
(!Object.hasOwn(rawStat, 'profit') || rawStat.profit === 0)),
|
|
426
|
+
'Native checkpoint economic stats unavailable',
|
|
427
|
+
);
|
|
407
428
|
demand(stat.wins + stat.losses <= stat.orders, 'Invalid native flat count');
|
|
408
429
|
expectedBySymbol.set(entry.result.test.symbol, {
|
|
409
430
|
trades: stat.orders,
|
|
@@ -472,12 +493,17 @@ export const verifyExecutedCompositionAuthority = async ({
|
|
|
472
493
|
if (a.runtimeCheckpoint && a.strategyArtifactAuthority) {
|
|
473
494
|
await load('runtimeCheckpoint');
|
|
474
495
|
await load('strategyArtifactAuthority');
|
|
475
|
-
|
|
496
|
+
const resolvedRuntime = await (
|
|
476
497
|
validators.ensureRuntimeBuild ?? ensureRuntimeBuild
|
|
477
498
|
)(a.frameworkRepositoryRoot, {
|
|
478
499
|
projectRoot,
|
|
479
500
|
runtimeCheckpoint: a.runtimeCheckpoint.path,
|
|
480
501
|
});
|
|
502
|
+
executionRuntime = resolvedRuntime?.executionRuntime;
|
|
503
|
+
demand(
|
|
504
|
+
executionRuntime && typeof executionRuntime === 'object',
|
|
505
|
+
'Missing verified checkpoint execution runtime',
|
|
506
|
+
);
|
|
481
507
|
(
|
|
482
508
|
validators.verifyStrategyArtifactAuthority ??
|
|
483
509
|
verifyStrategyArtifactAuthority
|
|
@@ -1480,12 +1506,21 @@ export const deriveExecutedCompositionReport = async ({
|
|
|
1480
1506
|
counters.newTailTestFinalEligibleCompletedCount++;
|
|
1481
1507
|
if (label.newTail && label.newTailIntersection === null)
|
|
1482
1508
|
counters.newTailUnknownPartitionCount++;
|
|
1509
|
+
const actualEntryNotional = verified.entryNotional
|
|
1510
|
+
? null
|
|
1511
|
+
: getEntryStressNotional(result);
|
|
1483
1512
|
const notional = verified.entryNotional
|
|
1484
1513
|
? deriveFeeEntryStressNotional(result, verified.entryNotional)
|
|
1485
|
-
:
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1514
|
+
: finite(actualEntryNotional) && actualEntryNotional > 0
|
|
1515
|
+
? {
|
|
1516
|
+
status: 'complete',
|
|
1517
|
+
notional: actualEntryNotional,
|
|
1518
|
+
reason: null,
|
|
1519
|
+
}
|
|
1520
|
+
: {
|
|
1521
|
+
status: 'missing',
|
|
1522
|
+
reason: 'missing-or-invalid-actual-entry-notional',
|
|
1523
|
+
};
|
|
1489
1524
|
if (exit !== null)
|
|
1490
1525
|
await sorter.append(
|
|
1491
1526
|
[
|
|
@@ -1513,10 +1548,38 @@ export const deriveExecutedCompositionReport = async ({
|
|
|
1513
1548
|
},
|
|
1514
1549
|
);
|
|
1515
1550
|
}
|
|
1551
|
+
const unmatchedExecutedClosures = {
|
|
1552
|
+
count: 0,
|
|
1553
|
+
status: 'NONE',
|
|
1554
|
+
classification: 'unknown-native-completed-closure',
|
|
1555
|
+
identities: [],
|
|
1556
|
+
identitiesComplete: true,
|
|
1557
|
+
maxExampleIdentities: 100,
|
|
1558
|
+
evidence: 'Unchanged checksum-bound execution streams and compact index',
|
|
1559
|
+
meaning:
|
|
1560
|
+
'Observed successful own entry without either native completed-closure trace. Not a closed trade, pending-position proof, nonfill, zero PnL or realized loss.',
|
|
1561
|
+
};
|
|
1562
|
+
for await (const entry of indexes.execution.entries()) {
|
|
1563
|
+
const key = entry.row.key;
|
|
1564
|
+
const hasEntry = indexes.entryTrace.get(key) !== null;
|
|
1565
|
+
const hasExit = indexes.exitTrace.get(key) !== null;
|
|
1566
|
+
demand(hasEntry === hasExit, 'One-sided native closure trace');
|
|
1567
|
+
if (!hasEntry) {
|
|
1568
|
+
unmatchedExecutedClosures.count++;
|
|
1569
|
+
if (unmatchedExecutedClosures.identities.length < 100)
|
|
1570
|
+
unmatchedExecutedClosures.identities.push(entry.row.value);
|
|
1571
|
+
else unmatchedExecutedClosures.identitiesComplete = false;
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
if (unmatchedExecutedClosures.count) {
|
|
1575
|
+
unmatchedExecutedClosures.status = 'UNKNOWN';
|
|
1576
|
+
blockers.push('successful-entry-without-native-completed-closure');
|
|
1577
|
+
}
|
|
1516
1578
|
demand(
|
|
1517
1579
|
native.trades === indexes.exitTrace.rows &&
|
|
1518
1580
|
native.trades === indexes.entryTrace.rows &&
|
|
1519
|
-
native.trades
|
|
1581
|
+
native.trades + unmatchedExecutedClosures.count ===
|
|
1582
|
+
indexes.execution.rows,
|
|
1520
1583
|
'Completed/execution/entry/exit-trace count mismatch',
|
|
1521
1584
|
);
|
|
1522
1585
|
const expected = a.nativeReconciliation;
|
|
@@ -1829,8 +1892,9 @@ export const deriveExecutedCompositionReport = async ({
|
|
|
1829
1892
|
'exitTimestamp grouped; stable run/test/symbol/signal/invocation loss-streak ties',
|
|
1830
1893
|
occupancy: 'actual closed-loop execution, not conditional posthoc subset',
|
|
1831
1894
|
eventSupport: 'unique original decision timestamps',
|
|
1832
|
-
stress:
|
|
1833
|
-
'extra20bps fee-derived entry-notional; same identities/qty/occupancy'
|
|
1895
|
+
stress: verified.entryNotional
|
|
1896
|
+
? 'extra20bps verified fee-derived entry-notional; same identities/qty/occupancy'
|
|
1897
|
+
: 'extra20bps closedQty (or qty when absent) * actual entryPrice; same identities/qty/occupancy',
|
|
1834
1898
|
},
|
|
1835
1899
|
matrices: {
|
|
1836
1900
|
observed: {
|
|
@@ -1856,6 +1920,7 @@ export const deriveExecutedCompositionReport = async ({
|
|
|
1856
1920
|
economicBoundary: 'exact-END native only',
|
|
1857
1921
|
},
|
|
1858
1922
|
funnel,
|
|
1923
|
+
unmatchedExecutedClosures,
|
|
1859
1924
|
decisionFunnel,
|
|
1860
1925
|
crossing: counters,
|
|
1861
1926
|
fieldAvailability: availability,
|
|
@@ -6,6 +6,7 @@ import fs from 'node:fs';
|
|
|
6
6
|
import os from 'node:os';
|
|
7
7
|
import path from 'node:path';
|
|
8
8
|
import { fileURLToPath } from 'node:url';
|
|
9
|
+
import { gzipSync } from 'node:zlib';
|
|
9
10
|
import {
|
|
10
11
|
canonicalJson,
|
|
11
12
|
objectSha256,
|
|
@@ -48,6 +49,7 @@ export const executedFixture = (
|
|
|
48
49
|
id = 'candidate',
|
|
49
50
|
sharedRoot,
|
|
50
51
|
selectionAuthority,
|
|
52
|
+
nativeEmptyStats = false,
|
|
51
53
|
} = {},
|
|
52
54
|
) => {
|
|
53
55
|
const root =
|
|
@@ -215,6 +217,20 @@ export const executedFixture = (
|
|
|
215
217
|
? { gateObservationSha256: objectSha256(gate) }
|
|
216
218
|
: {}),
|
|
217
219
|
});
|
|
220
|
+
if (tr.unmatchedClosure) {
|
|
221
|
+
if (tr.unmatchedClosure !== true) {
|
|
222
|
+
const entryOnly = tr.unmatchedClosure === 'entry-only';
|
|
223
|
+
stages[entryOnly ? 'entryTrace' : 'exitTrace'].push({
|
|
224
|
+
...identity,
|
|
225
|
+
timestamp: identity.timestamp,
|
|
226
|
+
invocationId,
|
|
227
|
+
event: entryOnly ? 'entry_executed' : 'position_exited',
|
|
228
|
+
backtestRunId: 'native-run',
|
|
229
|
+
backtestTestKey: identity.symbol,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
218
234
|
for (const [name, event] of [
|
|
219
235
|
['entryTrace', 'entry_executed'],
|
|
220
236
|
['exitTrace', 'position_exited'],
|
|
@@ -264,12 +280,23 @@ export const executedFixture = (
|
|
|
264
280
|
backtestTestKey: base.symbol,
|
|
265
281
|
chunkId: `${base.symbol}-chunk`,
|
|
266
282
|
},
|
|
267
|
-
stat:
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
283
|
+
stat:
|
|
284
|
+
nativeEmptyStats && rows.length === 0
|
|
285
|
+
? {
|
|
286
|
+
amount: 100,
|
|
287
|
+
profit: 0,
|
|
288
|
+
orders: 0,
|
|
289
|
+
}
|
|
290
|
+
: {
|
|
291
|
+
orders: rows.length,
|
|
292
|
+
wins: rows.filter((r) => r.tradeResult.netProfit > 0).length,
|
|
293
|
+
losses: rows.filter((r) => r.tradeResult.netProfit < 0)
|
|
294
|
+
.length,
|
|
295
|
+
netProfit: rows.reduce(
|
|
296
|
+
(n, r) => n + r.tradeResult.netProfit,
|
|
297
|
+
0,
|
|
298
|
+
),
|
|
299
|
+
},
|
|
273
300
|
},
|
|
274
301
|
};
|
|
275
302
|
}),
|
|
@@ -368,7 +395,9 @@ export const executedFixture = (
|
|
|
368
395
|
rootReview: artifact,
|
|
369
396
|
});
|
|
370
397
|
const validators = {
|
|
371
|
-
ensureRuntimeBuild: async () => ({
|
|
398
|
+
ensureRuntimeBuild: async () => ({
|
|
399
|
+
executionRuntime: { checkpoint: artifact, packages: {} },
|
|
400
|
+
}),
|
|
372
401
|
verifyStrategyArtifactAuthority: () => ({}),
|
|
373
402
|
verifyEntryNotionalAuthority: async () => ({
|
|
374
403
|
method: 'fee-derived-full-closed',
|
|
@@ -396,6 +425,200 @@ const derive = (fixture, outputPrefix = 'report') =>
|
|
|
396
425
|
validators: fixture.validators,
|
|
397
426
|
outputPrefix,
|
|
398
427
|
});
|
|
428
|
+
|
|
429
|
+
test('gzip-only transport preserves actual matrices, fills and native reconciliation', async (t) => {
|
|
430
|
+
const f = executedFixture(t, { trades: [{ pnl: 3 }, { pnl: -2 }] });
|
|
431
|
+
const plain = await derive(f, 'plain-report');
|
|
432
|
+
for (const stream of [
|
|
433
|
+
...Object.values(f.authority.streams),
|
|
434
|
+
f.authority.completedStream,
|
|
435
|
+
]) {
|
|
436
|
+
for (const file of stream.files) {
|
|
437
|
+
const compressed = gzipSync(
|
|
438
|
+
fs.readFileSync(path.join(f.root, file.path)),
|
|
439
|
+
);
|
|
440
|
+
file.path += '.gz';
|
|
441
|
+
fs.writeFileSync(path.join(f.root, file.path), compressed, {
|
|
442
|
+
flag: 'wx',
|
|
443
|
+
});
|
|
444
|
+
file.sha256 = sha256(compressed);
|
|
445
|
+
file.bytes = compressed.length;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
f.descriptor = f.json('gzip-authority.json', f.authority);
|
|
449
|
+
const compressed = await derive(f, 'gzip-report');
|
|
450
|
+
assert.deepEqual(compressed.report.matrices, plain.report.matrices);
|
|
451
|
+
assert.deepEqual(compressed.report.funnel, plain.report.funnel);
|
|
452
|
+
assert.deepEqual(
|
|
453
|
+
compressed.report.nativeReconciliation,
|
|
454
|
+
plain.report.nativeReconciliation,
|
|
455
|
+
);
|
|
456
|
+
assert.deepEqual(
|
|
457
|
+
compressed.report.selectionDecision,
|
|
458
|
+
plain.report.selectionDecision,
|
|
459
|
+
);
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
test('successful own entry without either native closure trace remains explicit UNKNOWN', async (t) => {
|
|
463
|
+
const f = executedFixture(t, { trades: [{}, { unmatchedClosure: true }] });
|
|
464
|
+
const before = fs.readFileSync(
|
|
465
|
+
path.join(f.root, f.authority.streams.execution.files[0].path),
|
|
466
|
+
);
|
|
467
|
+
const { report } = await derive(f);
|
|
468
|
+
assert.equal(report.authorityStatus, 'UNKNOWN');
|
|
469
|
+
assert.equal(report.selectionDecision.status, 'UNKNOWN');
|
|
470
|
+
assert.equal(report.nativeReconciliation.trades, 1);
|
|
471
|
+
assert.equal(report.nativeReconciliation.status, 'MATCH');
|
|
472
|
+
assert.equal(report.funnel.execution, 2);
|
|
473
|
+
assert.equal(report.unmatchedExecutedClosures.count, 1);
|
|
474
|
+
assert.equal(report.unmatchedExecutedClosures.status, 'UNKNOWN');
|
|
475
|
+
assert.equal(
|
|
476
|
+
report.unmatchedExecutedClosures.identities[0].signalId,
|
|
477
|
+
'signal-1',
|
|
478
|
+
);
|
|
479
|
+
assert.equal(report.unmatchedExecutedClosures.identitiesComplete, true);
|
|
480
|
+
assert.deepEqual(
|
|
481
|
+
fs.readFileSync(
|
|
482
|
+
path.join(f.root, f.authority.streams.execution.files[0].path),
|
|
483
|
+
),
|
|
484
|
+
before,
|
|
485
|
+
);
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
test('one-sided native closure trace is never downgraded to unmatched-entry diagnostics', async (t) => {
|
|
489
|
+
for (const unmatchedClosure of ['entry-only', 'exit-only']) {
|
|
490
|
+
const f = executedFixture(t, { trades: [{}, { unmatchedClosure }] });
|
|
491
|
+
await assert.rejects(
|
|
492
|
+
derive(f),
|
|
493
|
+
/Completed\/execution\/entry\/exit-trace count mismatch|One-sided native closure trace/,
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
test('unmatched successful entry still cannot bypass a false own gate', async (t) => {
|
|
499
|
+
const f = executedFixture(t, { trades: [{}, { unmatchedClosure: true }] });
|
|
500
|
+
const file = path.join(f.root, f.authority.streams.gate.files[0].path);
|
|
501
|
+
const rows = fs.readFileSync(file, 'utf8').trim().split('\n').map(JSON.parse);
|
|
502
|
+
rows[1].allow = false;
|
|
503
|
+
f.authority.streams.gate = f.stream(
|
|
504
|
+
'false-gate-before-extra-execution',
|
|
505
|
+
rows,
|
|
506
|
+
);
|
|
507
|
+
f.descriptor = f.json('false-gate-extra-authority.json', f.authority);
|
|
508
|
+
await assert.rejects(derive(f), /Blocked identity appears downstream/);
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
test('real native empty checkpoint stats bind with mixed fills without input mutation', async (t) => {
|
|
512
|
+
const f = executedFixture(t, { trades: [{}], nativeEmptyStats: true });
|
|
513
|
+
const mapping = JSON.parse(
|
|
514
|
+
fs.readFileSync(path.join(f.root, f.authority.runBinding.path), 'utf8'),
|
|
515
|
+
);
|
|
516
|
+
const file = path.join(f.root, mapping.completedCheckpoint.path);
|
|
517
|
+
const before = fs.readFileSync(file);
|
|
518
|
+
const checkpoint = JSON.parse(before);
|
|
519
|
+
assert.equal(
|
|
520
|
+
checkpoint.filter((e) => Object.keys(e.result.stat).length === 3).length,
|
|
521
|
+
575,
|
|
522
|
+
);
|
|
523
|
+
assert.deepEqual(checkpoint[1].result.stat, {
|
|
524
|
+
amount: 100,
|
|
525
|
+
profit: 0,
|
|
526
|
+
orders: 0,
|
|
527
|
+
});
|
|
528
|
+
const { report } = await derive(f);
|
|
529
|
+
assert.equal(report.nativeReconciliation.trades, 1);
|
|
530
|
+
assert.deepEqual(fs.readFileSync(file), before);
|
|
531
|
+
assert.equal(
|
|
532
|
+
sha256(fs.readFileSync(file)),
|
|
533
|
+
mapping.completedCheckpoint.sha256,
|
|
534
|
+
);
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
test('all genuine native empty stats produce verified zero, not missing-record inference', async (t) => {
|
|
538
|
+
const f = executedFixture(t, {
|
|
539
|
+
zero: true,
|
|
540
|
+
allow: false,
|
|
541
|
+
nativeEmptyStats: true,
|
|
542
|
+
});
|
|
543
|
+
const { report } = await derive(f);
|
|
544
|
+
assert.equal(report.nativeReconciliation.trades, 0);
|
|
545
|
+
assert.equal(report.nativeReconciliation.pnl, 0);
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
test('unsupported or contradictory native zero checkpoint shapes fail closed', async (t) => {
|
|
549
|
+
const invalid = [
|
|
550
|
+
null,
|
|
551
|
+
{},
|
|
552
|
+
{ amount: 100, profit: 0 },
|
|
553
|
+
{ amount: 100, profit: 0, orders: -1 },
|
|
554
|
+
{ amount: 100, profit: 0, orders: 1 },
|
|
555
|
+
{ amount: 100, profit: 1, orders: 0 },
|
|
556
|
+
{ amount: 101, profit: 0, orders: 0 },
|
|
557
|
+
{ amount: 100, profit: 0, orders: 0, wins: 0 },
|
|
558
|
+
{ orders: 0, wins: 1, losses: 0, netProfit: 0 },
|
|
559
|
+
{ orders: 0, wins: 0, losses: 1, netProfit: 0 },
|
|
560
|
+
{ orders: 0, wins: 0, losses: 0, netProfit: 1 },
|
|
561
|
+
{ orders: 0, wins: 0, losses: 0, netProfit: 0, profit: 1 },
|
|
562
|
+
];
|
|
563
|
+
for (const [i, stat] of invalid.entries()) {
|
|
564
|
+
const f = executedFixture(t, { zero: true, id: `bad-native-empty-${i}` });
|
|
565
|
+
const mapping = JSON.parse(
|
|
566
|
+
fs.readFileSync(path.join(f.root, f.authority.runBinding.path), 'utf8'),
|
|
567
|
+
);
|
|
568
|
+
const checkpoint = JSON.parse(
|
|
569
|
+
fs.readFileSync(
|
|
570
|
+
path.join(f.root, mapping.completedCheckpoint.path),
|
|
571
|
+
'utf8',
|
|
572
|
+
),
|
|
573
|
+
);
|
|
574
|
+
checkpoint[0].result.stat = stat;
|
|
575
|
+
mapping.completedCheckpoint = f.json(
|
|
576
|
+
'invalid-empty-checkpoint.json',
|
|
577
|
+
checkpoint,
|
|
578
|
+
);
|
|
579
|
+
f.authority.runBinding = f.json('invalid-empty-mapping.json', mapping);
|
|
580
|
+
f.descriptor = f.json('invalid-empty-authority.json', f.authority);
|
|
581
|
+
await assert.rejects(
|
|
582
|
+
derive(f),
|
|
583
|
+
/Native checkpoint economic stats unavailable|Invalid native flat count/,
|
|
584
|
+
);
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
test('verified checkpoint runtime is unwrapped for source and notional authorities', async (t) => {
|
|
589
|
+
const f = executedFixture(t, { trades: [{}] });
|
|
590
|
+
const executionRuntime = {
|
|
591
|
+
mode: 'verified-project-runtime-checkpoint',
|
|
592
|
+
checkpoint: f.authority.runtimeCheckpoint,
|
|
593
|
+
packages: { '@tradejs/node': { packageRoot: f.root } },
|
|
594
|
+
};
|
|
595
|
+
f.validators.ensureRuntimeBuild = async () => ({ executionRuntime });
|
|
596
|
+
const seen = [];
|
|
597
|
+
f.validators.verifyStrategyArtifactAuthority = (options) => {
|
|
598
|
+
assert.strictEqual(options.executionRuntime, executionRuntime);
|
|
599
|
+
seen.push('source');
|
|
600
|
+
};
|
|
601
|
+
const verifyNotional = f.validators.verifyEntryNotionalAuthority;
|
|
602
|
+
f.validators.verifyEntryNotionalAuthority = async (options) => {
|
|
603
|
+
assert.strictEqual(options.executionRuntime, executionRuntime);
|
|
604
|
+
seen.push('notional');
|
|
605
|
+
return verifyNotional(options);
|
|
606
|
+
};
|
|
607
|
+
await derive(f);
|
|
608
|
+
assert.deepEqual(seen, ['source', 'notional']);
|
|
609
|
+
});
|
|
610
|
+
|
|
611
|
+
test('checkpoint resolution without nested runtime fails closed before authority checks', async (t) => {
|
|
612
|
+
const f = executedFixture(t, { trades: [{}] });
|
|
613
|
+
f.validators.ensureRuntimeBuild = async () => ({ packages: {} });
|
|
614
|
+
f.validators.verifyStrategyArtifactAuthority = () => {
|
|
615
|
+
assert.fail('Invalid runtime must not reach source verification');
|
|
616
|
+
};
|
|
617
|
+
await assert.rejects(
|
|
618
|
+
derive(f),
|
|
619
|
+
/Missing verified checkpoint execution runtime/,
|
|
620
|
+
);
|
|
621
|
+
});
|
|
399
622
|
const all = (r, id, basis = 'observed') =>
|
|
400
623
|
[
|
|
401
624
|
...r.matrices[basis].exitWindow,
|
|
@@ -683,6 +906,55 @@ test('missing bridge authority preserves diagnostics and UNKNOWN, not economic F
|
|
|
683
906
|
assert.ok(report.blockers.includes('missing-accepted-opt-in-bridge-parity'));
|
|
684
907
|
});
|
|
685
908
|
|
|
909
|
+
test('default stress uses existing actual-entry notional without optional fee authority', async (t) => {
|
|
910
|
+
const f = executedFixture(t, {
|
|
911
|
+
accepted: false,
|
|
912
|
+
trades: [
|
|
913
|
+
{
|
|
914
|
+
pnl: 1,
|
|
915
|
+
result: { entryPrice: 100, qty: 2, closedQty: 1, openFee: null },
|
|
916
|
+
},
|
|
917
|
+
],
|
|
918
|
+
});
|
|
919
|
+
delete f.authority.entryNotionalAuthority;
|
|
920
|
+
f.descriptor = f.json('authority-default-stress.json', f.authority);
|
|
921
|
+
const before = fs.readFileSync(
|
|
922
|
+
path.join(f.root, f.authority.completedStream.files[0].path),
|
|
923
|
+
);
|
|
924
|
+
const { report } = await derive(f);
|
|
925
|
+
assert.equal(all(report, 'FULL_1461D', 'stress20bps').pnl, 0.8);
|
|
926
|
+
assert.equal(
|
|
927
|
+
all(report, 'FULL_1461D', 'stress20bps').economicCompletenessStatus,
|
|
928
|
+
'COMPLETE',
|
|
929
|
+
);
|
|
930
|
+
assert.equal(report.authorityStatus, 'UNKNOWN');
|
|
931
|
+
assert.deepEqual(
|
|
932
|
+
fs.readFileSync(
|
|
933
|
+
path.join(f.root, f.authority.completedStream.files[0].path),
|
|
934
|
+
),
|
|
935
|
+
before,
|
|
936
|
+
);
|
|
937
|
+
});
|
|
938
|
+
|
|
939
|
+
test('default stress retains UNKNOWN for missing or invalid actual notional fields', async (t) => {
|
|
940
|
+
for (const result of [
|
|
941
|
+
{ entryPrice: null },
|
|
942
|
+
{ closedQty: 2, qty: 1 },
|
|
943
|
+
{ entryPrice: -100 },
|
|
944
|
+
{ entryPrice: 1e308, qty: 1e308, closedQty: 1e308 },
|
|
945
|
+
]) {
|
|
946
|
+
const f = executedFixture(t, { trades: [{ result }] });
|
|
947
|
+
delete f.authority.entryNotionalAuthority;
|
|
948
|
+
f.descriptor = f.json('authority-default-stress.json', f.authority);
|
|
949
|
+
const { report } = await derive(f);
|
|
950
|
+
assert.equal(all(report, 'FULL_1461D', 'stress20bps').pnl, null);
|
|
951
|
+
assert.equal(
|
|
952
|
+
all(report, 'FULL_1461D', 'stress20bps').economicCompletenessStatus,
|
|
953
|
+
'UNKNOWN',
|
|
954
|
+
);
|
|
955
|
+
}
|
|
956
|
+
});
|
|
957
|
+
|
|
686
958
|
test('missing actual fee only blocks affected stress cells, without displayed-price fallback', async (t) => {
|
|
687
959
|
const f = executedFixture(t, { trades: [{ result: { openFee: null } }] });
|
|
688
960
|
const { report } = await derive(f);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schema": "tradejs-skill-bundle/v1",
|
|
3
3
|
"source": "TradeJS-Dev/TradeJS:.codex/skills",
|
|
4
|
-
"bundleSha256": "
|
|
4
|
+
"bundleSha256": "5420dc3f8ed36fd7d8cf508704acd5919b508dd7972d291091b0470834e0bd2e",
|
|
5
5
|
"skills": [
|
|
6
6
|
"ai-train-local-research",
|
|
7
7
|
"backtest-config-redis",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
".codex/skills/ai-train-local-research/references/reporting.md": "ae0faaab3b3dd0f4bd27142fa2c9aa889c5e7572a10e9fe1ee79ff57ebbd8653",
|
|
26
26
|
".codex/skills/ai-train-local-research/scripts/ai-gate-ablation.mjs": "1ae57fcebf1cb01a0a0d3222881cc5f9ae36b3e008979b3a5f2bae75e02ed653",
|
|
27
27
|
".codex/skills/ai-train-local-research/scripts/ai-gate-ablation.test.mjs": "3d37e2ab217991aa083a60e77369a9f1c9afe8c5d0f99b3ea964f59be2c143f3",
|
|
28
|
-
".codex/skills/ai-train-local-research/scripts/decision-capture-join.mjs": "
|
|
29
|
-
".codex/skills/ai-train-local-research/scripts/decision-capture-join.test.mjs": "
|
|
28
|
+
".codex/skills/ai-train-local-research/scripts/decision-capture-join.mjs": "be10d5bec64bf102d16aa8097eb8e32701e46cbe4328a99093ab7fb256b4f815",
|
|
29
|
+
".codex/skills/ai-train-local-research/scripts/decision-capture-join.test.mjs": "017511e3d1530ab85f1c43677cddd146c7a68aab49cfce201cae0a187038e625",
|
|
30
30
|
".codex/skills/ai-train-local-research/scripts/decision-capture-storage.mjs": "a72b30351c79bd040ec6543c42a43ecc145fc056ea4108da4ac26d1641c0112c",
|
|
31
31
|
".codex/skills/ai-train-local-research/scripts/decision-capture-storage.test.mjs": "10af1fabaa880023215239e27b7d17cdf66e6e09a4a1329a226f2a8a2606d18e",
|
|
32
32
|
".codex/skills/ai-train-local-research/scripts/equity-stability.mjs": "3f60d079f881437f275d50ca78e063ef76451b166e031ec3e8f7a29b3b5e8a8a",
|
|
@@ -54,8 +54,8 @@
|
|
|
54
54
|
".codex/skills/strategy-improvement-research/references/final-composition-board.md": "9be39311ab1feb8abbbdd7ee7575ca4b91ff45be13cb7df1e241d7cb1435eacc",
|
|
55
55
|
".codex/skills/strategy-improvement-research/scripts/build-final-composition-spec.mjs": "f336bb3b7e0d0040cabeda9f2d0d015301b017ea6e825f3bcdcafeed187ee93b",
|
|
56
56
|
".codex/skills/strategy-improvement-research/scripts/build-final-composition-spec.test.mjs": "1985664ce89c00269c5e5b8748aceaaa7377759c84e28dfca7bb3618bfcfbe44",
|
|
57
|
-
".codex/skills/strategy-improvement-research/scripts/executed-composition-report.mjs": "
|
|
58
|
-
".codex/skills/strategy-improvement-research/scripts/executed-composition-report.test.mjs": "
|
|
57
|
+
".codex/skills/strategy-improvement-research/scripts/executed-composition-report.mjs": "0dcae8ceef53974e689ea9c96ef8ab0f44150f98f4b3adc44661207e827b5aa5",
|
|
58
|
+
".codex/skills/strategy-improvement-research/scripts/executed-composition-report.test.mjs": "feab6c1f93ac44166ed51924e69868a01aab52af54cc13a00fc8474692a496c6",
|
|
59
59
|
".codex/skills/strategy-improvement-research/scripts/final-composition-board.mjs": "1d48c4af33308dbb1518e3242b658f169a782d4d5c6ffd4b4ff3ba832d14f8cd",
|
|
60
60
|
".codex/skills/strategy-improvement-research/scripts/final-composition-board.test.mjs": "4dae48e797c27a55913639b3c90b66b7c8effe8eb9da3c943d804df301d58aa6",
|
|
61
61
|
".codex/skills/strategy-improvement-research/scripts/freeze-gate-variants.mjs": "17b3c868880ddd5b326d309e6e42eaeef1ea22d625a5db947997ac73b221ad6d",
|