tool_call_history.cpp

src/mcp/tool_call_history.cpp

@file tool_call_history.cpp @brief ToolCallHistory ring buffer implementation.

Owners: @tvanfossen

ToolCallHistory

entropic::ToolCallHistory::ToolCallHistory(size_t capacity=100)

Construct with buffer capacity.

src/mcp/tool_call_history.cpp:30

Show on the map →

Called by none

Calls none

Source · 5 lines
ToolCallHistory::ToolCallHistory(size_t capacity)
    : capacity_(capacity) {
    buffer_.resize(capacity);
    logger->info("ToolCallHistory initialized (capacity={})", capacity);
}
Activity · 2 steps
lines 32–32buffer_.resize(capacity);lines 33–33logger->info("ToolCallHistory initialized (capacity={})", capacity);

Requirements

REQ-MCP-020

all

std::vector< ToolCallRecord > entropic::ToolCallHistory::all() const

Get all stored entries (oldest first, insertion order).

src/mcp/tool_call_history.cpp:97

Show on the map →

Called by SCENARIO, TEST_CASE, check_coverage, to_json

Calls none

Source · 13 lines
std::vector<ToolCallRecord> ToolCallHistory::all() const {
    std::shared_lock lock(mutex_);
    std::vector<ToolCallRecord> result;
    result.reserve(count_);

    size_t start = (count_ < capacity_) ? 0
                                        : head_;
    for (size_t i = 0; i < count_; ++i) {
        size_t idx = (start + i) % capacity_;
        result.push_back(buffer_[idx]);
    }
    return result;
}
Activity · 8 steps
lines 98–98std::shared_lock lock(mutex_);lines 99–99std::vector<ToolCallRecord> result;lines 100–100result.reserve(count_);lines 102–103size_t start = (count_ < capacity_) ? 0 : head_;lines 104–104◇ for (size_t i = 0; i < count_; ++i)lines 105–105size_t idx = (start + i) % capacity_;lines 106–106result.push_back(buffer_[idx]);lines 108–108return result;

Requirements

REQ-MCP-020

recent

std::vector< ToolCallRecord > entropic::ToolCallHistory::recent(size_t count) const

Get the N most recent entries (newest first).

src/mcp/tool_call_history.cpp:72

Show on the map →

Called by SCENARIO, TEST_CASE, to_json

Calls none

Source · 12 lines
std::vector<ToolCallRecord> ToolCallHistory::recent(size_t count) const {
    std::shared_lock lock(mutex_);
    size_t n = std::min(count, count_);
    std::vector<ToolCallRecord> result;
    result.reserve(n);

    for (size_t i = 0; i < n; ++i) {
        size_t idx = (head_ + capacity_ - 1 - i) % capacity_;
        result.push_back(buffer_[idx]);
    }
    return result;
}
Activity · 8 steps
lines 73–73std::shared_lock lock(mutex_);lines 74–74size_t n = std::min(count, count_);lines 75–75std::vector<ToolCallRecord> result;lines 76–76result.reserve(n);lines 78–78◇ for (size_t i = 0; i < n; ++i)lines 79–79size_t idx = (head_ + capacity_ - 1 - i) % capacity_;lines 80–80result.push_back(buffer_[idx]);lines 82–82return result;

Requirements

REQ-MCP-020

record

void entropic::ToolCallHistory::record(const ToolCallRecord &entry)

Record a completed tool call.

src/mcp/tool_call_history.cpp:49

Show on the map →

Called by SCENARIO, SCENARIO, TEST_CASE, record_tool_history

Calls none

Source · 10 lines
void ToolCallHistory::record(const ToolCallRecord& entry) {
    std::unique_lock lock(mutex_);
    buffer_[head_] = entry;
    head_ = (head_ + 1) % capacity_;
    if (count_ < capacity_) {
        ++count_;
    }
    logger->info("Recorded: tool='{}', {}/{} slots",
                 entry.tool_name, count_, capacity_);
}
Activity · 6 steps
lines 50–50std::unique_lock lock(mutex_);lines 51–51buffer_[head_] = entry;lines 52–52head_ = (head_ + 1) % capacity_;lines 53–53◇ if (count_ < capacity_)lines 54–54++count_;lines 56–57logger->info("Recorded: tool='{}', {}/{} slots", entry.tool_name, count_, capacity_);

Requirements

REQ-MCP-020

record_to_json

static nlohmann::json entropic::record_to_json(const ToolCallRecord &rec)

Serialize a single ToolCallRecord to JSON.

src/mcp/tool_call_history.cpp:120

Show on the map →

Called by to_json

Calls none

