main
John Lauer
Use native Codex editor titles and verified persistent rename
74a1165
'use strict';
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const {execFile} = require('node:child_process');
// More than one Codex backend may use the same home (including a user-selected
// native engine). Native notifications do not cross those processes. Watch only
// store changes, then read names for OPEN panels, never transcripts or model output.
const listeners = new Set();
let watcher, timer, busy = false, again = false, lastRead = 0;
function schedule() {
if (!listeners.size || timer) return;
if (busy) {again = true; return;}
timer = setTimeout(read, Math.max(100, 1000 - (Date.now() - lastRead)));
timer.unref?.();
}
function read() {
timer = undefined;
const ids = [...new Set([...listeners].map(item => item.id()).filter(Boolean))];
if (!ids.length) return;
busy = true; lastRead = Date.now();
execFile('python3', [path.join(__dirname, 'thread_names.py'), JSON.stringify(ids)],
{timeout: 4000, maxBuffer: 256 * 1024}, (error, stdout) => {
busy = false;
if (!error) {
try {const names = JSON.parse(stdout); for (const item of listeners) item.changed(names[item.id()]);}
catch (_) { /* a locked/changed store must never break the editor */ }
}
if (again) {again = false; schedule();}
});
}
exports.subscribe = function (id, changed) {
const item = {id, changed}; listeners.add(item);
if (!watcher) {
try {
watcher = fs.watch(process.env.CODEX_HOME || path.join(os.homedir(), '.codex'), (_event, file) => {
if (/^state_\d+\.sqlite(?:-wal)?$/.test(String(file))) schedule();
});
watcher.unref?.(); watcher.on('error', () => {});
} catch (_) { /* native notifications remain available */ }
}
schedule();
return {refresh: schedule, dispose() {
listeners.delete(item);
if (!listeners.size) {watcher?.close(); watcher = undefined; clearTimeout(timer); timer = undefined; again = false;}
}};
};