Adds the respire-memory gateway middleware (packages/respire-memory, 0.3.6) by luodaoyi, with its registry.json entry and README row. It recalls from a loopback Respire Runtime on each new turn, reads the engine policy each turn and puts it in the history once (again only when it changes), keeps turn state in an encrypted store, and optionally archives turns in a detached worker through a user-configured Chat Completions service. Diary writes are confirmed by a bounded fragment readback. Run at 7aba754 on mote (Node 22.22.0, Bun 1.4.2): - packages/respire-memory: `node scripts/check.mjs` ok (4 protocol, policy dedup/change/revert, byte-identical prefixes, failed read, diary readback); `bun test` 1 pass. - root: `HOME=$(mktemp -d) bun test` 4187 pass, 9 skip, 0 fail (4196 tests, 328 files); `bun scripts/check.mjs` exit 0. - Review probe (fake loopback Runtime, 2000-byte policy, six chat turns with a host restart at turn 3, a policy change, then the Runtime down), Bun and Node: per-turn overhead grows ~524 bytes a turn (the read receipt), not the policy; no earlier prefix changed; one policy read per turn; two policy blocks after the change; with the Runtime down the latest turn is unchanged. With no Runtime at all the body passes through unchanged. - Capture probe: with capture on, a turn whose JSON exceeds the ~4000-char readback budget is written and then recorded unconfirmed, and archival skips it. The PR's check asserts this and the README documents it. - Harm check: no new dependencies, install scripts or workflow changes; network only to the loopback Runtime and the user's own generation URL. Not tried: a real Respire Runtime.
97 lines
4.2 KiB
JavaScript
97 lines
4.2 KiB
JavaScript
import { mkdir, readdir, writeFile, readFile, unlink, rmdir, lstat } from 'node:fs/promises';
|
|
import { join } from 'node:path';
|
|
import { randomBytes } from 'node:crypto';
|
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
|
|
export function alive(pid) {
|
|
if (!Number.isSafeInteger(pid) || pid <= 0) return true;
|
|
try { process.kill(pid, 0); return true; }
|
|
catch (error) { if (error.code === 'ESRCH') return false; if (error.code === 'EPERM') return true; throw error; }
|
|
}
|
|
|
|
async function removeEmpty(path) {
|
|
for (let attempt = 0; ; attempt++) {
|
|
try { await rmdir(path); return true; }
|
|
catch (error) {
|
|
if (['ENOENT', 'ENOTEMPTY', 'EEXIST'].includes(error.code)) return false;
|
|
if (process.platform === 'win32' && ['EPERM', 'EBUSY'].includes(error.code)) {
|
|
// Windows can briefly retain a directory handle; an empty lock remains recoverable.
|
|
if (attempt === 2) return false;
|
|
await delay(25); continue;
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function acquireLock(path, { waitMs = 5000 } = {}) {
|
|
const deadline = Date.now() + Math.max(waitMs, 100);
|
|
let first = true;
|
|
while (true) {
|
|
if (!first && Date.now() >= deadline) throw new Error('lock-busy');
|
|
first = false;
|
|
try {
|
|
await mkdir(path, { mode: 0o700 });
|
|
const owner = `${process.pid}-${randomBytes(12).toString('hex')}.owner`;
|
|
try {
|
|
await writeFile(join(path, owner), String(Date.now()), { flag: 'wx', mode: 0o600 });
|
|
const owners = await readdir(path);
|
|
// An empty directory may be recovered while its creator is publishing ownership.
|
|
if (owners.length === 1 && owners[0] === owner) return async () => {
|
|
await unlink(join(path, owner)); await removeEmpty(path);
|
|
};
|
|
await unlink(join(path, owner));
|
|
} catch (error) {
|
|
// Concurrent parent removal reports EPERM on Windows and EINVAL on macOS.
|
|
const racedOpen = (process.platform === 'win32' && error.code === 'EPERM')
|
|
|| (process.platform === 'darwin' && error.code === 'EINVAL');
|
|
if (racedOpen && error.syscall === 'open' && error.path === join(path, owner)) {
|
|
if (Date.now() >= deadline) throw error;
|
|
await delay(25); continue;
|
|
}
|
|
if (error.code !== 'ENOENT') throw error;
|
|
}
|
|
} catch (error) {
|
|
// Windows can keep a released directory delete-pending before the next mkdir.
|
|
if (process.platform === 'win32' && error.code === 'EPERM' && error.syscall === 'mkdir' && error.path === path) {
|
|
if (Date.now() >= deadline) throw error;
|
|
await delay(25); continue;
|
|
}
|
|
if (error.code !== 'EEXIST') throw error;
|
|
try {
|
|
if ((await lstat(path)).isDirectory()) {
|
|
for (const name of await readdir(path)) {
|
|
const match = /^([1-9]\d*)-[a-f0-9]+\.owner$/.exec(name);
|
|
if (match && !alive(Number(match[1]))) {
|
|
try { await unlink(join(path, name)); } catch (e) { if (e.code !== 'ENOENT') throw e; }
|
|
}
|
|
}
|
|
if (await removeEmpty(path)) continue;
|
|
} else {
|
|
// Older workers used a PID file. Never unlink an unknown or live owner.
|
|
const pid = Number(await readFile(path, 'utf8'));
|
|
if (!alive(pid)) {
|
|
try { await unlink(path); } catch (e) { if (!['ENOENT', 'EISDIR', 'EPERM'].includes(e.code)) throw e; }
|
|
continue;
|
|
}
|
|
}
|
|
} catch (e) {
|
|
if (['ENOENT', 'EISDIR', 'ENOTDIR'].includes(e.code)) continue;
|
|
// Recheck legacy ownership after Windows releases a busy PID-file handle.
|
|
if (process.platform === 'win32' && e.code === 'EBUSY' && e.syscall === 'unlink' && e.path === path) {
|
|
if (Date.now() >= deadline) throw e;
|
|
await delay(25); continue;
|
|
}
|
|
// A competing release can leave the lock directory delete-pending during a probe.
|
|
if (process.platform === 'win32' && e.code === 'EPERM' && e.syscall === 'scandir' && e.path === path) {
|
|
if (Date.now() >= deadline) throw e;
|
|
await delay(25); continue;
|
|
}
|
|
throw e;
|
|
}
|
|
}
|
|
if (waitMs === 0 || Date.now() >= deadline) throw new Error('lock-busy');
|
|
await delay(25);
|
|
}
|
|
}
|