diff --git a/.forgejo/workflows/ci.yaml b/.forgejo/workflows/ci.yaml index ec1a47a..0f2e7ad 100644 --- a/.forgejo/workflows/ci.yaml +++ b/.forgejo/workflows/ci.yaml @@ -140,7 +140,7 @@ jobs: # (/etc/crafter-flatpak: signing-key.asc and key-id) and mounted # read-only; it is imported into a throwaway keyring per run. if [ ! -s /run/crafter-flatpak/signing-key.asc ] || [ ! -s /run/crafter-flatpak/key-id ]; then - echo "no signing key under /etc/crafter-flatpak on the runner host; generate it as described in AGENTS.md (CI and publishing)" >&2 + echo "no signing key under /etc/crafter-flatpak on the runner host; generate it as described in AGENTS.template.md (CI and publishing)" >&2 exit 1 fi export GNUPGHOME=$(mktemp -d) diff --git a/.gitignore b/.gitignore index 67d2a68..bb506eb 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ share/crafter-build/ .claude # written by CI next to the manifest so relative source paths still resolve packaging/flatpak/ci-manifest.yaml +# written by every build and test run from AGENTS.template.md +/AGENTS.md diff --git a/AGENTS.md b/AGENTS.template.md similarity index 94% rename from AGENTS.md rename to AGENTS.template.md index 1c445f4..cb23933 100644 --- a/AGENTS.md +++ b/AGENTS.template.md @@ -11,6 +11,7 @@ crafter-build is a C++26 modules build system. A project is described by a `proj | `interfaces/Crafter.Build-.cppm` | Module interface partitions. `Crafter.Build.cppm` is the umbrella that re-exports them. | | `implementations/Crafter.Build-.cpp` | Implementation units, one per partition, named `Crafter.Build:_impl`. `main.cpp` is a thin wrapper around `Crafter::Run`. | | `project.cpp` | The self-host build description: a `crafter.build-lib` library, a `crafter.build-exe` executable, the test declarations, the lint rules and the `flatpak` and `install` scripts. | +| `AGENTS.template.md` | The source of AGENTS.md. Every build and test run writes AGENTS.md from it, with the notes the dependencies ship; AGENTS.md is gitignored, so changes go here. | | `tests//main.cpp` | One self-test per directory; `tests//fixture/` holds the sample project it builds. | | `examples/` | Standalone user-facing projects, MIT licensed. | | `build.sh` | Bootstrap script. It compiles everything by hand, without crafter-build. | @@ -29,7 +30,7 @@ Partitions and what they own: - `:Test` holds `AddTest`, `RunTests` and `TestRunner`. - `:Lint` is the lint/format driver plus the libclang token and AST layer. - `:Script` is the driver behind `crafter-build run`: it looks a name up in `cfg.scripts`, calls the callback and turns what it returns into the exit code. -- `:Agents` keeps the dependency notes section of a project's AGENTS.md: it walks the dependencies `cfg.dependencyNotes` selects, reads each one's `AGENTS.consumer.md` and merges them between the section's markers. Which Configurations belong to another project, and where it lives, comes from the loader (`LoadedProjectOf` in `:External`, `PragmaDependencies` in `:Platform`), because only the Configuration `GitProject` and `LocalProject` return has its `path` rebased. +- `:Agents` writes a project's AGENTS.md from its AGENTS.template.md: it walks the dependencies `cfg.dependencyNotes` selects, reads each one's `AGENTS.consumer.md` and merges them into the template's dependency notes section. Which Configurations belong to another project, and where it lives, comes from the loader (`LoadedProjectOf` in `:External`, `PragmaDependencies` in `:Platform`), because only the Configuration `GitProject` and `LocalProject` return has its `path` rebased. - `:Progress` is console output. `:Shader` compiles shaders through glslang. `:Asset` is the hook into Crafter.Asset. ## Dev environment @@ -141,7 +142,7 @@ These were decided deliberately. Do not undo them without the maintainer's say-s - Separate commits for the fix, its test and the README change when each stands on its own. - Update the README in the same PR when user-visible behaviour changes. - `format`, `lint` and `test` green before pushing. CI builds the Flatpak and runs the suite inside it on PRs, publishes the `beta` branch of the Catcrafts repo on every push to `master`, and the `stable` branch plus a release on an `X.Y.Z` tag. -- Never commit `bin/`, `build/`, `share/crafter-build/`, `lib/*.a` or fixture build outputs (all gitignored), and never edit the vendored files in `lib/`. +- Never commit `bin/`, `build/`, `share/crafter-build/`, `lib/*.a`, `AGENTS.md` or fixture build outputs (all gitignored), and never edit the vendored files in `lib/`. - Keep `CRAFTER_BUILD_MARCH=x86-64-v2` in the manifest for the shipped binary: it has to run on any x86_64 machine. User projects still default to `native`. ## Security considerations @@ -194,4 +195,4 @@ Local builds are two scripts in `project.cpp`. Run them with the native binary: - The wasm compile flags live in one place, `WasmBaselineFlags` in `Crafter.Build-Clang.cppm`: the std module and every unit have to agree on them, and `VariantId()` and the std PCM directory hash them, so a change rebuilds instead of mixing old and new BMIs. They enable C++ exceptions, so the link takes the exception-enabled libc++ and libunwind from the sysroot's `lib//eh/` (wasi-sdk 33 and later). The Flatpak's sysroot has them and a distro's may not; where they are missing the wasm tests skip, so run those through the Flatpak. A browser page checks the engine with the `WasmBaselineProbes` named in `variants.json`. - `WasiBrowserRuntimeExit` runs `wasi-runtime/runtime.js` under node and skips where there is none. The Flatpak has no node, so CI never runs it: after touching `runtime.js`, run the suite in the native loop with node on `PATH`. - `VariantId()` names the output directory, so CI globs `bin/crafter.build-exe---*/` rather than predicting the hash. The hash includes the compiler, so after a toolchain update the previous variant's directories stay in `bin/` and `build/` until `clean`, and such a glob matches both. -- The self-host build and `test` keep the dependency notes section of this very file. None of Crafter.Math, Crafter.Asset and Crafter.LintRules ships an `AGENTS.consumer.md` yet. Once one does, a build here can change AGENTS.md whenever that repository changes its notes, since all three are unpinned. The tree is then dirty, so the next build's `git describe --dirty` version differs, which is a new `crafter.build-lib` variant and a full rebuild. Commit the section to settle it. +- AGENTS.md is build output: the self-host build and `test` write it from `AGENTS.template.md`, with the `AGENTS.consumer.md` of each dependency that ships one (Crafter.Math, today) merged in, and it is gitignored. Edit the template; an edit made in AGENTS.md is gone after the next build. A fresh clone or worktree has no AGENTS.md until its first `./bin/crafter-build` or `test`, since `./build.sh` does not write it. A harness from before the template (3.0.0 and older) never creates one and keeps its section in one it finds; the next build with a current harness writes it over. diff --git a/README.md b/README.md index 77514ec..c57beb6 100644 --- a/README.md +++ b/README.md @@ -101,9 +101,23 @@ Such a dependency is built for the host, whatever the project targets, and its ` ### Dependency notes for agents -An agent working on a project reads its `AGENTS.md`, which says nothing about the libraries the project builds against unless someone copied that in by hand. A library ships it instead: an `AGENTS.consumer.md` beside its `project.cpp`, written for whoever uses the library rather than whoever works on it. When a consumer has an `AGENTS.md` beside its own `project.cpp`, `crafter-build` and `crafter-build test` keep its dependencies' notes in a section of it: +An agent working on a project reads its `AGENTS.md`, which says nothing about the libraries the project builds against unless someone copied that in by hand. A library ships it instead: an `AGENTS.consumer.md` beside its `project.cpp`, written for whoever uses the library rather than whoever works on it. + +A consumer that wants its dependencies' notes writes its own instructions in an `AGENTS.template.md` beside its `project.cpp`, and commits that instead of an `AGENTS.md`. `crafter-build` and `crafter-build test` then write `AGENTS.md` from the template with the notes added, so `AGENTS.md` is build output and stays out of version control: a dependency that changes its notes changes nothing a diff shows. A project with a committed `AGENTS.md` moves over from the directory of its `project.cpp`: + +```bash +git mv AGENTS.md AGENTS.template.md +echo /AGENTS.md >> .gitignore +``` + +What a build then writes: ```markdown + +# The template's text + +... + @@ -116,11 +130,13 @@ An agent working on a project reads its `AGENTS.md`, which says nothing about th ``` - Each file goes in as written, under a line naming the dependency: its repository's name, or a `LocalProject`'s directory name. That reads the same on every machine, and for a `--local` checkout and its git clone alike. The file lands among the consumer's own headings, so start it with a `##` heading that names the library. -- The section is appended the first time, after a blank line, and rewritten in place after that, wherever it has been moved to. Edits inside it are lost on the next build; the text around it is left as it is. The file is written only when the section changes, in place, so hard links, ownership and a read-only `AGENTS.md` are respected, and an `AGENTS.md` is never created. +- The section goes after the template's text and a blank line. To have it elsewhere, put the begin and end marker lines in the template where it should go: whatever they enclose is replaced. A template moved over from a committed `AGENTS.md` has them where its section was, around notes that can be deleted. +- `AGENTS.md` is written only when its text changes, in place, so a `CLAUDE.md` linked to it, its ownership and a read-only `AGENTS.md` are respected. An edit made in it is gone after the next build, and a fresh clone has none until its first build. The template is never written. +- An `AGENTS.md` that no build wrote, such as a committed one still next to a new template, is never replaced: each build warns instead, until it is deleted. Without a template, `AGENTS.md` is never touched. One that still holds a section from crafter-build 3.0.0, which kept the notes in a committed `AGENTS.md`, gets a warning: move it over as above, or delete the section. - The update runs once `project.cpp` has loaded and before the project compiles, so a build that fails still has the notes. A `project.cpp` that fails to load gets no update. `test --list`, `lint`, `format`, `run` and `clean` leave the file alone. - The section follows what the build loaded: a dependency that only some flags pull in (a `--target=`, a flag of the project's own) comes and goes with them. An unpinned dependency's notes follow its default branch, as its code does, so the section can change on any build. Pin a `commit` to decide when. - A library reached through two specs, pinned at one revision here and another further down, is described once, as the nearer one has it. -- When there are notes to write, unbalanced markers leave the file as it is, with a warning. A notes file that would break the section (a code block it never closes, a marker line of its own) is left out, also with a warning. Markers quoted inside a fenced code block don't count. +- When there are notes to write, unbalanced markers in the template leave `AGENTS.md` as it is, with a warning. A notes file that would break the section (a code block it never closes, a marker line of its own) is left out, also with a warning. Markers quoted inside a fenced code block don't count. `cfg.dependencyNotes` picks the dependencies: @@ -128,7 +144,7 @@ An agent working on a project reads its `AGENTS.md`, which says nothing about th |---|---| | `DependencyNotes::Transitive` (default) | everything the project builds against, all the way down: its `dependencies` and its own `#pragma crafter dependency` tooling | | `DependencyNotes::Direct` | only the projects the project's own configurations and `#pragma crafter dependency` lines name | -| `DependencyNotes::None` | none, and a section from an earlier build is removed | +| `DependencyNotes::None` | none: `AGENTS.md` holds the template's text alone | Three things never count: the project's own `AGENTS.consumer.md`; its tests' dependencies, since a plain build never looks at the tests; and a dependency's `#pragma crafter dependency` tooling, which is about how the dependency is built, not how it is used. diff --git a/implementations/Crafter.Build-Agents.cpp b/implementations/Crafter.Build-Agents.cpp index f26c8c2..bddd41b 100644 --- a/implementations/Crafter.Build-Agents.cpp +++ b/implementations/Crafter.Build-Agents.cpp @@ -22,12 +22,26 @@ namespace { // Right under the begin marker, for whoever opens the file: an edit made // inside the section is gone after the next build. constexpr std::string_view Preamble = ""; + // The first line of every AGENTS.md a build writes. It tells whoever + // opens the file, an agent asked to change it included, where an edit + // has to be made to outlive the next build, and it is how a build tells + // its own AGENTS.md from one someone wrote, which it never replaces. A + // changed word would make every AGENTS.md written before look like + // someone's own. + constexpr std::string_view GeneratedLine = ""; constexpr std::string_view NotesFileName = "AGENTS.consumer.md"; + constexpr std::string_view TemplateFileName = "AGENTS.template.md"; + constexpr std::string_view AgentsFileName = "AGENTS.md"; void Warn(std::string_view message) { Progress::Diagnostic(std::format("warning: {}", message)); } + // The line ending `text` uses, so a CRLF file does not end up mixed. + std::string_view LineEnd(std::string_view text) { + return text.contains("\r\n") ? "\r\n" : "\n"; + } + std::string ErrnoMessage() { return std::error_code(errno, std::generic_category()).message(); } @@ -249,9 +263,9 @@ namespace { } // Why `text` cannot go into the section as it is; empty when it can. A - // marker line of its own would end or restart the section once the file - // is read back, and a code block left open would hide the end marker: - // either way the next build could no longer find what this one wrote. + // marker line of its own would end or restart the section for whoever + // reads it, a template copied from the result included, and a code block + // left open would run on over the notes after it and the end marker. // Lines are numbered from `firstLine`. std::string NoteProblem(std::string_view text, std::size_t firstLine) { const MarkerScan scan = ScanMarkers(text); @@ -397,6 +411,33 @@ namespace { return notes; } + // Whether a build may replace `agentsMd`, the AGENTS.md beside a + // template: one a build wrote, or one with no text at all, which is what + // a write cut short leaves of a new file. Anything else someone wrote, + // and beside a template that is easily the case: an AGENTS.md committed + // before the template was, or one an agent kept its own notes in. + bool Replaceable(std::string_view agentsMd) { + if (agentsMd.find_first_not_of(" \t\r\n") == std::string_view::npos) return true; + std::string_view first = agentsMd.substr(0, agentsMd.find('\n')); + if (first.ends_with('\r')) first.remove_suffix(1); + return first == GeneratedLine; + } + + // An AGENTS.md with no template beside it is someone's own, and no build + // writes it. One that still holds a section from the builds that kept the + // notes in a committed AGENTS.md would go stale without a word, under a + // preamble saying every build rewrites it, so each build says so until + // the file has moved to a template or the section is gone. + void WarnStaleSection(const fs::path& agents) { + std::error_code ec; + if (!fs::is_regular_file(agents, ec)) return; + std::string text; + if (!ReadFile(agents, text).empty()) return; + const MarkerScan scan = ScanMarkers(text); + if (scan.begins.empty() && scan.ends.empty()) return; + Warn(std::format("{} has a dependency notes section, which builds no longer update; rename the file to {} and keep {} out of version control to have every build write it with current notes, or delete the section", agents.string(), TemplateFileName, AgentsFileName)); + } + // Bring `fd`, which holds `current`, to `wanted`: from the first byte the // two differ at, so the text in front of the section is never rewritten, // then cut to length and synced. Empty on success, otherwise the error. @@ -405,10 +446,9 @@ namespace { // rename swaps in another file, which drops whatever belongs to the old // one (a hard link such as a CLAUDE.md beside it, ACLs, ownership, being // read-only) and needs fsyncs to be crash safe at all. The price is that - // a reader can catch the section half written, and that a write failing - // partway, on a full disk say, leaves it so. With the section last, - // where it is appended, only the section can be damaged, and the next - // build reports its markers instead of guessing. + // a reader can catch the file half written, and that a write failing + // partway, on a full disk say, leaves it so until the next build writes + // it again. std::string WriteChanged(std::int32_t fd, std::string_view current, std::string_view wanted) { const auto diverge = std::ranges::mismatch(current, wanted); for (auto done = static_cast(diverge.in2 - wanted.begin()); done < wanted.size();) { @@ -427,24 +467,24 @@ namespace { } } -std::string Crafter::MergeDependencyNotes(std::string_view agentsMd, std::span notes) { +std::string Crafter::MergeDependencyNotes(std::string_view templateMd, std::span notes) { for (const DependencyNote& note : notes) { if (const std::string problem = NoteProblem(note.text, 1); !problem.empty()) throw std::runtime_error(std::format("the notes from {}: {}", note.name, problem)); } - const MarkerScan scan = ScanMarkers(agentsMd); + const MarkerScan scan = ScanMarkers(templateMd); const bool section = scan.begins.size() == 1 && scan.ends.size() == 1 && scan.begins[0] < scan.ends[0]; - // The file's own line ending, so a CRLF file does not end up mixed. - const std::string_view eol = agentsMd.contains("\r\n") ? "\r\n" : "\n"; + const std::string_view eol = LineEnd(templateMd); if (notes.empty()) { - // With nothing to write, leaving the file alone loses nothing, so - // anything short of a well-formed section is no concern of this one. - if (!section) return std::string(agentsMd); - std::string_view before = agentsMd.substr(0, scan.lines[scan.begins[0]].begin); - const std::string_view after = agentsMd.substr(scan.lines[scan.ends[0]].next); - // The blank line a section at the end was appended after goes with - // it. One the user moved keeps the text around it apart. + // With nothing to place, nothing can land in the wrong place, so + // anything short of a well-formed section is left as it was written. + if (!section) return std::string(templateMd); + std::string_view before = templateMd.substr(0, scan.lines[scan.begins[0]].begin); + const std::string_view after = templateMd.substr(scan.lines[scan.ends[0]].next); + // A section that ends the template takes the blank line in front of + // it, which an appended one gets. One placed elsewhere keeps the text + // around it apart. if (after.empty() && before.ends_with(std::format("{0}{0}", eol))) before.remove_suffix(eol.size()); return std::format("{}{}", before, after); } @@ -455,13 +495,12 @@ std::string Crafter::MergeDependencyNotes(std::string_view agentsMd, std::span notes = project.dependencyNotes == DependencyNotes::None ? std::vector() : CollectNotes(project, projectFile, projectDir); + std::string wanted; + try { + wanted = std::format("{}{}{}", GeneratedLine, LineEnd(templateMd), MergeDependencyNotes(templateMd, notes)); + } catch (const std::exception& e) { + Warn(std::format("{}: {}, so {} was not written", templateFile.string(), e.what(), agents.string())); + return false; + } - // Locked from the read through the write, so two builds never interleave - // their writes, and on the file itself, which also covers two projects - // that link one AGENTS.md. A read-only descriptor takes the lock as - // well, so a read-only AGENTS.md is still read without complaint. - const Descriptor reading(::open(agents.c_str(), O_RDONLY | O_CLOEXEC)); + // Created when missing, since a fresh clone has no build output, and only + // now, with the text in hand, so a template the notes cannot be placed + // in leaves no empty AGENTS.md behind. Locked from the read through the + // write, so two builds never interleave their writes, and on the file + // itself, which also covers two projects that link one AGENTS.md. A + // read-only descriptor takes the lock as well, so a read-only AGENTS.md + // is still read without complaint. + const Descriptor reading(::open(agents.c_str(), O_RDONLY | O_CREAT | O_CLOEXEC, 0666)); if (!reading.Valid()) { - Warn(std::format("{} could not be read ({}), so its dependency notes were not updated", agents.string(), ErrnoMessage())); + Warn(std::format("{} could not be opened ({}), so it was not written", agents.string(), ErrnoMessage())); return false; } while (::flock(reading.Get(), LOCK_EX) != 0 && errno == EINTR) {} std::string current; if (const std::string error = ReadAll(reading.Get(), current); !error.empty()) { - Warn(std::format("{} could not be read ({}), so its dependency notes were not updated", agents.string(), error)); + Warn(std::format("{} could not be read ({}), so it was not written", agents.string(), error)); return false; } - - std::string merged; - try { - merged = MergeDependencyNotes(current, notes); - } catch (const std::exception& e) { - Warn(std::format("{}: {}, so its dependency notes were not updated", agents.string(), e.what())); + if (current == wanted) return false; + if (!Replaceable(current)) { + Warn(std::format("{} was not written by a build, so it was not replaced by one written from {}; once the template holds what it says, delete it", agents.string(), TemplateFileName)); return false; } - if (merged == current) return false; // Only now, with something to write: a read-only AGENTS.md is refused // here, and not on every build that leaves it as it is. const Descriptor writing(::open(agents.c_str(), O_WRONLY | O_CLOEXEC)); if (!writing.Valid()) { - Warn(std::format("{} could not be opened for writing ({}), so its dependency notes were not updated", agents.string(), ErrnoMessage())); + Warn(std::format("{} could not be opened for writing ({}), so it was not written", agents.string(), ErrnoMessage())); return false; } - if (const std::string error = WriteChanged(writing.Get(), current, merged); !error.empty()) { + if (const std::string error = WriteChanged(writing.Get(), current, wanted); !error.empty()) { // Put back what was there. A full disk that refused the new bytes // usually still takes the old ones, whose space was just freed. - if (WriteChanged(writing.Get(), merged, current).empty()) { - Warn(std::format("{} could not be written ({}), so its dependency notes were not updated", agents.string(), error)); + if (WriteChanged(writing.Get(), wanted, current).empty()) { + Warn(std::format("{} could not be written ({}), so it was left as it was", agents.string(), error)); } else { - Warn(std::format("{} could not be written ({}) and may now end in part of the section; the next build reports it if so", agents.string(), error)); + Warn(std::format("{} could not be written ({}) and may now be half written; the next build writes it again", agents.string(), error)); } return false; } - // A build that edits a file the user commits says so. + // A file that appears beside project.cpp, or changes there, unasked, + // says where it came from. if (Progress::GetVerbosity() != Progress::Verbosity::Quiet) { Progress::Clear(); if (notes.empty()) { - std::println("Removed the dependency notes from {}", agents.string()); + std::println("Wrote {} from {}", agents.string(), TemplateFileName); } else { - std::println("Updated the dependency notes in {} ({} {})", agents.string(), notes.size(), notes.size() == 1 ? "dependency" : "dependencies"); + std::println("Wrote {} from {} and the notes of {} {}", agents.string(), TemplateFileName, notes.size(), notes.size() == 1 ? "dependency" : "dependencies"); } } return true; diff --git a/implementations/Crafter.Build-Clang.cpp b/implementations/Crafter.Build-Clang.cpp index e0d8329..c37c362 100644 --- a/implementations/Crafter.Build-Clang.cpp +++ b/implementations/Crafter.Build-Clang.cpp @@ -1840,11 +1840,13 @@ Clean (after the `clean` subcommand): and dependency artifact under those trees goes with it. AGENTS.md: - When an AGENTS.md sits beside the project file, a build and a test run keep a - section in it holding the AGENTS.consumer.md each dependency ships beside its - project.cpp, and write the file only when that section changes. An AGENTS.md - is never created. cfg.dependencyNotes picks the dependencies: Transitive (the - default), Direct, or None, which removes the section. + When an AGENTS.template.md sits beside the project file, a build and a test + run write AGENTS.md from it, with a section holding the AGENTS.consumer.md + each dependency ships beside its project.cpp, and only when the text changes. + Commit the template and keep AGENTS.md out of version control. An AGENTS.md + no build wrote is never replaced, and without a template AGENTS.md is left + alone. cfg.dependencyNotes picks the dependencies: Transitive (the default), + Direct, or None. Project args: Any flag not consumed above is forwarded verbatim to CrafterBuildProject as diff --git a/interfaces/Crafter.Build-Agents.cppm b/interfaces/Crafter.Build-Agents.cppm index 77e3c4e..11762fe 100644 --- a/interfaces/Crafter.Build-Agents.cppm +++ b/interfaces/Crafter.Build-Agents.cppm @@ -7,10 +7,17 @@ namespace fs = std::filesystem; // Dependency notes. A crafter-build project ships an AGENTS.consumer.md beside // its project.cpp, written for the agents of the projects that depend on it, -// and crafter-build keeps those files in each consumer's AGENTS.md. Without +// and crafter-build merges those files into each consumer's AGENTS.md. Without // that, an agent working on a consumer knows nothing about the libraries it // builds against unless someone copied the knowledge in by hand, and a copy // goes stale the moment the library changes. +// +// The consumer writes its own instructions in an AGENTS.template.md beside its +// project.cpp, and every build writes AGENTS.md from that, so AGENTS.md is +// build output and stays out of version control. Kept in a committed +// AGENTS.md instead, as they once were, the notes changed that file whenever +// a dependency changed its own, on builds that changed nothing else, and +// every diff and rebase carried changes nobody had made. export namespace Crafter { struct Configuration; @@ -23,7 +30,7 @@ export namespace Crafter { // Only the projects this one's own configurations and // `#pragma crafter dependency` lines name. Direct, - // None. A section an earlier build wrote is removed. + // None: AGENTS.md holds the template's text alone. None, }; @@ -34,38 +41,43 @@ export namespace Crafter { std::string name; // The file's content, without leading blank lines or trailing // whitespace. Lines end in LF; the merge writes the line ending the - // document uses. + // template uses. std::string text; }; - // `agentsMd` with its dependency notes section holding `notes`, in order. - // A document without a section gets one appended after a blank line; one - // with a section has it rewritten in place, wherever it was moved to; and - // empty `notes` remove it. Removing a section that ends the document takes - // the blank line in front of it too, so a document that ended in a newline - // before it gained the section gets its own bytes back. The section uses - // the document's line ending, so a CRLF file stays CRLF. + // `templateMd` with its dependency notes section holding `notes`, in + // order: AGENTS.md as it reads below the line that says a build wrote it. + // A template without a section gets one appended after a blank line; one + // with a section, which is how a template places it, has it rewritten + // where it stands, whatever it held; and empty `notes` remove it. + // Removing a section that ends the template takes the blank line in front + // of it too, so the result ends as the text before it did. The section + // uses the template's line ending, so a CRLF file stays CRLF. // // The two marker lines count only as whole lines outside fenced code // blocks, which is what lets a document quote them. Throws, naming the // line, when there are notes to write and the markers are unbalanced, or - // when the section would have to be appended to a document that ends - // inside a code block: guessing which part of someone's file is generated - // would overwrite their text, and missing the section would add another on - // every build. With no notes it never throws, since leaving the document - // alone loses nothing. Also throws for a note that would break the section - // when it is read back: one with a marker line of its own, or a code block - // it never closes. - std::string MergeDependencyNotes(std::string_view agentsMd, std::span notes); + // when the section would have to be appended to a template that ends + // inside a code block: a lone or doubled marker is a slip in the + // template, and any guess at what it meant would put the notes, or cut + // the text, where its author did not, while notes appended inside a code + // block would read as code. With no notes it never throws, since nothing + // has to be placed. Also throws for a note that would break the section + // for whoever reads it: one with a marker line of its own, or a code + // block it never closes. + std::string MergeDependencyNotes(std::string_view templateMd, std::span notes); - // Refresh the dependency notes section of the AGENTS.md beside - // `projectFile` from the AGENTS.consumer.md of every dependency - // project.dependencyNotes selects. `project` is the Configuration that - // file returned, after LoadProject: loading is what found the - // dependencies. Does nothing when there is no AGENTS.md, which is never - // created. Writes only when the text changes, in place, so hard links, - // ownership and a read-only AGENTS.md are respected; returns whether it - // wrote. A read or write that fails, a section that cannot be placed and + // Write the AGENTS.md beside `projectFile` from the AGENTS.template.md + // beside it, with the AGENTS.consumer.md of every dependency + // project.dependencyNotes selects merged in. `project` is the + // Configuration that file returned, after LoadProject: loading is what + // found the dependencies. Creates AGENTS.md when it is missing, and + // replaces only one a build wrote: an AGENTS.md someone wrote is left + // alone, with a warning. Without a template it writes nothing, and only + // warns about a section an earlier crafter-build left in AGENTS.md. + // Writes only when the text changes, in place, so hard links, ownership + // and a read-only AGENTS.md are respected; returns whether it wrote. A + // read or write that fails, a template the notes cannot be placed in and // a dependency's unusable notes are warnings: the notes never fail a // build, and nothing is reported when nothing would have been written. bool UpdateAgentsMd(const Configuration& project, const fs::path& projectFile); diff --git a/interfaces/Crafter.Build-Clang.cppm b/interfaces/Crafter.Build-Clang.cppm index 0797e88..b4bbc68 100644 --- a/interfaces/Crafter.Build-Clang.cppm +++ b/interfaces/Crafter.Build-Clang.cppm @@ -529,8 +529,9 @@ export namespace Crafter { // Not part of VariantId: a script changes nothing that is compiled. std::vector