// storage/spec-observability-store.ts — Persistence for spec usage events (SPEC-158)
// Append-only log at data/projects/{projectId}/spec-usage.json
// Zero-overhead when not queried: writes are fire-and-forget.
import { readJson, writeJson, projectDataDir } from './base-store.js';
import { withFileLock } from './file-mutex.js';
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const MAX_EVENTS = 10_000;
// ---------------------------------------------------------------------------
// File path
// ---------------------------------------------------------------------------
function usageFile(projectId) {
    return `${projectDataDir(projectId)}/spec-usage.json`;
}
// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------
function emptyLog() {
    return { events: [] };
}
async function loadLog(projectId) {
    return readJson(usageFile(projectId), emptyLog());
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Append a spec usage event for the given project.
 * When the event count exceeds MAX_EVENTS, the oldest entries are trimmed (FIFO).
 * This is intended to be called fire-and-forget from tool wrappers.
 */
export async function recordSpecAccess(projectId, event) {
    await withFileLock(usageFile(projectId), async () => {
        const log = await loadLog(projectId);
        const events = [...log.events, event];
        const trimmed = events.length > MAX_EVENTS ? events.slice(events.length - MAX_EVENTS) : events;
        await writeJson(usageFile(projectId), { events: trimmed });
    });
}
/**
 * Read all spec usage events for the given project.
 */
export async function getSpecUsageLog(projectId) {
    return loadLog(projectId);
}
/**
 * Remove all events (useful for testing / reset).
 */
export async function clearSpecUsageLog(projectId) {
    await withFileLock(usageFile(projectId), async () => {
        await writeJson(usageFile(projectId), emptyLog());
    });
}
//# sourceMappingURL=spec-observability-store.js.map