Skip to content

Commit 7522f0e

Browse files
nodeeeeeeclaude
andcommitted
Fix smart match returning empty: resolve() called outside Promise scope
The pre-check used resolve() before the Promise was created, so it was undefined — the handler returned undefined and the script never ran. Restructured so pre-check failures return directly from the async handler, and only the script execution uses Promise/resolve. All error paths now include full diagnostics: Python path, script path, course dir existence, package import check results. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6a7c30a commit 7522f0e

1 file changed

Lines changed: 22 additions & 28 deletions

File tree

electron/main.js

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,15 +1000,15 @@ function registerIpc() {
10001000
const courseDir = path.join(outDir, String(cid));
10011001

10021002
// Pre-flight diagnostics
1003-
const diag = [];
1004-
diag.push(`Python: ${python} (exists: ${fs.existsSync(python)})`);
1005-
diag.push(`Script: ${script} (exists: ${fs.existsSync(script)})`);
1006-
diag.push(`DATA_DIR: ${DATA_DIR}`);
1007-
diag.push(`OutputDir: ${outDir}`);
1008-
diag.push(`CourseDir: ${courseDir} (exists: ${fs.existsSync(courseDir)})`);
1009-
diag.push(`Captions: ${fs.existsSync(path.join(courseDir, 'captions'))}`);
1010-
diag.push(`Materials: ${fs.existsSync(path.join(courseDir, 'materials'))}`);
1011-
console.log(`[align:suggestMatches] ${diag.join(' | ')}`);
1003+
const diagLines = [
1004+
`Python: ${python} (exists: ${fs.existsSync(python)})`,
1005+
`Script: ${script} (exists: ${fs.existsSync(script)})`,
1006+
`DATA_DIR: ${DATA_DIR}`,
1007+
`OutputDir: ${outDir}`,
1008+
`CourseDir: ${courseDir} (exists: ${fs.existsSync(courseDir)})`,
1009+
`Captions dir: ${fs.existsSync(path.join(courseDir, 'captions'))}`,
1010+
`Materials dir: ${fs.existsSync(path.join(courseDir, 'materials'))}`,
1011+
];
10121012

10131013
// Quick pre-check: can this Python import the needed packages?
10141014
const checkCode = "import sys; " +
@@ -1022,19 +1022,22 @@ function registerIpc() {
10221022
env: { ...process.env, AUTONOTE_DATA_DIR: DATA_DIR },
10231023
});
10241024
const chkOut = (chk.stdout || '') + (chk.stderr || '');
1025-
diag.push(`PreCheck: ${chkOut.replace(/\n/g, ' | ').trim()}`);
1026-
console.log(`[align:suggestMatches] precheck: ${chkOut.trim()}`);
1025+
diagLines.push(`PreCheck: ${chkOut.replace(/\n/g, ' | ').trim()}`);
1026+
const diagStr = diagLines.join('\n');
1027+
console.log(`[align:suggestMatches]\n${diagStr}`);
10271028

1028-
if (chkOut.includes('MISSING:') && !chkOut.includes('MISSING:OK') && !chkOut.includes('MISSING:\n')) {
1029+
// If packages are missing, return immediately with clear error
1030+
if (chkOut.includes('MISSING:') && !chkOut.includes('OK')) {
10291031
const missing = chkOut.match(/MISSING:(.+)/)?.[1]?.trim();
10301032
if (missing) {
1031-
resolve({ __error: `Python (${python}) is missing packages: ${missing}. Reinstall ML environment with these components enabled.` });
1032-
return;
1033+
return { __error: `Python (${python}) is missing packages: ${missing}.\nReinstall ML environment with these components enabled.\n\n${diagStr}` };
10331034
}
10341035
}
10351036

1037+
// Run the actual matching script
10361038
const cmd = [python, script, '--course', String(cid),
10371039
'--suggest-matches', '--match-model', model || 'bge-m3'];
1040+
10381041
return new Promise((resolve) => {
10391042
let proc;
10401043
try {
@@ -1043,43 +1046,34 @@ function registerIpc() {
10431046
stdio: ['ignore', 'pipe', 'pipe'],
10441047
});
10451048
} catch (e) {
1046-
resolve({ __error: `Failed to spawn: ${e.message}` });
1049+
resolve({ __error: `Failed to spawn process: ${e.message}\n\n${diagStr}` });
10471050
return;
10481051
}
10491052
let stdout = '';
10501053
let stderr = '';
10511054
proc.stdout.on('data', d => { stdout += d.toString(); });
10521055
proc.stderr.on('data', d => { stderr += d.toString(); });
10531056
proc.on('error', (e) => {
1054-
resolve({ __error: `Process error: ${e.message}` });
1057+
resolve({ __error: `Process error: ${e.message}\n\n${diagStr}` });
10551058
});
10561059
proc.on('close', (code) => {
1057-
// Combine stdout+stderr for diagnostics (Python prints to both)
10581060
const combined = stdout + '\n' + stderr;
10591061
if (code !== 0) {
1060-
// Look for common errors
1061-
let hint = '';
1062-
if (combined.includes('No module named')) {
1063-
const m = combined.match(/No module named '([^']+)'/);
1064-
hint = m ? `Missing package: ${m[1]}. Reinstall ML environment.` : '';
1065-
}
1066-
console.error(`[align:suggestMatches] exit ${code}\n${combined.slice(-500)}`);
1067-
resolve({ __error: hint || `Process exited with code ${code}. ${combined.slice(-200)}` });
1062+
resolve({ __error: `Exit code ${code}:\n${combined.trim()}\n\n── Diagnostics ──\n${diagStr}` });
10681063
return;
10691064
}
10701065
const marker = '__MATCH_RESULT__';
10711066
const idx = combined.indexOf(marker);
10721067
if (idx < 0) {
1073-
resolve({ __error: combined.trim() || 'Script produced no output.' });
1068+
resolve({ __error: `No results marker in output:\n${combined.trim()}\n\n── Diagnostics ──\n${diagStr}` });
10741069
return;
10751070
}
10761071
try {
10771072
const result = JSON.parse(combined.slice(idx + marker.length).trim());
1078-
// Attach the log so the UI can show it even on empty results
10791073
result.__log = combined.slice(0, idx).trim();
10801074
resolve(result);
10811075
} catch (e) {
1082-
resolve({ __error: `JSON parse error: ${e.message}\n\n${combined.trim()}` });
1076+
resolve({ __error: `JSON parse error: ${e.message}\n${combined.trim()}` });
10831077
}
10841078
});
10851079
});

0 commit comments

Comments
 (0)