Reuse the existing resolver/sampler for opt-in aggregate training summaries. Reject incomplete or undecodable snapshots without usable partial reports, retain best-effort readers elsewhere, and document cooperative extent checks rather than atomic snapshot guarantees. Include command-table/help, JSON, fixture and corruption regressions; no model training or promotion.
91 lines
3.9 KiB
C++
91 lines
3.9 KiB
C++
// Non-installed fixture writer for CLI contract tests, never a training command.
|
|
#include "strawberry/database.hpp"
|
|
#include "strawberry/learn/replay.hpp"
|
|
#include "strawberry/telemetry.hpp"
|
|
#include "strawberry/store/log_store.hpp"
|
|
|
|
#include <filesystem>
|
|
#include <iostream>
|
|
#include <nlohmann/json.hpp>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
int main(int argc, char** argv) {
|
|
const std::string_view mode = argc == 3 ? argv[2] : "full";
|
|
const bool empty = mode == "empty", malformed = mode == "malformed-label", sealed = mode == "sealed";
|
|
if (argc < 2 || argc > 3 || (mode != "full" && !empty && !malformed && !sealed) ||
|
|
std::filesystem::exists(argv[1])) return 2;
|
|
using namespace strawberry;
|
|
constexpr int64_t sec = 1'000'000'000;
|
|
constexpr int64_t t0 = 1'800'000'000LL * sec;
|
|
Database db;
|
|
if (!db.open(argv[1])) return 3;
|
|
if (!empty) {
|
|
std::vector<TelemetryEvent> events;
|
|
for (int i = 0; i < 4; ++i) {
|
|
ProcessExecRecord e;
|
|
e.timestamp_ns = t0 + i * 61 * sec;
|
|
e.pid = 5000 + i;
|
|
e.process_node_id = "synthetic_process_" + std::to_string(i);
|
|
e.exe = "/synthetic/program-" + std::to_string(i);
|
|
e.cmdline = e.exe;
|
|
e.sha256 = std::string(64, static_cast<char>('a' + i));
|
|
e.source = "live";
|
|
events.push_back({store::RecordType::process_exec, encode_process_exec(e)});
|
|
if (i < 2) {
|
|
ScanResultRecord s;
|
|
s.timestamp_ns = e.timestamp_ns + sec;
|
|
s.pid = e.pid;
|
|
s.process_node_id = e.process_node_id;
|
|
s.rules = {"SyntheticTeacher"};
|
|
events.push_back({store::RecordType::scan_result, encode_scan_result(s)});
|
|
}
|
|
}
|
|
// Two blocks let the real CLI prove it does not export a valid
|
|
// prefix when the later block is corrupt.
|
|
std::vector<TelemetryEvent> first(events.begin(), events.begin() + 3);
|
|
std::vector<TelemetryEvent> last(events.begin() + 3, events.end());
|
|
if (!db.append_telemetry(first, true) || !db.append_telemetry(last, true)) return 4;
|
|
LabelRecord negative;
|
|
negative.timestamp_ns = t0 + 300 * sec;
|
|
negative.polarity = LabelPolarity::negative;
|
|
negative.source = "operator";
|
|
negative.action = "allowlist-add";
|
|
negative.sha256 = std::string(64, 'a');
|
|
negative.allowlist_entry = "synthetic-entry-a";
|
|
if (!db.append_evidence(store::RecordType::label, encode_label(negative))) return 5;
|
|
LabelRecord positive;
|
|
positive.timestamp_ns = t0 + 301 * sec;
|
|
positive.polarity = LabelPolarity::positive;
|
|
positive.source = "operator";
|
|
positive.action = "quarantine";
|
|
positive.sha256 = std::string(64, 'd');
|
|
positive.rule = "SyntheticQuarantine";
|
|
if (!db.append_evidence(store::RecordType::label, encode_label(positive))) return 6;
|
|
if (malformed && !db.append_evidence(store::RecordType::label, std::string(1, static_cast<char>(0x80)))) return 8;
|
|
}
|
|
db.close();
|
|
if (malformed) {
|
|
std::cout << "{\"malformed_label\":true}\n";
|
|
return 0;
|
|
}
|
|
if (sealed) {
|
|
store::StoreOptions so;
|
|
so.dir = argv[1];
|
|
store::StoreOpenReport opened;
|
|
std::string error;
|
|
auto log = store::LogStore::open(so, opened, error);
|
|
if (!log || !log->roll(store::Stream::telemetry)) return 9;
|
|
}
|
|
learn::ReplayOptions options;
|
|
options.collect_examples = true;
|
|
options.warmup_ns = 0;
|
|
std::string error;
|
|
const auto r = learn::replay_store(argv[1], options, error);
|
|
std::size_t positives = 0;
|
|
for (const auto& e : r.examples) positives += e.label > 0.5f;
|
|
std::cout << nlohmann::json{{"error", error}, {"eligible", r.example_windows},
|
|
{"eligible_positive", r.example_positives}, {"retained", r.examples.size()},
|
|
{"retained_positive", positives}}.dump() << '\n';
|
|
return error.empty() ? 0 : 7;
|
|
}
|