import { c as getProjectRoot, b as getLogger } from './paths-2M_UxD_H.js';
import 'node:fs';
import 'node:path';

//#region ../core/dist/audit.js
var log = getLogger("audit");
/**
* Query audit entries from SQLite audit_log table.
* Used by session-grade.ts for behavioral analysis.
*
* Returns entries ordered chronologically (ASC) to preserve
* behavioral sequence for grading analysis.
*/
async function queryAudit(options) {
	try {
		const { getDb } = await import('./sqlite2-UG45Z53q.js');
		const { auditLog } = await import('./tasks-schema2-DJGD-IKf.js');
		const { and, eq, gte, or } = await import('./index-DIWrtHjY.js');
		const db = await getDb(getProjectRoot());
		const conditions = [];
		if (options?.sessionId) conditions.push(eq(auditLog.sessionId, options.sessionId));
		if (options?.domain) conditions.push(eq(auditLog.domain, options.domain));
		if (options?.operation) conditions.push(or(eq(auditLog.operation, options.operation), eq(auditLog.action, options.operation)));
		if (options?.taskId) conditions.push(eq(auditLog.taskId, options.taskId));
		if (options?.since) conditions.push(gte(auditLog.timestamp, options.since));
		const limit = options?.limit ?? 1e3;
		return (await db.select().from(auditLog).where(conditions.length > 0 ? and(...conditions) : void 0).orderBy(auditLog.timestamp).limit(limit)).map((row) => ({
			timestamp: row.timestamp,
			sessionId: row.sessionId,
			domain: row.domain ?? "unknown",
			operation: row.operation ?? row.action,
			params: row.detailsJson ? JSON.parse(row.detailsJson) : {},
			result: {
				success: row.success === 1,
				exitCode: row.success === 1 ? 0 : 1,
				duration: row.durationMs ?? 0
			},
			metadata: {
				taskId: row.taskId !== "system" && row.taskId !== "unknown" ? row.taskId : void 0,
				source: row.source ?? "cli",
				gateway: row.gateway
			},
			error: row.errorMessage ?? void 0
		}));
	} catch (err) {
		log.warn({ err }, "Failed to query audit entries from SQLite");
		return [];
	}
}

export { queryAudit as q };
//# sourceMappingURL=audit4-S87oN-BC.js.map