Source · 14 lines
static nlohmann::json record_to_json(const ToolCallRecord& rec) {
    nlohmann::json j;
    j["sequence"] = rec.sequence;
    j["tool_name"] = rec.tool_name;
    j["params_summary"] = rec.params_summary;
    j["status"] = rec.status;
    j["result_summary"] = rec.result_summary;
    j["elapsed_ms"] = rec.elapsed_ms;
    j["iteration"] = rec.iteration;
    if (!rec.error_detail.empty()) {
        j["error_detail"] = rec.error_detail;
    }
    return j;
}
Activity · 11 steps
lines 121–121nlohmann::json j;lines 122–122j["sequence"] = rec.sequence;lines 123–123j["tool_name"] = rec.tool_name;lines 124–124j["params_summary"] = rec.params_summary;lines 125–125j["status"] = rec.status;lines 126–126j["result_summary"] = rec.result_summary;lines 127–127j["elapsed_ms"] = rec.elapsed_ms;lines 128–128j["iteration"] = rec.iteration;lines 129–129◇ if (!rec.error_detail.empty())lines 130–130j["error_detail"] = rec.error_detail;lines 132–132return j;

Requirements

REQ-MCP-020

size

size_t entropic::ToolCallHistory::size() const

Current number of stored entries.

src/mcp/tool_call_history.cpp:163

Show on the map →

Called by SCENARIO, TEST_CASE, build_classification_prompt

Calls none

Source · 4 lines
size_t ToolCallHistory::size() const {
    std::shared_lock lock(mutex_);
    return count_;
}
Activity · 2 steps
lines 164–164std::shared_lock lock(mutex_);lines 165–165return count_;

Requirements

REQ-MCP-020

summarize_params

std::string entropic::summarize_params(const std::string &args_json)

Extract top-level JSON keys as a comma-separated summary.

src/mcp/tool_call_history.cpp:179

Show on the map →

Called by TEST_CASE, record_tool_history

Calls none

Source · 18 lines
std::string summarize_params(const std::string& args_json) {
    try {
        auto j = nlohmann::json::parse(args_json);
        if (!j.is_object()) {
            return args_json;
        }
        std::string result;
        for (auto it = j.begin(); it != j.end(); ++it) {
            if (!result.empty()) {
                result += ", ";
            }
            result += it.key();
        }
        return result;
    } catch (...) {
        return args_json;
    }
}
Activity · 1 steps
lines 180–195try { auto j = nlohmann::json::parse(args_json); if (!j.is_object()) { return args_json; } std::string result; for (auto it = j.begin(); it != j.end(); ++it) { if (!result.empty()) { result += ", "; } result += it.key(); } return result; } catch (...) { return args_json; }

Requirements

REQ-MCP-020

to_json

std::string entropic::ToolCallHistory::to_json(size_t count) const

Serialize recent entries to JSON array string.

src/mcp/tool_call_history.cpp:144

Show on the map →

Called by SCENARIO, SCENARIO, TEST_CASE, tool_history_json_thunk

Calls all, recent, record_to_json

Source · 11 lines
std::string ToolCallHistory::to_json(size_t count) const {
    auto entries = (count == 0) ? all() : recent(count);
    nlohmann::json arr = nlohmann::json::array();
    for (const auto& rec : entries) {
        arr.push_back(record_to_json(rec));
    }
    // gh#216: never throw on bad bytes. The history is handle-wide and
    // re-serialized every run, so one record that cannot be dumped failed
    // every later turn on the host. Invalid UTF-8 becomes U+FFFD.
    return arr.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace);
}
Activity · 5 steps
lines 145–145auto entries = (count == 0) ? all() : recent(count);lines 146–146nlohmann::json arr = nlohmann::json::array();lines 147–147◇ for (const auto& rec : entries)lines 148–148arr.push_back(record_to_json(rec));lines 153–153return arr.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace);

Requirements

REQ-MCP-020

truncate_result

std::string entropic::truncate_result(const std::string &text, size_t max_len)

Truncate a string to max_len characters with "..." suffix.

src/mcp/tool_call_history.cpp:215

Show on the map →

Called by SCENARIO, TEST_CASE, record_tool_history

Calls none

Source · 10 lines
std::string truncate_result(const std::string& text, size_t max_len) {
    if (text.size() <= max_len) {
        return text;
    }
    size_t cut = max_len;
    while (cut > 0 && (static_cast<unsigned char>(text[cut]) & 0xC0) == 0x80) {
        --cut;
    }
    return text.substr(0, cut) + "...";
}
Activity · 6 steps
lines 216–216◇ if (text.size() <= max_len)lines 217–217return text;lines 219–219size_t cut = max_len;lines 220–220◇ while (cut > 0 && (static_cast<unsigned char>(text[cut]) & 0xC0) == 0x80)lines 221–221--cut;lines 223–223return text.substr(0, cut) + "...";

Requirements

REQ-MCP-020