Entropic 2.3.8
Local-first agentic inference engine
Loading...
Searching...
No Matches
entropic_audit.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
13#include "engine_handle.h"
14
15#include <entropic/entropic.h>
18#include "json_serializers.h"
19#include <fstream>
20#include <string>
21
22static auto logger = entropic::log::get("facade.audit");
23
31extern "C" ENTROPIC_EXPORT entropic_error_t
33 if (!handle) { return ENTROPIC_ERROR_INVALID_HANDLE; }
34 if (!handle->audit_logger) { return ENTROPIC_OK; }
35 handle->audit_logger->flush();
36 return ENTROPIC_OK;
37}
38
46extern "C" ENTROPIC_EXPORT entropic_error_t
48 if (!handle || !count) {
49 return !handle ? ENTROPIC_ERROR_INVALID_HANDLE
51 }
52 *count = handle->audit_logger
53 ? handle->audit_logger->entry_count() : 0;
54 return ENTROPIC_OK;
55}
56
66extern "C" ENTROPIC_EXPORT entropic_error_t
68 entropic_handle_t handle,
69 const char* path,
70 const char* /*filter_json*/,
71 char** result_json) {
72 if (!handle || !path || !result_json) {
73 return !handle ? ENTROPIC_ERROR_INVALID_HANDLE
75 }
76 try {
77 std::ifstream file(path);
78 if (!file.is_open()) { throw std::runtime_error("cannot open"); }
79 nlohmann::json arr = nlohmann::json::array();
80 std::string line;
81 while (std::getline(file, line)) {
82 if (line.empty()) { continue; }
83 // Issue #3 (v2.1.1): inbound boundary from disk. Audit files
84 // written by pre-2.1.1 engine versions (or by future writers
85 // that miss a sanitize step) may contain bytes that parse
86 // fine but throw on the arr.dump() below. Sanitize before
87 // ingestion so the dump cannot fail. See
88 // include/entropic/mcp/utf8_sanitize.h for the boundary policy.
89 arr.push_back(nlohmann::json::parse(
90 entropic::mcp::sanitize_utf8(line)));
91 }
92 *result_json = strdup(arr.dump().c_str());
93 return ENTROPIC_OK;
94 } catch (const std::exception& e) {
95 handle->last_error = e.what();
96 return ENTROPIC_ERROR_IO;
97 }
98}
Private definition of the entropic_engine struct.
Public C API for the Entropic inference engine.
ENTROPIC_EXPORT entropic_error_t entropic_audit_flush(entropic_handle_t handle)
Flush the audit logger to disk.
ENTROPIC_EXPORT entropic_error_t entropic_audit_read(entropic_handle_t handle, const char *path, const char *, char **result_json)
Read audit log entries from a JSONL file.
ENTROPIC_EXPORT entropic_error_t entropic_audit_count(entropic_handle_t handle, size_t *count)
Get the number of audit log entries this session.
entropic_error_t
Error codes returned by all C API functions.
Definition error.h:35
@ ENTROPIC_OK
Success.
Definition error.h:36
@ ENTROPIC_ERROR_INVALID_ARGUMENT
NULL pointer, empty string, out-of-range value.
Definition error.h:37
@ ENTROPIC_ERROR_INVALID_HANDLE
NULL or destroyed handle (v1.8.9)
Definition error.h:55
@ ENTROPIC_ERROR_IO
File/network I/O error.
Definition error.h:50
JSON serialization helpers for the facade.
spdlog initialization and logger access.
ENTROPIC_EXPORT std::shared_ptr< spdlog::logger > get(const std::string &name)
Get or create a named logger.
Definition logging.cpp:211
Engine handle struct — owns all subsystems.
std::unique_ptr< entropic::AuditLogger > audit_logger
Audit log.
std::string last_error
Per-handle error message.
UTF-8 validation + replacement at every system boundary where bytes change ownership.