Step 102: memory strategy dashboard

This commit is contained in:
Bill
2026-02-09 10:41:54 -07:00
parent 7738c3d6b4
commit 60ae9e2d70
5 changed files with 182 additions and 0 deletions

View File

@@ -0,0 +1,88 @@
#pragma once
// Step 102: Memory strategy dashboard helpers
#include <string>
#include <vector>
#include <map>
#include <nlohmann/json.hpp>
#include "ast/ASTNode.h"
#include "ast/Annotation.h"
#include "ast/Function.h"
#include "ast/Module.h"
struct AnnotationEntry {
std::string nodeId;
std::string nodeName;
std::string label;
int line = -1;
};
inline bool isMemoryAnnotation(const ASTNode* anno) {
if (!anno) return false;
const auto& ct = anno->conceptType;
return ct == "DeallocateAnnotation" || ct == "LifetimeAnnotation" ||
ct == "ReclaimAnnotation" || ct == "OwnerAnnotation" ||
ct == "AllocateAnnotation";
}
inline std::string annotationLabelFor(const ASTNode* anno) {
if (!anno) return "";
if (anno->conceptType == "ReclaimAnnotation") {
auto* a = static_cast<const ReclaimAnnotation*>(anno);
return "@Reclaim(" + a->strategy + ")";
}
if (anno->conceptType == "OwnerAnnotation") {
auto* a = static_cast<const OwnerAnnotation*>(anno);
return "@Owner(" + a->strategy + ")";
}
if (anno->conceptType == "DeallocateAnnotation") {
auto* a = static_cast<const DeallocateAnnotation*>(anno);
return "@Deallocate(" + a->strategy + ")";
}
if (anno->conceptType == "LifetimeAnnotation") {
auto* a = static_cast<const LifetimeAnnotation*>(anno);
return "@Lifetime(" + a->strategy + ")";
}
if (anno->conceptType == "AllocateAnnotation") {
auto* a = static_cast<const AllocateAnnotation*>(anno);
return "@Allocate(" + a->strategy + ")";
}
return anno->conceptType;
}
inline std::string nodeNameFor(const ASTNode* node) {
if (!node) return "";
if (node->conceptType == "Function") return static_cast<const Function*>(node)->name;
if (node->conceptType == "Module") return static_cast<const Module*>(node)->name;
return node->conceptType;
}
inline void collectAnnotationEntries(const ASTNode* node, std::vector<AnnotationEntry>& out) {
if (!node) return;
for (auto* anno : node->getChildren("annotations")) {
if (!isMemoryAnnotation(anno)) continue;
AnnotationEntry e;
e.nodeId = node->id;
e.nodeName = nodeNameFor(node);
e.label = annotationLabelFor(anno);
if (node->hasSpan()) e.line = node->spanStartLine;
out.push_back(std::move(e));
}
for (auto* child : node->allChildren()) {
collectAnnotationEntries(child, out);
}
}
inline nlohmann::json buildAnnotationSummaryJson(const std::vector<AnnotationEntry>& entries) {
nlohmann::json j;
j["annotations"] = nlohmann::json::array();
for (const auto& e : entries) {
j["annotations"].push_back({
{"nodeId", e.nodeId},
{"nodeName", e.nodeName},
{"label", e.label},
{"line", e.line}
});
}
return j;
}

View File

@@ -32,6 +32,7 @@
#include "ASTMutationAPI.h"
#include "MemoryStrategyInference.h"
#include "AnnotationConflict.h"
#include "StrategyDashboard.h"
#include "ast/Generator.h"
#include "ast/Annotation.h"
@@ -1826,6 +1827,55 @@ int main(int, char**) {
ImGui::End();
// ---------------------------------------------------------------
// Memory Strategies dashboard
// ---------------------------------------------------------------
ImGui::Begin("Memory Strategies");
ImGui::PushFont(uiFont);
Module* ast = state.active() ? state.active()->sync.getAST() : nullptr;
if (!ast) {
ImGui::TextDisabled("(no AST)");
} else {
std::vector<AnnotationEntry> entries;
collectAnnotationEntries(ast, entries);
std::map<std::string, int> counts;
for (const auto& e : entries) counts[e.label]++;
ImGui::Text("Annotations");
ImGui::Separator();
for (const auto& [label, count] : counts) {
ImGui::Text("%s: %d", label.c_str(), count);
}
ImGui::Spacing();
ImGui::Text("Details");
ImGui::Separator();
for (const auto& e : entries) {
std::string lineInfo = e.line >= 0 ? ("L" + std::to_string(e.line + 1)) : "-";
std::string row = e.label + "" + e.nodeName + " (" + lineInfo + ")";
if (ImGui::Selectable(row.c_str())) {
if (state.active()) state.jumpTo(state.active(), e.line, 0);
}
}
ImGui::Spacing();
ImGui::Text("Suggestions");
ImGui::Separator();
for (const auto& s : state.suggestions) {
if (s.confidence < 0.5) continue;
std::string row = s.annotationType + "(" + s.strategy + ") — " +
s.nodeId + " (" + std::to_string(s.confidence) + ")";
ImGui::TextUnformatted(row.c_str());
}
if (ImGui::Button("Export JSON")) {
auto j = buildAnnotationSummaryJson(entries);
state.outputLog += "Annotation summary:\\n" + j.dump(2) + "\\n";
}
}
ImGui::PopFont();
ImGui::End();
// ---------------------------------------------------------------
// Status bar
// ---------------------------------------------------------------