Files
plugins/packages/nowledge-mem-agent/lib/files.mjs
T
Alexhang1314520 090b46ba4f agent-nowledge-mem 0.1.0: pick Nowledge Mem's Default, AI Now and Background models in magpie (#821)
Adds packages/nowledge-mem-agent (@magpie-community/agent-nowledge-mem
0.1.0), a kind: agent registry entry and a row in the README's Agents table.
Picking a magpie model for the "Nowledge Mem / AI Now" agent points
Nowledge's Default, AI Now and Background purposes at one native
`magpie:nowledge-agent` provider instance; disconnecting restores what the
user had. Distinct from packages/nowledge-mem (gateway recall/capture).

The descriptor (nowledge-mem.agent.js) records the selection; a
dependency-free Bun main syncs it through Nowledge's /remote-llm/* routes
with a journal, manual-edit detection, a restore phase, a pid lock and 0600
atomic writes. Remote backends need HTTPS with a key, redirects are
refused, and unchanged remote polls back off to 30s.

Reviewed head d3b6e3937b371dea24839fe602818c4e6aa4d03d, merged onto
origin/main 049295f5 (clean). Run on mote (macOS) and WSL2 Ubuntu 24.04,
Bun 1.4.2, HOME=$(mktemp -d) each run:
- bun test packages/nowledge-mem-agent: 28 pass, 0 fail (both machines)
- bun scripts/check.mjs nowledge-mem-agent: ok (it skips agent hooks)
- bun scripts/check.mjs: exit 0
- repo-root bun test: 6608 pass, 20 skip, 0 fail (479 files)

Broken by hand, one at a time, each red on the named test:
redirect: 'error'; the quiet-period return; the remote backoff skip;
!referenced before provider removal; defaultRestored before turning
Remote off; the native provider_type check; the per-purpose shape check;
the sandbox's XDG containment; the sandbox's agent-home deletes; the
sandbox's HOME check (its two halves together; each alone is covered by
the other).

Not tried by the reviewer: a real Nowledge Mem app or nmem (the author
reports a contract run on Nowledge 0.10.115), the opt-in smoke and
contract scripts, the runtime inside magpie's plugin host, Windows.
2026-11-07 12:45:50 +08:00

50 lines
1.9 KiB
JavaScript

import fs from 'node:fs/promises';
import path from 'node:path';
import { randomUUID } from 'node:crypto';
export async function readJSON(file) {
try { return JSON.parse(await fs.readFile(file, 'utf8')); }
catch (error) { if (error.code === 'ENOENT') return null; throw error; }
}
export async function writeJSON(file, value) {
await fs.mkdir(path.dirname(file), { recursive: true, mode: 0o700 });
const temp = `${file}.${randomUUID()}.tmp`;
try {
const handle = await fs.open(temp, 'wx', 0o600);
try { await handle.writeFile(`${JSON.stringify(value, null, 2)}\n`); await handle.sync(); }
finally { await handle.close(); }
await fs.rename(temp, file);
} finally { await fs.rm(temp, { force: true }); }
}
export async function withLock(stateFile, run) {
const lock = `${stateFile}.lock`;
await fs.mkdir(path.dirname(lock), { recursive: true, mode: 0o700 });
let handle;
for (let attempt = 0; attempt < 2; attempt++) {
try { handle = await fs.open(lock, 'wx', 0o600); break; }
catch (error) {
if (error.code !== 'EEXIST') throw error;
let stale = false;
try {
const owner = await readJSON(lock);
if (Number.isSafeInteger(owner?.pid) && owner.pid > 0) {
try { process.kill(owner.pid, 0); }
catch (e) { if (e.code === 'ESRCH') stale = true; }
}
} catch (e) {
if (e.code === 'ENOENT') continue;
// A process can die between creating and filling the lock.
if (e instanceof SyntaxError) stale = Date.now() - (await fs.stat(lock)).mtimeMs > 30000;
else throw e;
}
if (!stale) throw new Error('Another Nowledge sync is running; retry shortly.');
await fs.rm(lock, { force: true });
}
}
if (!handle) throw new Error('Could not acquire Nowledge sync lock.');
try { await handle.writeFile(JSON.stringify({ pid: process.pid })); return await run(); }
finally { await handle.close(); await fs.rm(lock, { force: true }); }
}