main
John Lauer
Preserve existing Codex nonce panel identity before activation
4b85a6a
'use strict';
const fs = require('node:fs'), path = require('node:path'), os = require('node:os');
const {createHash, randomUUID} = require('node:crypto');
const UUID = /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i;
function normalized(route) {
const match = /^\/extension\/panel\/new\?(.+)$/.exec(route);
if (!match) return null;
try {const query = decodeURIComponent(match[1]); return /^(?:adom|nonce)=[A-Za-z0-9_.:-]{1,128}$/.test(query) ? '/extension/panel/new?' + query : null;}
catch (_) {return null;}
}
function location(route) {
const canonical = normalized(route); if (!canonical) return null;
const home = fs.realpathSync(process.env.CODEX_HOME || path.join(os.homedir(), '.codex'));
const digest = value => createHash('sha256').update(value).digest('hex');
const base = process.env.ADOM_CODEX_BINDINGS_DIR || path.join(os.homedir(), '.local/state/adom/codex-integration/panel-bindings');
return {canonical, file: path.join(base, digest(home), digest(canonical) + '.json')};
}
exports.read = route => {
try {const target = location(route); if (!target) return; const record = JSON.parse(fs.readFileSync(target.file, 'utf8')); if (record.route === target.canonical && UUID.test(record.threadId || '')) return record.threadId;}
catch (_) { /* absent bindings retain the normal new-panel behavior */ }
};
exports.save = (route, threadId) => {
let temporary;
try {
if (!UUID.test(threadId || '')) return;
const target = location(route); if (!target) return;
fs.mkdirSync(path.dirname(target.file), {recursive: true, mode: 0o700});
temporary = target.file + '.' + randomUUID() + '.tmp';
fs.writeFileSync(temporary, JSON.stringify({route: target.canonical, threadId}), {mode: 0o600});
fs.renameSync(temporary, target.file);
} catch (_) {if (temporary) try {fs.unlinkSync(temporary);} catch (_) {} }
};