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) {

View file

@ -122,9 +122,24 @@ namespace Crafter::Review::Prompt {
}
}
namespace {
std::map<std::string, std::filesystem::path>& Sections() {
static std::map<std::string, std::filesystem::path> files;
return files;
}
}
void SetLensSections(std::map<std::string, std::filesystem::path> files) {
Sections() = std::move(files);
}
std::filesystem::path LensFile(std::string_view name) {
return LensDir() / std::string(name);
}
std::string SystemText(std::string_view mode) {
if (mode == "reply" || mode == "series") return Dyn::Replace(Dyn::ReadText(LensDir() / "_common.md"), "{{ARTIFACT_KIND}}", mode == "reply" ? "mailing-list reply" : "patch series");
return Dyn::Replace(Dyn::ReadText(LensDir() / "_common-repo.md"), "{{ARTIFACT_KIND}}", "commit range");
if (mode == "reply" || mode == "series") return Dyn::Replace(Dyn::ReadText(LensFile("_common.md")), "{{ARTIFACT_KIND}}", mode == "reply" ? "mailing-list reply" : "patch series");
return Dyn::Replace(Dyn::ReadText(LensFile("_common-repo.md")), "{{ARTIFACT_KIND}}", "commit range");
}
namespace {
@ -166,8 +181,43 @@ namespace Crafter::Review::Prompt {
Dyn::Join(inputs, "\n"), hash, ArtifactLines(lines), units.size(), InventoryLines(units), refDigest);
}
Section ReadSection(const std::filesystem::path& file) {
Section out;
std::error_code error;
const std::filesystem::file_status entry = std::filesystem::symlink_status(file, error);
if (error && error != std::errc::no_such_file_or_directory && error != std::errc::not_a_directory) {
out.present = true;
out.problem = std::format("cannot be read ({})", error.message());
return out;
}
if (!std::filesystem::exists(entry)) return out;
out.present = true;
if (!std::filesystem::is_regular_file(std::filesystem::status(file, error))) {
out.problem = std::format("cannot be read ({})", error ? error.message() : std::string("not a regular file"));
return out;
}
try {
out.text = Dyn::ReadText(file);
} catch (const Dyn::Error& failure) {
out.problem = failure.Kind() == "UnicodeDecodeError" ? std::string("is not UTF-8") : std::string("cannot be read");
}
return out;
}
std::string Charter(std::string_view lensFile) {
return Dyn::ReadText(LensDir() / std::string(lensFile));
std::string text = Dyn::ReadText(LensFile(lensFile));
if (const auto own = Sections().find(std::string(lensFile)); own != Sections().end()) {
// Through the reader the config checks it with: a section the
// config reported never reaches a run, and none is dropped here.
const Section section = ReadSection(own->second);
if (!section.problem.empty() || !section.present) {
throw Dyn::Error("ValueError", std::format("lenses.{}.section: {} {}", Plan::LensName(lensFile), own->second.string(),
section.present ? section.problem : std::string("does not exist")));
}
while (text.ends_with('\n')) text.pop_back();
text += std::format("\n\n{}", section.text);
}
return text;
}
std::string RunLayer(std::string_view artifactRel, std::string_view base, std::string_view date, std::span<const std::string> trees, std::string_view hash,

View file

@ -615,6 +615,19 @@ namespace Crafter::Review::Run {
log(std::format("reads: every code unit of the {}code lenses needs {} clean reads, one per round; this is round {} on this hash{}", Kernel() ? "" : "static ", x, round,
newRound || !sameHash ? std::string() : std::string(" (continuing an unfinished run)")));
}
// The repository's own lens sections (lenses.<name>.section): each
// file is appended to its lens's shipped charter (Prompt::Charter), so
// the charter sha in every reuse key covers it. The config refused a
// path outside the repository and a file the run could not read.
const std::map<std::string, fs::path> sections = Config::LensSectionsOf(cfg, repo);
Prompt::SetLensSections(sections);
if (!sections.empty()) {
std::vector<std::string> own;
for (const std::string& lf : modeLenses) {
if (sections.contains(lf)) own.push_back(Plan::LensName(lf));
}
log(std::format("lenses: the repository's sections appended to {}", own.empty() ? std::string("no charter of this mode") : Dyn::Join(own, ", ")));
}
for (const Plan::Lens& lens : Plan::Lenses) {
const std::string lf(lens.file);
charters[lf] = Prompt::Charter(lf);