Files

34 lines
1.4 KiB
JavaScript

import { writeFileSync, openSync, closeSync, linkSync, unlinkSync } from 'node:fs';
import { join } from 'node:path';
// The host needs a model-activity signal, never event/output text or a credential.
// The historical filename/token stays stable for the host watchdog protocol.
export function firstOutputMarker(runDir) {
let seen = false;
return chunk => {
if (seen || !chunk.length) return;
try { writeFileSync(join(runDir, 'proxy-first-output'), 'output\n', { flag: 'wx', mode: 0o600 }); }
catch (error) { if (error.code !== 'EEXIST') throw error; }
seen = true;
};
}
// Publish only a fixed time record inside the existing writable run directory.
export function firstTurnMarker(runDir) {
return () => {
const stamp = process.hrtime.bigint();
const temporary = join(runDir, `.proxy-first-turn-${process.pid}-${stamp}`);
let fd, owned = false, failed = false;
try {
fd = openSync(temporary, 'wx', 0o600); owned = true;
writeFileSync(fd, `turn ${stamp}\n`);
try { linkSync(temporary, join(runDir, 'proxy-first-turn')); }
catch (error) { if (error.code !== 'EEXIST') throw error; }
} catch { failed = true; }
finally {
if (fd !== undefined) { try { closeSync(fd); } catch { failed = true; } }
if (owned) { try { unlinkSync(temporary); } catch { failed = true; } }
}
if (failed) throw new Error('first-turn marker publication failed');
};
}