feat(config): lens sections - a repository's own text appended to a shipped charter

A lens has one charter, the one this pipeline ships; what a repository
knows that is not general to the lens - its platform and layers, the
record it keeps its decisions in, what its users already have, the
calibration entries that are its own - goes in a file the config names
for the lens (lenses.<name>.section), and Charter appends it after one
blank line. The charter sha in every reuse key covers the combined text.

The config refuses a path outside the repository - absolute, or
climbing out with `..` - by the check slots.dir had, now one both
settings share; a file missing or one the run could not use (a dangling
link, unreadable, not UTF-8) is a problem too, checked through the
reader Charter uses (Prompt::ReadSection), so a section is never
dropped in silence, and a name that is no lens is the unknown key it
always was. Nothing lists a directory, so what lies beside the files is
no concern of the config's: a directory listed would have to tolerate or
refuse every foreign entry. The run logs which charters got one.

The better-solution charter gains a consumer's general entry
(2026-10-31: a mechanism reworked a second time in one submission is the
signal to derive the fix at every layer the data crosses). The procedure
names the section wherever it names the charter: among the config's keys,
in the lens inputs, in the reuse rule (an edit to either re-runs the lens)
and in the feedback loop, which routes a repository's own lesson to its
section and a general one to the shipped charter.
This commit is contained in:
Jorijn van der Graaf 2026-11-01 01:30:22 +01:00
commit 6cf15919db
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
16 changed files with 1235 additions and 524 deletions

View file

@ -11,6 +11,7 @@ import :Tools;
import :PgWire;
import :Process;
import :Platform;
import :Prompt;
import std;
namespace fs = std::filesystem;
@ -121,8 +122,10 @@ namespace Crafter::Review::Config {
Json Defaults() {
Json lenses = Json::object();
// Every lens is off until the repository's config turns it on
// (2026-10-31, Jorijn: "all lenses off by default and opt in only").
for (const Plan::Lens& lens : Plan::Lenses) lenses[Plan::LensName(lens.file)] = {{"run", false}, {"environment", lens.env}};
// (2026-10-31, Jorijn: "all lenses off by default and opt in only");
// its section is a file of the repository's own appended to its
// shipped charter (null: none).
for (const Plan::Lens& lens : Plan::Lenses) lenses[Plan::LensName(lens.file)] = {{"run", false}, {"environment", lens.env}, {"section", nullptr}};
return {
// The integration branch a range is measured against (e.g.
// origin/dev): merge-base HEAD <base>.
@ -204,9 +207,23 @@ namespace Crafter::Review::Config {
for (const std::string& warning : warnings) warningList.push_back(warning);
config["_warnings"] = std::move(warningList);
std::ranges::move(Validate(config), std::back_inserter(loaded.problems));
std::ranges::move(SectionProblems(config, repo), std::back_inserter(loaded.problems));
return loaded;
}
namespace {
// A path setting resolved against the repository root (slots.dir,
// lenses.<name>.section): a relative path that stays inside it - not
// blank, no leading `/`, no `..` part (2026-11-01: one check, so the
// settings cannot drift apart).
bool InsideRepository(const Json& value) {
if (!value.is_string()) return false;
const std::string& dir = value.get_ref<const std::string&>();
const std::vector<std::string> parts = Dyn::Split(Dyn::Replace(dir, "\\", "/"), "/");
return !Dyn::Strip(dir).empty() && !dir.starts_with('/') && !std::ranges::contains(parts, std::string(".."));
}
}
std::vector<std::string> Validate(const Json& config) {
std::vector<std::string> out;
const Json& trailers = Dyn::At(config, "aiTrailers");
@ -237,6 +254,8 @@ namespace Crafter::Review::Config {
}
if (!Dyn::Get(entry, "run").is_boolean()) out.push_back(std::format("lenses.{}.run must be true or false", it.key()));
if (!Dyn::In(Dyn::Get(entry, "environment"), environments)) out.push_back(std::format("lenses.{}.environment must be one of {}", it.key(), EnvironmentNames(", ")));
const Json& section = Dyn::Get(entry, "section");
if (!section.is_null() && !InsideRepository(section)) out.push_back(std::format("lenses.{}.section must be null or a file inside the repository (a relative path)", it.key()));
}
auto runs = [](const Json& entry) { return entry.is_object() && Dyn::Get(entry, "run") == true; };
// Every lens is off until the repository turns it on: a config that
@ -305,14 +324,7 @@ namespace Crafter::Review::Config {
if (!slots.is_object()) {
out.emplace_back("slots must be an object {dir, start, stop, memMb, startMb}");
} else if (!Dyn::Get(slots, "dir").is_null()) {
const Json& dir = Dyn::At(slots, "dir");
bool bad = !dir.is_string();
if (!bad) {
const std::string& d = dir.get_ref<const std::string&>();
const std::vector<std::string> parts = Dyn::Split(Dyn::Replace(d, "\\", "/"), "/");
bad = Dyn::Strip(d).empty() || d.starts_with('/') || std::ranges::contains(parts, std::string(".."));
}
if (bad) out.emplace_back("slots.dir must be null or a directory inside the repository (a relative path)");
if (!InsideRepository(Dyn::At(slots, "dir"))) out.emplace_back("slots.dir must be null or a directory inside the repository (a relative path)");
for (const std::string_view key : {"start", "stop"}) {
const Json& value = Dyn::Get(slots, key);
if (!value.is_null() && (!value.is_string() || !value.get_ref<const std::string&>().contains("{n}"))) {
@ -373,6 +385,31 @@ namespace Crafter::Review::Config {
return {email};
}
std::map<std::string, fs::path> LensSectionsOf(const Json& config, const fs::path& repo) {
// Only a file inside the repository, by the rule Validate refuses the
// rest by: fs::path's `/` would let an absolute one replace the root.
std::map<std::string, fs::path> out;
for (const Plan::Lens& lens : Plan::Lenses) {
const Json& section = Dyn::Get(LensEntry(config, lens.file), "section");
if (InsideRepository(section)) out.emplace(std::string(lens.file), repo / section.get<std::string>());
}
return out;
}
std::vector<std::string> SectionProblems(const Json& config, const fs::path& repo) {
// Through the reader the run itself uses (Prompt::ReadSection): a
// section the run could not use is a problem here, never a section
// left out in silence or an error in the run's Gate 0.
std::vector<std::string> out;
for (const auto& [lensFile, file] : LensSectionsOf(config, repo)) {
const std::string where = std::format("lenses.{}.section: {}", Plan::LensName(lensFile), Dyn::Str(Dyn::Get(LensEntry(config, lensFile), "section")));
const Prompt::Section section = Prompt::ReadSection(file);
if (!section.problem.empty()) out.push_back(std::format("{} {}", where, section.problem));
else if (!section.present) out.push_back(std::format("{} does not exist", where));
}
return out;
}
std::vector<std::string> LensesOff(const Json& config) {
std::vector<std::string> out;
for (const Plan::Lens& lens : Plan::Lenses) {