UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

58 lines • 2.71 kB
import { readSkillMarkdownSource, SKILL_FRONTMATTER_PREFIX_BYTES } from "./skill-discovery.js"; /** * Coarse filesystem clocks (Linux jiffies, FAT's 2 s) can give a same-size rewrite the same stamp as * the read before it. As git does for racy index entries, a read is reused only when the file was last * changed well before that read, so no later write can share its stamp. The change time is the later * of mtime and ctime: cp -p, tar, rsync -t and utimes restore an old mtime, and only ctime moves. */ const RACY_WRITE_WINDOW_MS = 3000; const DEFAULT_LIMITS = { maxEntryBytes: 8 * SKILL_FRONTMATTER_PREFIX_BYTES, maxTotalBytes: 8 * 1024 * 1024, }; function statStamp(stats) { return `${stats.dev}:${stats.ino}:${stats.size}:${stats.mtimeMs}:${stats.ctimeMs}`; } /** `readSkillMarkdownSource` for user skill files, reusing the last read while `stats` still matches it. */ export function createSkillSourceCache(limits = DEFAULT_LIMITS, now = () => Date.now()) { const sources = new Map(); let totalBytes = 0; const forget = (filePath) => { const entry = sources.get(filePath); if (entry === undefined) return; sources.delete(filePath); totalBytes -= entry.bytes; }; return (filePath, stats) => { const stamp = statStamp(stats); const cached = sources.get(filePath); if (cached?.stamp === stamp) return cached.source; const source = readSkillMarkdownSource(filePath); forget(filePath); // The local clock judges the file's change time, as in git; a share whose clock runs behind ours by // more than the window can still make a just-written file look settled. if (Math.max(stats.mtimeMs, stats.ctimeMs) > now() - RACY_WRITE_WINDOW_MS) return source; const bytes = Buffer.byteLength(source); if (bytes > limits.maxEntryBytes) return source; sources.set(filePath, { stamp, source, bytes }); totalBytes += bytes; for (const oldest of sources.keys()) { if (totalBytes <= limits.maxTotalBytes) break; forget(oldest); } return source; }; } /** * Process-wide: a shared RPC host opens a fresh resource loader for every session, and each open * loaded every user skill twice (omo#11437). The stat the caller already took is the validator, so an * edited, replaced, or touched SKILL.md is read again on the next load and hot reload is unchanged. * Project skills never come here; their checked descriptor read stays per load. */ export const readUserSkillSource = createSkillSourceCache(); //# sourceMappingURL=skill-source-cache.js.map