-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpm-engine.crossval.js
More file actions
422 lines (397 loc) · 17 KB
/
cpm-engine.crossval.js
File metadata and controls
422 lines (397 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Cross-validation: JS computeCPM vs Python compute_cpm on identical fixtures.
// Run with: node cpm-engine.crossval.js
// The Python side is invoked via child_process; output is compared node-by-node.
'use strict';
const E = require('./cpm-engine.js');
const { execFileSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const os = require('os');
const PY = 'python';
// Python reference path resolution (in priority order):
// 1. $CPP_PYTHON_REFERENCE_DIR — explicit override
// 2. $CPP_PYTHON_REFERENCE_DIRS — colon/semicolon-separated list (for xer-parser + _cpp_common together)
// 3. ./python_reference — sibling dir for open-source consumers
// 4. ../../../_cpp_common/scripts — CPP-internal source-tree layout (developer-only)
//
// External contributors: set CPP_PYTHON_REFERENCE_DIR to the directory containing cpm.py
// (the canonical CPP Python reference engine). See CONTRIBUTING.md.
function _resolvePythonRefDirs() {
const dirs = [];
if (process.env.CPP_PYTHON_REFERENCE_DIRS) {
for (const d of process.env.CPP_PYTHON_REFERENCE_DIRS.split(/[;:]/)) {
if (d.trim()) dirs.push(path.resolve(d.trim()));
}
}
if (process.env.CPP_PYTHON_REFERENCE_DIR) {
dirs.push(path.resolve(process.env.CPP_PYTHON_REFERENCE_DIR));
}
// Sibling dir for open-source distribution
const sibling = path.join(__dirname, 'python_reference');
dirs.push(sibling);
// CPP-internal source-tree layout (developer-only)
const internalCpm = path.join(__dirname, '..', 'scripts');
dirs.push(internalCpm);
const internalXer = path.join(__dirname, '..', '..', 'xer-parser', 'scripts');
dirs.push(internalXer);
return Array.from(new Set(dirs));
}
const _PY_REF_DIRS = _resolvePythonRefDirs();
const _PY_SYS_PATH_INSERTS = _PY_REF_DIRS
.map(d => `sys.path.insert(0, r'${d.replace(/\\/g, '/')}')`)
.join('\n');
const PY_HARNESS = `
import sys, json
${_PY_SYS_PATH_INSERTS}
from cpm import compute_cpm, date_to_num
payload = json.loads(sys.stdin.read())
result = compute_cpm(
payload['activities'],
payload['relationships'],
data_date=payload.get('data_date', ''),
cal_map=payload.get('cal_map') or None,
)
result_json = {
'project_finish_num': result['project_finish_num'],
'project_finish': result['project_finish'],
'critical_codes': sorted(result['critical_codes']),
'topo_order': result['topo_order'],
'alert_count': len(result['alerts']),
'nodes': {
c: {
'es': n['es'], 'ef': n['ef'], 'ls': n['ls'], 'lf': n['lf'],
'tf': n['tf'],
'es_date': n['es_date'], 'ef_date': n['ef_date'],
'ls_date': n['ls_date'], 'lf_date': n['lf_date'],
}
for c, n in result['nodes'].items()
}
}
print(json.dumps(result_json))
`;
function runPython(payload) {
const tmp = path.join(os.tmpdir(), `cpm_xval_${process.pid}.py`);
fs.writeFileSync(tmp, PY_HARNESS);
try {
const out = execFileSync(PY, [tmp], {
input: JSON.stringify(payload),
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'pipe'],
});
return JSON.parse(out);
} finally {
try { fs.unlinkSync(tmp); } catch {}
}
}
function runJS(payload) {
const r = E.computeCPM(
payload.activities,
payload.relationships,
{ dataDate: payload.data_date || '', calMap: payload.cal_map || {} }
);
const nodes = {};
for (const code of Object.keys(r.nodes)) {
const n = r.nodes[code];
nodes[code] = {
es: n.es, ef: n.ef, ls: n.ls, lf: n.lf,
tf: n.tf,
es_date: n.es_date, ef_date: n.ef_date,
ls_date: n.ls_date, lf_date: n.lf_date,
};
}
return {
project_finish_num: r.projectFinishNum,
project_finish: r.projectFinish,
critical_codes: Array.from(r.criticalCodes).sort(),
topo_order: r.topoOrder,
alert_count: r.alerts.length,
nodes,
};
}
let fixturesPassed = 0;
let fixturesFailed = 0;
let totalChecks = 0;
let totalFails = 0;
function compareFixture(name, payload) {
console.log('\n--- ' + name + ' ---');
let py, js;
try { py = runPython(payload); }
catch (e) {
console.log(' PYTHON ERROR: ' + e.message);
fixturesFailed += 1;
return;
}
try { js = runJS(payload); }
catch (e) {
console.log(' JS ERROR: ' + e.message);
fixturesFailed += 1;
return;
}
let fails = 0;
function eq(label, a, b) {
totalChecks += 1;
if (JSON.stringify(a) === JSON.stringify(b)) {
console.log(' PASS ' + label);
} else {
fails += 1;
totalFails += 1;
console.log(' FAIL ' + label);
console.log(' py: ' + JSON.stringify(a));
console.log(' js: ' + JSON.stringify(b));
}
}
eq('project_finish_num', py.project_finish_num, js.project_finish_num);
eq('project_finish', py.project_finish, js.project_finish);
eq('critical_codes', py.critical_codes, js.critical_codes);
eq('topo_order', py.topo_order, js.topo_order);
eq('alert_count', py.alert_count, js.alert_count);
for (const code of Object.keys(py.nodes).sort()) {
const a = py.nodes[code], b = js.nodes[code];
if (!b) { fails += 1; totalFails += 1; console.log(' FAIL node ' + code + ' missing in JS'); continue; }
eq('node ' + code + '.es/ef/ls/lf/tf',
{ es: a.es, ef: a.ef, ls: a.ls, lf: a.lf, tf: a.tf },
{ es: b.es, ef: b.ef, ls: b.ls, lf: b.lf, tf: b.tf });
eq('node ' + code + '.dates',
{ es_date: a.es_date, ef_date: a.ef_date, ls_date: a.ls_date, lf_date: a.lf_date },
{ es_date: b.es_date, ef_date: b.ef_date, ls_date: b.ls_date, lf_date: b.lf_date });
}
if (fails === 0) fixturesPassed += 1; else fixturesFailed += 1;
}
// =====================================================================
// FIXTURE 1 — Linear chain, no calendar (ordinal fallback)
// =====================================================================
compareFixture('F1 — A→B→C linear, no cal', {
activities: [
{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: 7 },
{ code: 'C', duration_days: 3 },
],
relationships: [
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'B', to_code: 'C', type: 'FS', lag_days: 0 },
],
data_date: '2026-01-05',
cal_map: {},
});
// =====================================================================
// FIXTURE 2 — Linear chain WITH MonFri calendar
// =====================================================================
compareFixture('F2 — A→B→C linear, MonFri', {
activities: [
{ code: 'A', duration_days: 5, early_start: '2026-01-05', clndr_id: 'MF' },
{ code: 'B', duration_days: 7, clndr_id: 'MF' },
{ code: 'C', duration_days: 3, clndr_id: 'MF' },
],
relationships: [
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'B', to_code: 'C', type: 'FS', lag_days: 0 },
],
data_date: '2026-01-05',
cal_map: { MF: { work_days: [1,2,3,4,5], holidays: [] } },
});
// =====================================================================
// FIXTURE 3 — Off-CP branch + TF computation
// =====================================================================
compareFixture('F3 — A→B→C + A→X (off-CP), MonFri', {
activities: [
{ code: 'A', duration_days: 5, early_start: '2026-01-05', clndr_id: 'MF' },
{ code: 'B', duration_days: 7, clndr_id: 'MF' },
{ code: 'C', duration_days: 3, clndr_id: 'MF' },
{ code: 'X', duration_days: 2, clndr_id: 'MF' },
],
relationships: [
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'B', to_code: 'C', type: 'FS', lag_days: 0 },
{ from_code: 'A', to_code: 'X', type: 'FS', lag_days: 0 },
],
data_date: '2026-01-05',
cal_map: { MF: { work_days: [1,2,3,4,5], holidays: [] } },
});
// =====================================================================
// FIXTURE 4 — Mixed rel types + lags
// =====================================================================
compareFixture('F4 — Mixed FS/SS/FF/SF + lags', {
activities: [
{ code: 'A', duration_days: 10, early_start: '2026-01-05', clndr_id: 'MF' },
{ code: 'B', duration_days: 8, clndr_id: 'MF' },
{ code: 'C', duration_days: 6, clndr_id: 'MF' },
{ code: 'D', duration_days: 5, clndr_id: 'MF' },
{ code: 'E', duration_days: 4, clndr_id: 'MF' },
],
relationships: [
{ from_code: 'A', to_code: 'B', type: 'SS', lag_days: 2 }, // SS+2
{ from_code: 'A', to_code: 'C', type: 'FS', lag_days: 1 }, // FS+1
{ from_code: 'B', to_code: 'D', type: 'FF', lag_days: 0 }, // FF
{ from_code: 'C', to_code: 'D', type: 'FS', lag_days: 0 },
{ from_code: 'D', to_code: 'E', type: 'FS', lag_days: 0 },
],
data_date: '2026-01-05',
cal_map: { MF: { work_days: [1,2,3,4,5], holidays: [] } },
});
// =====================================================================
// FIXTURE 5 — Calendar with holidays
// =====================================================================
compareFixture('F5 — MonFri + holidays', {
activities: [
{ code: 'A', duration_days: 5, early_start: '2026-01-05', clndr_id: 'MFH' },
{ code: 'B', duration_days: 10, clndr_id: 'MFH' },
],
relationships: [
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
],
data_date: '2026-01-05',
cal_map: {
MFH: {
work_days: [1,2,3,4,5],
// Skip MLK day Mon 01-19, Family Day Mon 02-16
holidays: ['2026-01-19', '2026-02-16'],
},
},
});
// =====================================================================
// FIXTURE 6 — 7-day calendar (24/7 work)
// =====================================================================
compareFixture('F6 — 7-day calendar (no weekends)', {
activities: [
{ code: 'A', duration_days: 5, early_start: '2026-01-05', clndr_id: '247' },
{ code: 'B', duration_days: 3, clndr_id: '247' },
],
relationships: [
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
],
data_date: '2026-01-05',
cal_map: { '247': { work_days: [0,1,2,3,4,5,6], holidays: [] } },
});
// =====================================================================
// FIXTURE 6.5 — SF relationship (the v14 fix; missing from F1-F4)
// =====================================================================
compareFixture('F6.5 — SF + FS mix', {
activities: [
{ code: 'A', duration_days: 5, early_start: '2026-01-05', clndr_id: 'MF' },
{ code: 'B', duration_days: 3, clndr_id: 'MF' },
{ code: 'C', duration_days: 4, clndr_id: 'MF' },
],
relationships: [
{ from_code: 'A', to_code: 'B', type: 'SF', lag_days: 2 }, // B finishes >= A.ES + 2
{ from_code: 'B', to_code: 'C', type: 'FS', lag_days: 0 },
],
data_date: '2026-01-05',
cal_map: { MF: { work_days: [1,2,3,4,5], holidays: [] } },
});
// =====================================================================
// FIXTURE 7 — Multiple parallel paths converging
// =====================================================================
compareFixture('F7 — Diamond network', {
activities: [
{ code: 'START', duration_days: 1, early_start: '2026-01-05', clndr_id: 'MF' },
{ code: 'P1', duration_days: 5, clndr_id: 'MF' },
{ code: 'P2', duration_days: 8, clndr_id: 'MF' },
{ code: 'P3', duration_days: 3, clndr_id: 'MF' },
{ code: 'END', duration_days: 1, clndr_id: 'MF' },
],
relationships: [
{ from_code: 'START', to_code: 'P1', type: 'FS', lag_days: 0 },
{ from_code: 'START', to_code: 'P2', type: 'FS', lag_days: 0 },
{ from_code: 'START', to_code: 'P3', type: 'FS', lag_days: 0 },
{ from_code: 'P1', to_code: 'END', type: 'FS', lag_days: 0 },
{ from_code: 'P2', to_code: 'END', type: 'FS', lag_days: 0 },
{ from_code: 'P3', to_code: 'END', type: 'FS', lag_days: 0 },
],
data_date: '2026-01-05',
cal_map: { MF: { work_days: [1,2,3,4,5], holidays: [] } },
});
// =====================================================================
// FIXTURE 8 — Numeric-only activity codes (regression for JS Object-key
// integer-hoisting bug discovered 2026-05-09 during real-XER stress test)
// =====================================================================
compareFixture('F8 — Numeric codes interleaved with alpha codes', {
activities: [
{ code: 'A1', duration_days: 5, early_start: '2026-01-05', clndr_id: 'MF' },
{ code: '100', duration_days: 3, clndr_id: 'MF' },
{ code: 'A2', duration_days: 4, clndr_id: 'MF' },
{ code: '5', duration_days: 2, clndr_id: 'MF' },
{ code: 'A3', duration_days: 6, clndr_id: 'MF' },
{ code: '2170', duration_days: 7, clndr_id: 'MF' },
],
relationships: [
{ from_code: 'A1', to_code: '100', type: 'FS', lag_days: 0 },
{ from_code: '100', to_code: 'A2', type: 'FS', lag_days: 0 },
{ from_code: 'A1', to_code: '5', type: 'FS', lag_days: 0 },
{ from_code: 'A2', to_code: 'A3', type: 'FS', lag_days: 0 },
{ from_code: 'A3', to_code: '2170', type: 'FS', lag_days: 0 },
],
data_date: '2026-01-05',
cal_map: { MF: { work_days: [1,2,3,4,5], holidays: [] } },
});
// =====================================================================
// FIXTURE 9 — Negative lag (FS-3 = 3-day lead)
// =====================================================================
compareFixture('F9 — FS-3 lead', {
activities: [
{ code: 'A', duration_days: 10, early_start: '2026-01-05', clndr_id: 'MF' },
{ code: 'B', duration_days: 5, clndr_id: 'MF' },
],
relationships: [
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: -3 },
],
data_date: '2026-01-05',
cal_map: { MF: { work_days: [1,2,3,4,5], holidays: [] } },
});
// =====================================================================
// FIXTURE 10 — is_complete + open successor (locked to actuals)
// =====================================================================
compareFixture('F10 — Completed + uncompleted mix', {
activities: [
{ code: 'A', duration_days: 5, actual_start: '2026-01-05',
actual_finish: '2026-01-12', is_complete: true, clndr_id: 'MF' },
{ code: 'B', duration_days: 7, clndr_id: 'MF' },
{ code: 'C', duration_days: 3, clndr_id: 'MF' },
],
relationships: [
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'B', to_code: 'C', type: 'FS', lag_days: 0 },
],
data_date: '2026-01-12',
cal_map: { MF: { work_days: [1,2,3,4,5], holidays: [] } },
});
// =====================================================================
// FIXTURE 11 — Multiple calendars in same network
// =====================================================================
compareFixture('F11 — MonFri + 7-day calendars mixed', {
activities: [
{ code: 'A', duration_days: 5, early_start: '2026-01-05', clndr_id: 'MF' },
{ code: 'B', duration_days: 5, clndr_id: '247' }, // 7-day → finishes earlier
{ code: 'C', duration_days: 3, clndr_id: 'MF' },
],
relationships: [
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'B', to_code: 'C', type: 'FS', lag_days: 0 },
],
data_date: '2026-01-05',
cal_map: {
MF: { work_days: [1,2,3,4,5], holidays: [] },
'247': { work_days: [0,1,2,3,4,5,6], holidays: [] },
},
});
// =====================================================================
// FIXTURE 12 — early_start pin ahead of logic (constraint-style)
// =====================================================================
compareFixture('F12 — early_start pin ahead of logic', {
activities: [
{ code: 'A', duration_days: 5, early_start: '2026-01-05', clndr_id: 'MF' },
{ code: 'B', duration_days: 5, clndr_id: 'MF' },
// C is pinned LATER than logic would put it. The pin wins in forward pass.
{ code: 'C', duration_days: 3, early_start: '2026-02-02', clndr_id: 'MF' },
],
relationships: [
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'B', to_code: 'C', type: 'FS', lag_days: 0 },
],
data_date: '2026-01-05',
cal_map: { MF: { work_days: [1,2,3,4,5], holidays: [] } },
});
console.log('\n=========================================');
console.log(' Fixtures: ' + fixturesPassed + ' passed, ' + fixturesFailed + ' failed');
console.log(' Checks: ' + (totalChecks - totalFails) + ' / ' + totalChecks);
console.log('=========================================\n');
process.exit(fixturesFailed > 0 ? 1 : 0);