import * as path from 'path';
import { readMicroverseState } from '../services/microverse-state.js';
if (process.argv[1] && path.basename(process.argv[1]) === 'read-microverse.js') {
    const [sessionRoot, field] = process.argv.slice(2);
    if (!sessionRoot || !field) {
        process.stderr.write('Usage: read-microverse <session-root> <field>\n');
        process.exit(1);
    }
    try {
        // readMicroverseState recovers a dead writer's microverse.json.tmp.<pid>
        // snapshot when the base file is mid-rename. An fs.existsSync(microverse.json)
        // pre-gate would short-circuit to '0' in exactly that window and defeat the
        // tmp recovery this reader was switched to (c0e91aed). It returns null for the
        // genuinely-absent case, so the gate is both redundant and harmful.
        const raw = readMicroverseState(sessionRoot);
        if (!raw) {
            process.stdout.write('0\n');
            process.exit(0);
        }
        const val = raw[field] ?? 0;
        process.stdout.write(String(val) + '\n');
    }
    catch {
        process.stdout.write('0\n');
    }
}
