-
-
Notifications
You must be signed in to change notification settings - Fork 11k
Expand file tree
/
Copy pathverify-pipeline.mjs
More file actions
262 lines (238 loc) · 9.59 KB
/
Copy pathverify-pipeline.mjs
File metadata and controls
262 lines (238 loc) · 9.59 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
#!/usr/bin/env node
/**
* verify-pipeline.mjs — Health check for career-ops pipeline integrity
*
* Checks:
* 1. All statuses are canonical (per states.yml)
* 2. No duplicate company+role entries
* 3. All report links point to existing files
* 4. Scores match format X.XX/5 or N/A or DUP
* 5. All rows have proper pipe-delimited format
* 6. No pending TSVs in tracker-additions/ (only in merged/ or archived/)
* 7. states.yml canonical IDs for cross-system consistency
*
* Run: node career-ops/verify-pipeline.mjs
*/
import { readFileSync, readdirSync, existsSync, mkdirSync, unlinkSync, statSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const CAREER_OPS = dirname(fileURLToPath(import.meta.url));
// Support both layouts: data/applications.md (boilerplate) and applications.md (original).
// CAREER_OPS_TRACKER overrides the path (used by tests and non-standard layouts).
const APPS_FILE = process.env.CAREER_OPS_TRACKER
? process.env.CAREER_OPS_TRACKER
: existsSync(join(CAREER_OPS, 'data/applications.md'))
? join(CAREER_OPS, 'data/applications.md')
: join(CAREER_OPS, 'applications.md');
const ADDITIONS_DIR = join(CAREER_OPS, 'batch/tracker-additions');
const REPORTS_DIR = join(CAREER_OPS, 'reports');
const STATES_FILE = existsSync(join(CAREER_OPS, 'templates/states.yml'))
? join(CAREER_OPS, 'templates/states.yml')
: join(CAREER_OPS, 'states.yml');
// Ensure required directories exist (fresh setup)
mkdirSync(join(CAREER_OPS, 'data'), { recursive: true });
mkdirSync(REPORTS_DIR, { recursive: true });
const CANONICAL_STATUSES = [
'evaluated', 'applied', 'responded', 'interview',
'offer', 'rejected', 'discarded', 'skip',
];
const ALIASES = {
'evaluada': 'evaluated', 'condicional': 'evaluated', 'hold': 'evaluated', 'evaluar': 'evaluated', 'verificar': 'evaluated',
'aplicado': 'applied', 'enviada': 'applied', 'aplicada': 'applied', 'applied': 'applied', 'sent': 'applied',
'respondido': 'responded',
'entrevista': 'interview',
'oferta': 'offer',
'rechazado': 'rejected', 'rechazada': 'rejected',
'descartado': 'discarded', 'descartada': 'discarded', 'cerrada': 'discarded', 'cancelada': 'discarded',
'no aplicar': 'skip', 'no_aplicar': 'skip', 'monitor': 'skip', 'geo blocker': 'skip',
};
let errors = 0;
let warnings = 0;
function error(msg) { console.log(`❌ ${msg}`); errors++; }
function warn(msg) { console.log(`⚠️ ${msg}`); warnings++; }
function ok(msg) { console.log(`✅ ${msg}`); }
// --- Read applications.md ---
if (!existsSync(APPS_FILE)) {
console.log('\n📊 No applications.md found. This is normal for a fresh setup.');
console.log(' The file will be created when you evaluate your first offer.\n');
process.exit(0);
}
const content = readFileSync(APPS_FILE, 'utf-8');
const lines = content.split('\n');
// Map columns by header name so the checks work whether the tracker uses the
// original 9-column layout or a customized one with an extra column (e.g. a
// Location column after Role). Fixed-position indexing would otherwise read
// Location where Score is expected and flag false errors. Falls back to the
// legacy fixed layout when no recognizable header row is found.
const LEGACY_COLMAP = { num: 1, date: 2, company: 3, role: 4, score: 5, status: 6, pdf: 7, report: 8, notes: 9 };
const HEADER_ALIASES = {
'#': 'num', 'num': 'num', 'date': 'date', 'company': 'company', 'empresa': 'company',
'role': 'role', 'puesto': 'role', 'location': 'location', 'score': 'score',
'status': 'status', 'pdf': 'pdf', 'report': 'report', 'notes': 'notes',
};
function detectColumns(allLines) {
for (const line of allLines) {
if (!line.startsWith('|')) continue;
const cells = line.split('|').map(s => s.trim().toLowerCase());
if (!cells.includes('company') || !cells.includes('role')) continue;
const map = {};
cells.forEach((c, i) => { if (HEADER_ALIASES[c] != null) map[HEADER_ALIASES[c]] = i; });
if (['num', 'company', 'role', 'score', 'status'].every(k => map[k] != null)) return map;
}
return null;
}
const COLMAP = detectColumns(lines) || LEGACY_COLMAP;
const MAX_IDX = Math.max(...Object.values(COLMAP));
const entries = [];
for (const line of lines) {
if (!line.startsWith('|')) continue;
const parts = line.split('|').map(s => s.trim());
if (parts.length <= MAX_IDX) continue;
const num = parseInt(parts[COLMAP.num]);
if (isNaN(num)) continue;
entries.push({
num,
date: parts[COLMAP.date],
company: parts[COLMAP.company],
role: parts[COLMAP.role],
location: COLMAP.location != null ? parts[COLMAP.location] : '',
score: parts[COLMAP.score],
status: parts[COLMAP.status],
pdf: parts[COLMAP.pdf],
report: parts[COLMAP.report],
notes: COLMAP.notes != null ? (parts[COLMAP.notes] || '') : '',
});
}
console.log(`\n📊 Checking ${entries.length} entries in applications.md\n`);
// --- Check 1: Canonical statuses ---
let badStatuses = 0;
for (const e of entries) {
const clean = e.status.replace(/\*\*/g, '').trim().toLowerCase();
// Strip trailing dates
const statusOnly = clean.replace(/\s+\d{4}-\d{2}-\d{2}.*$/, '').trim();
if (!CANONICAL_STATUSES.includes(statusOnly) && !ALIASES[statusOnly]) {
error(`#${e.num}: Non-canonical status "${e.status}"`);
badStatuses++;
}
// Check for markdown bold in status
if (e.status.includes('**')) {
error(`#${e.num}: Status contains markdown bold: "${e.status}"`);
badStatuses++;
}
// Check for dates in status
if (/\d{4}-\d{2}-\d{2}/.test(e.status)) {
error(`#${e.num}: Status contains date: "${e.status}" — dates go in date column`);
badStatuses++;
}
}
if (badStatuses === 0) ok('All statuses are canonical');
// --- Check 2: Duplicates ---
const companyRoleMap = new Map();
let dupes = 0;
for (const e of entries) {
const key = e.company.toLowerCase().replace(/[^a-z0-9]/g, '') + '::' +
e.role.toLowerCase().replace(/[^a-z0-9 ]/g, '');
if (!companyRoleMap.has(key)) companyRoleMap.set(key, []);
companyRoleMap.get(key).push(e);
}
for (const [key, group] of companyRoleMap) {
if (group.length > 1) {
warn(`Possible duplicates: ${group.map(e => `#${e.num}`).join(', ')} (${group[0].company} — ${group[0].role})`);
dupes++;
}
}
if (dupes === 0) ok('No exact duplicates found');
// --- Check 3: Report links ---
// Markdown links resolve relative to the file that contains them, so report
// links must resolve against the tracker's own directory (see #760). For the
// transition we also accept legacy root-relative links: try the tracker dir
// first, then fall back to the repo root before flagging a link broken.
const TRACKER_DIR = dirname(APPS_FILE);
let brokenReports = 0;
for (const e of entries) {
const match = e.report.match(/\]\(([^)]+)\)/);
if (!match) continue;
const link = match[1];
if (!existsSync(join(TRACKER_DIR, link)) && !existsSync(join(CAREER_OPS, link))) {
error(`#${e.num}: Report not found: ${link}`);
brokenReports++;
}
}
if (brokenReports === 0) ok('All report links valid');
// --- Check 4: Score format ---
let badScores = 0;
for (const e of entries) {
const s = e.score.replace(/\*\*/g, '').trim();
if (!/^\d+\.?\d*\/5$/.test(s) && s !== 'N/A' && s !== 'DUP') {
error(`#${e.num}: Invalid score format: "${e.score}"`);
badScores++;
}
}
if (badScores === 0) ok('All scores valid');
// --- Check 5: Row format ---
let badRows = 0;
for (const line of lines) {
if (!line.startsWith('|')) continue;
if (line.includes('---') || line.includes('Empresa')) continue;
const parts = line.split('|');
if (parts.length <= MAX_IDX) {
error(`Row with too few columns (need ${MAX_IDX} data cols): ${line.substring(0, 80)}...`);
badRows++;
}
}
if (badRows === 0) ok('All rows properly formatted');
// --- Check 6: Pending TSVs ---
let pendingTsvs = 0;
if (existsSync(ADDITIONS_DIR)) {
const files = readdirSync(ADDITIONS_DIR).filter(f => f.endsWith('.tsv'));
pendingTsvs = files.length;
if (pendingTsvs > 0) {
warn(`${pendingTsvs} pending TSVs in tracker-additions/ (not merged)`);
}
}
if (pendingTsvs === 0) ok('No pending TSVs');
// --- Check 7: Bold in scores ---
let boldScores = 0;
for (const e of entries) {
if (e.score.includes('**')) {
warn(`#${e.num}: Score has markdown bold: "${e.score}"`);
boldScores++;
}
}
if (boldScores === 0) ok('No bold in scores');
// --- Check 8: Stale report-number sentinels (GC) ---
// reserve-report-num.mjs drops NNN-RESERVED.md files in reports/ when a
// number is claimed. If the process crashed before writing the real report
// and deleting the sentinel it will linger. Sentinels older than 4 h are
// stale; remove them here so they don't skew the next slot allocation.
const SENTINEL_MAX_AGE_MS = 4 * 60 * 60 * 1000;
let staleSentinels = 0;
if (existsSync(REPORTS_DIR)) {
const now = Date.now();
for (const name of readdirSync(REPORTS_DIR)) {
if (!name.endsWith('-RESERVED.md')) continue;
const full = join(REPORTS_DIR, name);
try {
const { mtimeMs } = statSync(full);
if (now - mtimeMs > SENTINEL_MAX_AGE_MS) {
unlinkSync(full);
warn(`Removed stale reservation sentinel: ${name}`);
staleSentinels++;
}
} catch {
// Already gone between readdir and stat — fine.
}
}
}
if (staleSentinels === 0) ok('No stale reservation sentinels');
// --- Summary ---
console.log('\n' + '='.repeat(50));
console.log(`📊 Pipeline Health: ${errors} errors, ${warnings} warnings`);
if (errors === 0 && warnings === 0) {
console.log('🟢 Pipeline is clean!');
} else if (errors === 0) {
console.log('🟡 Pipeline OK with warnings');
} else {
console.log('🔴 Pipeline has errors — fix before proceeding');
}
process.exit(errors > 0 ? 1 : 0);