UNPKG

@code-yeongyu/senpi

Version:

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

78 lines • 3.78 kB
import { appendFile, chmod, rename, stat } from "node:fs/promises"; import { isAbsolute, relative, resolve, sep } from "node:path"; import { redactSecrets } from "../sensitive-output.js"; /** The log rolls over to one `.1` generation past this size, so it stays bounded on a busy machine. */ export const BLOCK_LOG_MAX_BYTES = 256 * 1024; const EXCERPT_MAX_CHARS = 240; // Account identifiers a provider message can carry: e-mail addresses, UUIDs, and prefixed ids // (`user-…`, `org-…`, `acct_…`, `account_…`). Credential shapes are handled by redactSecrets. const EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g; const UUID = /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/gi; const PREFIXED_ID = /\b(?:user|org|acct|account|ws|workspace)[-_][A-Za-z0-9]{6,}\b/gi; export function redactBlockExcerpt(text) { const masked = redactSecrets(text) .replace(EMAIL, "[EMAIL]") .replace(UUID, "[ID]") .replace(PREFIXED_ID, "[ID]") .replace(/\s+/g, " ") .trim(); return masked.length > EXCERPT_MAX_CHARS ? `${masked.slice(0, EXCERPT_MAX_CHARS)}…` : masked; } // One write chain per log path: appends run in the order the blocks happened, and the size check and rollover of // one append cannot interleave with the next. const pendingWrites = new Map(); /** * Appends one entry without blocking the caller (the block is recorded on the session path, which must not run * synchronous fs work: senpi#3993). A log that cannot be written never fails the request that was blocked. */ export function appendCredentialBlockLog(path, entry) { const previous = pendingWrites.get(path) ?? Promise.resolve(); const next = previous.then(() => writeEntry(path, entry)); pendingWrites.set(path, next); void next.finally(() => { if (pendingWrites.get(path) === next) pendingWrites.delete(path); }); } /** Resolves once every entry appended to `path` so far is on disk (or was dropped as unwritable). */ export function flushCredentialBlockLog(path) { return pendingWrites.get(path) ?? Promise.resolve(); } /** * Resolves once every entry appended so far to a log inside `directory` is on disk. A caller that removes a tree * holding a pool state file (a test teardown, senpi#6488) joins here first: an append still running would * otherwise create the log while the removal walks the tree, and the removal fails with ENOTEMPTY. */ export async function settledCredentialBlockLogsWithin(directory) { await Promise.all(pendingCredentialBlockLogsWithin(directory).map(flushCredentialBlockLog)); } /** The block logs inside `directory` with an append still pending, for a caller that cannot wait (a synchronous removal). */ export function pendingCredentialBlockLogsWithin(directory) { return [...pendingWrites.keys()].filter((path) => contains(directory, path)); } // Lexical containment, as the host gc pass registry: another spelling of the same directory is not joined. function contains(parent, child) { const path = relative(resolve(parent), resolve(child)); return path === "" || (path !== ".." && !path.startsWith(`..${sep}`) && !isAbsolute(path)); } async function writeEntry(path, entry) { try { const size = await fileSize(path); if (size !== undefined && size >= BLOCK_LOG_MAX_BYTES) await rename(path, `${path}.1`); await appendFile(path, `${JSON.stringify(entry)}\n`, { mode: 0o600 }); await chmod(path, 0o600); } catch { // Best effort: the block itself is already recorded in the pool state. } } async function fileSize(path) { try { return (await stat(path)).size; } catch { return undefined; } } //# sourceMappingURL=block-log.js.map