TraceMaker
C++ API of the engine
Loading...
Searching...
No Matches
advisor.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#pragma once
3// The advisor protocol (design doc 17 §5): an advisor is any executable that reads a stuck report (JSON) on stdin and writes
4// an advice file (JSON) on stdout. The engine starts it through the shell, with a time limit, and treats whatever comes back
5// as untrusted: the answer must be exactly one advice file and passes the same strict checks as a file written by hand.
6// A missing, slow, failing or nonsensical advisor is a warning, never an error of the run (rule 5, doc 17 §6).
7#include <string>
8
9#include <nlohmann/json.hpp>
10
11#include "advice/advice.hpp"
12
13namespace tmk::advice {
14
16 bool started = false;
17 int exit_code = -1; // -1: killed by a signal or never started
18 bool timed_out = false;
19 bool out_truncated = false; // more output than the limit
20 std::string out, err;
21 double seconds = 0;
22};
23// Runs `command` with /bin/sh -c in its own process group, feeds it `input` on stdin, and collects stdout (at most
24// `max_out` bytes) and stderr (at most `max_err`). After `timeout_s` seconds the whole group is killed.
25ProcessResult run_process(const std::string& command, const std::string& input, double timeout_s, std::size_t max_out = std::size_t{1} << 20,
26 std::size_t max_err = std::size_t{1} << 16);
27
29 std::string command;
30 double timeout_s = 120;
31};
32
33struct Consulted {
34 bool ok = false;
35 Advice advice; // aliases undone; names are not yet checked against the board
36 std::string text; // exactly what the advisor wrote (the file saved beside the output)
37 std::string error; // why there is no advice
38 nlohmann::json summary; // {"program", "seconds", "exit_code", "timed_out", "bytes_in", "bytes_out", "ok", "error"?, "stderr"?}
39};
40// Sends `report` to the advisor and reads its advice. The command line itself is not logged or put in the summary (it may carry
41// credentials); only the program name is.
42Consulted consult_advisor(const AdvisorOptions& opt, const nlohmann::json& report, const AliasMap& aliases, const std::string& advice_name);
43
44} // namespace tmk::advice
The advice file (design doc 17, issue #26): typed hints for the router, written by a person,...
Consulted consult_advisor(const AdvisorOptions &opt, const nlohmann::json &report, const AliasMap &aliases, const std::string &advice_name)
Sends report to the advisor and reads its advice.
ProcessResult run_process(const std::string &command, const std::string &input, double timeout_s, std::size_t max_out=std::size_t{1}<< 20, std::size_t max_err=std::size_t{1}<< 16)
Runs command with /bin/sh -c in its own process group, feeds it input on stdin, and collects stdout (...
Aliases for net names and part references (a redacted stuck report): the advice an advisor writes use...
Definition advice.hpp:105
nlohmann::json summary
{"program", "seconds", "exit_code", "timed_out", "bytes_in", "bytes_out", "ok", "error"?...
Definition advisor.hpp:38
Advice advice
aliases undone; names are not yet checked against the board
Definition advisor.hpp:35
std::string text
exactly what the advisor wrote (the file saved beside the output)
Definition advisor.hpp:36
std::string error
why there is no advice
Definition advisor.hpp:37
bool out_truncated
more output than the limit
Definition advisor.hpp:19
int exit_code
-1: killed by a signal or never started
Definition advisor.hpp:17