// SF Extension — Cache Invalidation
//
// Three module-scoped caches exist across the SF extension:
//   1. State cache (state.ts)  — memoized deriveState() result
//   2. Path cache  (paths.ts)  — directory listing results (readdirSync)
//   3. Parse cache (files.ts)  — parsed markdown file results
//
// After any file write that changes .sf/ contents, all three must be
// invalidated together to prevent stale reads. This module provides a
// single function that clears all three atomically.
import { clearParseCache } from "./files.js";
import { clearPathCache } from "./paths.js";
import { clearArtifacts } from "./sf-db.js";
import { invalidateStateCache } from "./state.js";
import { logWarning } from "./workflow-logger.js";
/**
 * Invalidate all SF runtime caches in one call.
 *
 * Call this after file writes, milestone transitions, merge reconciliation,
 * or any operation that changes .sf/ contents on disk. Forgetting to clear
 * any single cache causes stale reads (see #431, #793).
 *
 * Each cache clear is attempted independently; failures are logged but do not
 * prevent other caches from being cleared.
 */
export function invalidateAllCaches() {
	try {
		invalidateStateCache();
	} catch (err) {
		logWarning("state", `cache invalidation failed: ${err}`);
	}
	try {
		clearPathCache();
	} catch (err) {
		logWarning("state", `cache invalidation failed: ${err}`);
	}
	try {
		clearParseCache();
	} catch (err) {
		logWarning("state", `cache invalidation failed: ${err}`);
	}
	try {
		clearArtifacts();
	} catch (err) {
		logWarning("db", `cache invalidation failed: ${err}`);
	}
}
