diff --git a/doc/agile/versions/v0/sprint_01/surface_the_open_blame_reports/story.org b/doc/agile/versions/v0/sprint_01/surface_the_open_blame_reports/story.org index 452b95ce..91ce66ac 100644 --- a/doc/agile/versions/v0/sprint_01/surface_the_open_blame_reports/story.org +++ b/doc/agile/versions/v0/sprint_01/surface_the_open_blame_reports/story.org @@ -43,8 +43,8 @@ would have caught the breaks before they reached main. | Parent sprint | [[id:850C1BC8-FE02-4EE0-A5D6-6085DC34CE27][Sprint 1]] | | Now | =compass blames=, =check= and the git hooks show the open reports, the current environment's first and marked; the folder is analysed and three checks are proposed. The service query and the environment naming remain. | | Waiting on | Nothing. | -| Next | Serve the open reports over HTTP and MCP; unify the environment names. | -| Last touched | 2026-12-04 | +| Next | Serve the open reports over HTTP and MCP. | +| Last touched | 2026-12-05 | * Acceptance @@ -69,7 +69,7 @@ would have caught the breaks before they reached main. | [[id:686B61EC-0AD8-4AEB-ADA7-1F9EAB504AC6][List the open blame reports]] | DONE | 2026-11-29 | 2026-12-04 | A compass blames command that lists the open reports of the types a project names, the current environment's first and marked, and the same short summary from check and the git hooks. | | [[id:62A4D200-9C15-4D37-A47C-CB593BD51D64][Analyse the blame reports during the sprint]] | DONE | | 2026-12-04 | Read the blame folder at points during the sprint, class the breaks it records, and find the proactive checks that would have caught them before they reached main. | | [[id:BFCA17AF-5BDD-4B4B-8B07-5325F191C472][Serve the open blame reports]] | BACKLOG | | | Answer the open reports over the document service, as an HTTP read route and an MCP tool generated from the route models, so agents that read compass through it see them too. | -| [[id:8DFD6766-E279-4496-AAE6-A9CD7AC6457A][Unify environment names]] | BACKLOG | | | Give an environment one name everywhere compass and its projects state one, by a migration that renames corpus.environment.name and the environment columns of activity in one transaction, not by naming it differently on the next sync. | +| [[id:8DFD6766-E279-4496-AAE6-A9CD7AC6457A][Unify environment names]] | DONE | 2026-12-05 | 2026-12-05 | Give an environment one name everywhere compass and its projects state one, by a migration that renames corpus.environment.name and the environment columns of activity in one transaction, not by naming it differently on the next sync. | * Decisions diff --git a/doc/agile/versions/v0/sprint_01/surface_the_open_blame_reports/task_unify_environment_names.org b/doc/agile/versions/v0/sprint_01/surface_the_open_blame_reports/task_unify_environment_names.org index 78f7c06e..46f8c531 100644 --- a/doc/agile/versions/v0/sprint_01/surface_the_open_blame_reports/task_unify_environment_names.org +++ b/doc/agile/versions/v0/sprint_01/surface_the_open_blame_reports/task_unify_environment_names.org @@ -7,8 +7,8 @@ #+level: s1 #+filetags: :blame:db:environment:surface_the_open_blame_reports:sprint_01:v0: #+owner: marco -#+branch: -#+pr: +#+branch: agile/unify-on-the-short-name +#+pr: 279 #+blocked_on: #+blocked_since: #+created: 2026-11-29 @@ -43,22 +43,42 @@ whether =#+owner_prefix:= then goes. | Field | Value | |--------------+----------------------------------------| -| State | BACKLOG | +| State | DONE | | Parent story | [[id:1874A6CC-3640-4A89-B07F-C9A5F6AAD5F2][Surface the open blame reports]] | -| Now | Not yet started. | +| Now | Done: the short name wins, and the rename travels with the checkout on its next sync. | | Waiting on | Nothing. | -| Next | Begin implementation. | -| Last touched | 2026-11-29 | +| Next | Nothing. | +| Last touched | 2026-12-05 | * Acceptance -- +- An environment is named by its checkout's directory less the project's + =#+owner_prefix:=; a directory without the prefix keeps its whole name, and + a name given with =--environment= or =#+environment:= is taken as given. +- The name is computed the same way by sync, the git hooks, test export, + serve and =blames=. +- A checkout synced under its old, prefixed name is renamed to the short one + on its next sync, in one transaction, with the =activity.campaign=, + =activity.action=, =test.scenario= and =agent.agent_session= rows that + name the old environment moved with it. +- An open report is the environment's when its owner names the environment + whole; the prefix no longer bridges the two. * Plan -(Implementation strategy. Written when work starts; key decisions -are distilled into the parent story's =* Decisions= at close, but the -plan itself stays — it is the historical record of what we did.) +1. Name the environment in =config::environment_name= as the checkout's + directory less the project's prefix, which every caller already computes + the name with; the callers that did not read the configuration file now + pass its prefix. +2. Rename in the database rather than in a migration: the schema does not + change, and a migration cannot know a project's prefix. =write_environment= + finds the row with the same =root= whose name is the new name with a + prefix, and moves the row and the four text =environment= columns to the + new name in the sync's transaction. A different name chosen for the same + checkout is left alone. +3. Drop the prefix fallback from =blames=' ownership match. +4. Test the naming, the rename across the four tables, and that a stale + long name owns nothing. * Measurements @@ -72,10 +92,23 @@ task and name that file in the Command column. | What | Value | Command | Commit | |------+-------+---------+--------| -| | | | | +| =compass.cli.tests= | 2758 assertions in 267 cases | =compass.cli.tests= | this branch | +| =compass.db.tests= | 1307 assertions in 175 cases | =compass.db.tests= | this branch | +| Naming | the directory less =#+owner_prefix:= | =config::environment_name= | this branch | * Notes +- *The short name wins.* The product owner chose it on 2026-12-05: the + environment is =eager_maxwell=, not =ores_dev_eager_maxwell=, so it matches + the name ORE Studio's =Environment= trailer and its blame reports use. +- *A rename at sync, not a migration.* The schema does not change, and the + migration SQL cannot read a project's =#+owner_prefix:=. The sync knows both + the checkout's root and the name it computed, so it carries the record + forward in its own transaction. A migration would have needed the prefix + baked into SQL that is the same for every project. +- *The prefix stays, with one meaning.* It is how a checkout's directory maps + to its environment's name; it no longer bridges owner fields in reports. + * Test Scenarios Manual QA scenarios (scaffolded via =compass add test_scenario=) @@ -100,3 +133,12 @@ via its "Verifies task" field. | | | | | | * Result + +The environment's name is the checkout directory less the project's prefix. +Sync, the hooks, test export, serve and =blames= all compute it the same way, +and =serve= renames a checkout's environment to it on its next start. The +rename moves =corpus.environment= and the four text =environment= columns in +the sync's transaction; the test seeds one row in each and asserts the single +renamed environment. Ownership in =blames= is now a whole-name match, and the +tests cover the short name, the prefix edge cases and that a stale long name +owns nothing. diff --git a/projects/compass.cli/include/compass.cli/config/local_settings.hpp b/projects/compass.cli/include/compass.cli/config/local_settings.hpp index 9d1214bb..74753ae3 100644 --- a/projects/compass.cli/include/compass.cli/config/local_settings.hpp +++ b/projects/compass.cli/include/compass.cli/config/local_settings.hpp @@ -88,7 +88,7 @@ void write_local_setting(const std::filesystem::path& file, */ [[nodiscard]] std::string environment_name( const std::filesystem::path& checkout, const local_settings& s, - std::string_view given); + std::string_view given, std::string_view owner_prefix = {}); } diff --git a/projects/compass.cli/include/compass.cli/config/repository_file.hpp b/projects/compass.cli/include/compass.cli/config/repository_file.hpp index 5b3069f0..d2ddf1e2 100644 --- a/projects/compass.cli/include/compass.cli/config/repository_file.hpp +++ b/projects/compass.cli/include/compass.cli/config/repository_file.hpp @@ -68,10 +68,10 @@ inline constexpr std::string_view repository_file_name = ".compass.org"; * with other than four cells, no type or owner, a type named twice or * another surface is an error. * - * #+owner_prefix: is the prefix an environment's name carries that the - * owner fields of the reports leave out: with ores_dev_, an owner - * eager_maxwell is the environment ores_dev_eager_maxwell. Only the open - * reports read it; it renames no environment. + * #+owner_prefix: is the prefix a checkout's directory carries that its + * environment's name leaves out: with ores_dev_, the checkout + * ores_dev_eager_maxwell is the environment eager_maxwell. The open + * reports' owner fields name environments whole. */ [[nodiscard]] std::expected parse_repository_file( std::string_view text, std::string_view label); diff --git a/projects/compass.cli/include/compass.cli/ops/open_reports.hpp b/projects/compass.cli/include/compass.cli/ops/open_reports.hpp index 74e0a077..a9eca1ff 100644 --- a/projects/compass.cli/include/compass.cli/ops/open_reports.hpp +++ b/projects/compass.cli/include/compass.cli/ops/open_reports.hpp @@ -66,7 +66,7 @@ namespace compass::cli::ops { * and it is not evidence. It is open when the State row of its * Status * table holds one of the kind's open states. Its owners are the names in * the row of the kind's owner field, and it is owned when one of them is - * @p environment, whole, or is @p environment less @p owner_prefix: with + * @p environment, whole: * the prefix ores_dev_, the owner eager_maxwell is the environment * ores_dev_eager_maxwell, and maxwell is neither. Without the kind's open * states, the open states are those of every #+todo: line of the page, @@ -77,7 +77,7 @@ namespace compass::cli::ops { [[nodiscard]] types::open_reports select_open_reports( std::span kinds, const codec::types::corpus& corpus, std::string_view environment, - std::string_view owner_prefix, std::chrono::year_month_day today); + std::chrono::year_month_day today); } diff --git a/projects/compass.cli/src/app/application.cpp b/projects/compass.cli/src/app/application.cpp index 431d4855..da3b8e7b 100644 --- a/projects/compass.cli/src/app/application.cpp +++ b/projects/compass.cli/src/app/application.cpp @@ -254,7 +254,8 @@ std::expected> options_of( c.warnings.push_back(local.error() + "; the environment is " "named by its directory"); environment = config::environment_name(i.checkout, - local ? *local : config::local_settings{}, i.environment); + local ? *local : config::local_settings{}, i.environment, + file->owner_prefix); } return types::options{.which = i.which, .checkout = i.checkout, diff --git a/projects/compass.cli/src/app/database.cpp b/projects/compass.cli/src/app/database.cpp index 402fde9c..d1bac2e3 100644 --- a/projects/compass.cli/src/app/database.cpp +++ b/projects/compass.cli/src/app/database.cpp @@ -30,6 +30,7 @@ #include "compass.cli/app/sync.hpp" #include "compass.cli/app/test_export.hpp" #include "compass.cli/config/local_settings.hpp" +#include "compass.cli/config/repository_file.hpp" #include "compass.cli/io/listing.hpp" #include "compass.db/io/connection.hpp" #include "compass.db/io/corpus.hpp" @@ -333,6 +334,15 @@ int run_db(const types::invocation& i, std::ostream& out, return exit_failure; } +/** + * @brief The prefix the project's checkout directories carry, which its + * environments' names leave out. + */ +std::string owner_prefix_of(const fs::path& checkout) { + const auto layer = config::read_repository_file(checkout); + return layer ? layer->owner_prefix : std::string{}; +} + int run_sync(const types::invocation& i, std::ostream& out, std::ostream& err) { try { @@ -343,7 +353,7 @@ int run_sync(const types::invocation& i, std::ostream& out, r.checkout = i.checkout; r.threads = i.threads; r.environment = config::environment_name(i.checkout, s, - i.environment); + i.environment, owner_prefix_of(i.checkout)); if (!i.revision.empty()) r.commit = io::resolve_commit(i.checkout, i.revision); const auto report = sync_environment(c, r); @@ -362,7 +372,7 @@ int run_test(const types::invocation& i, std::ostream& out, auto c = connect(s); db::io::require_current(c); const auto environment = config::environment_name(i.checkout, s, - i.environment); + i.environment, owner_prefix_of(i.checkout)); const auto r = export_campaigns(c, i.checkout, environment, i.test.story); if (i.test.story && r.campaigns == 0 && r.skipped.empty()) { @@ -400,7 +410,8 @@ void sync_after_run(const fs::path& checkout, unsigned threads, bool timings, sync_request r; r.checkout = checkout; r.threads = threads; - r.environment = config::environment_name(checkout, s, {}); + r.environment = config::environment_name(checkout, s, {}, + owner_prefix_of(checkout)); const auto report = sync_environment(c, r); if (timings) write_sync(err, report, true); @@ -421,7 +432,8 @@ void sync_after_edit(const fs::path& checkout, std::string_view path, auto c = connect(s); db::io::require_current(c); const auto report = sync_file(c, checkout, - config::environment_name(checkout, s, {}), path); + config::environment_name(checkout, s, {}, + owner_prefix_of(checkout)), path); if (timings) write_sync(err, report, false); else diff --git a/projects/compass.cli/src/app/pipeline.cpp b/projects/compass.cli/src/app/pipeline.cpp index c94e6254..e02fef5d 100644 --- a/projects/compass.cli/src/app/pipeline.cpp +++ b/projects/compass.cli/src/app/pipeline.cpp @@ -1844,13 +1844,13 @@ types::check_result check(const types::options& o) { r.blame = blame(o, r.findings, document_ids_of(p), p.run.record); if (!o.commit) r.open = ops::select_open_reports(o.report_kinds, p.corpus, - o.environment, o.owner_prefix, o.today); + o.environment, o.today); return r; } types::open_reports open_reports(const types::options& o) { if (o.report_kinds.empty()) - return ops::select_open_reports({}, {}, o.environment, {}, o.today); + return ops::select_open_reports({}, {}, o.environment, o.today); listing l; list(l, o.checkout); auto files = open_files(o.checkout, @@ -1860,7 +1860,7 @@ types::open_reports open_reports(const types::options& o) { load(files, o.threads); return ops::select_open_reports(o.report_kinds, corpus_of(files, org::types::type_registry{}), o.environment, - o.owner_prefix, o.today); + o.today); } types::check_result blame_one(const types::options& o, @@ -2357,7 +2357,7 @@ types::ratchet_result ratchet(const types::options& o, if (!r.current.run.failed()) r.provenance = provenance_of(p, o, r.merge_base, head); r.current.open = ops::select_open_reports(o.report_kinds, p.corpus, - o.environment, o.owner_prefix, o.today); + o.environment, o.today); auto at_base = o; at_base.commit = r.merge_base; // A codec HEAD's configuration enables may need files the merge base diff --git a/projects/compass.cli/src/app/serve.cpp b/projects/compass.cli/src/app/serve.cpp index 549fb682..b7448ff6 100644 --- a/projects/compass.cli/src/app/serve.cpp +++ b/projects/compass.cli/src/app/serve.cpp @@ -77,7 +77,9 @@ http::app::environment_sync environment_sync_of(const fs::path& checkout, sync_request r; r.checkout = checkout; r.threads = threads; - r.environment = config::environment_name(checkout, s, {}); + const auto layer = config::read_repository_file(checkout); + r.environment = config::environment_name(checkout, s, {}, + layer ? layer->owner_prefix : std::string{}); auto name = r.environment; return {std::move(name), [r = std::move(r), database = s.database] { db::io::connection c(database); diff --git a/projects/compass.cli/src/config/local_settings.cpp b/projects/compass.cli/src/config/local_settings.cpp index 80ef00d8..b47dcaa6 100644 --- a/projects/compass.cli/src/config/local_settings.cpp +++ b/projects/compass.cli/src/config/local_settings.cpp @@ -150,7 +150,8 @@ void write_local_setting(const fs::path& file, std::string_view key, } std::string environment_name(const fs::path& checkout, - const local_settings& s, std::string_view given) { + const local_settings& s, std::string_view given, + std::string_view owner_prefix) { if (!given.empty()) return std::string(given); if (!s.environment.empty()) @@ -158,7 +159,13 @@ std::string environment_name(const fs::path& checkout, auto name = checkout.filename(); if (name.empty()) name = checkout.parent_path().filename(); - return name.string(); + auto text = name.string(); + // The directory a worktree lives in carries the project's prefix; the + // environment's name is what follows it. + if (!owner_prefix.empty() && text.size() > owner_prefix.size() && + text.starts_with(owner_prefix)) + text.erase(0, owner_prefix.size()); + return text; } } diff --git a/projects/compass.cli/src/ops/open_reports.cpp b/projects/compass.cli/src/ops/open_reports.cpp index 1f00d702..e17b19e8 100644 --- a/projects/compass.cli/src/ops/open_reports.cpp +++ b/projects/compass.cli/src/ops/open_reports.cpp @@ -178,7 +178,7 @@ std::vector open_states(std::string_view source, types::open_reports select_open_reports( std::span kinds, const codec::types::corpus& corpus, std::string_view environment, - std::string_view owner_prefix, std::chrono::year_month_day today) { + std::chrono::year_month_day today) { types::open_reports out; out.environment = std::string(environment); out.configured = !kinds.empty(); @@ -229,12 +229,7 @@ types::open_reports select_open_reports( "not an environment's name; fix: write each owner bare, " "separated by commas"); r.owned = std::ranges::any_of(r.owners, - [&](const std::string& o) { - return o == environment || (!owner_prefix.empty() && - environment.size() == owner_prefix.size() + o.size() && - environment.starts_with(owner_prefix) && - environment.ends_with(o)); - }); + [&](const std::string& o) { return o == environment; }); if (const auto created = codec::ops::parse_date( keyword(source, tables, "created"))) r.age = static_cast((std::chrono::sys_days(today) - diff --git a/projects/compass.cli/tests/app_blames_tests.cpp b/projects/compass.cli/tests/app_blames_tests.cpp index 3dbb414d..69f3d8d7 100644 --- a/projects/compass.cli/tests/app_blames_tests.cpp +++ b/projects/compass.cli/tests/app_blames_tests.cpp @@ -223,8 +223,8 @@ TEST_CASE("blames says none, not 0, when this environment owns no open " "the owners named answer their own\n")); } -TEST_CASE("the owner prefix gives a checkout the reports of its name less " - "the prefix, and names the environment as sync does", +TEST_CASE("the owner prefix names the environment, and a report is " + "its own when the owner names it whole", "[app][blames]") { const blamed_repo b("#+owner_prefix: check\n" + std::string(codecs) + std::string(open_reports)); @@ -237,7 +237,7 @@ TEST_CASE("the owner prefix gives a checkout the reports of its name less " CHECK(r.status == app::exit_success); CHECK(r.out == - "open reports: 1 of the 3 open is owned by checkout, this " + "open reports: 1 of the 3 open is owned by out, this " "environment:\n" "* STARTED 1d doc/blame/out/blame_report.org: Blame: out of space\n" "open reports: next: read each one marked *, answer it, and close " diff --git a/projects/compass.cli/tests/config_precedence_tests.cpp b/projects/compass.cli/tests/config_precedence_tests.cpp index 0909e9bd..f41288a5 100644 --- a/projects/compass.cli/tests/config_precedence_tests.cpp +++ b/projects/compass.cli/tests/config_precedence_tests.cpp @@ -17,9 +17,11 @@ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * */ +#include "compass.cli/config/local_settings.hpp" #include "compass.cli/config/precedence.hpp" #include +#include #include #include #include @@ -130,3 +132,23 @@ TEST_CASE("a setting that matches no codec or option is a problem naming " "'perhaps'; fix: set it to true or false"); CHECK(c.codecs[0].enabled.value == "true"); } + +TEST_CASE("an environment is named by its directory less the project's " + "prefix", "[config][environment]") { + const auto checkout = std::filesystem::path("/src/ores_dev_eager_maxwell"); + const config::local_settings none; + CHECK(config::environment_name(checkout, none, {}, "ores_dev_") == + "eager_maxwell"); + // A directory without the prefix keeps its whole name, as does a name + // given on the command line or in the settings. + CHECK(config::environment_name(checkout, none, {}, "") == + "ores_dev_eager_maxwell"); + CHECK(config::environment_name(checkout, none, {}, "ores_") == + "dev_eager_maxwell"); + CHECK(config::environment_name(checkout, none, "given", "ores_dev_") == + "given"); + config::local_settings stated; + stated.environment = "from_settings"; + CHECK(config::environment_name(checkout, stated, {}, "ores_dev_") == + "from_settings"); +} diff --git a/projects/compass.cli/tests/ops_open_reports_tests.cpp b/projects/compass.cli/tests/ops_open_reports_tests.cpp index ddd94d0a..1a7ea60c 100644 --- a/projects/compass.cli/tests/ops_open_reports_tests.cpp +++ b/projects/compass.cli/tests/ops_open_reports_tests.cpp @@ -159,7 +159,7 @@ TEST_CASE("select_open_reports lists the open reports of a kind, the " const std::vector kinds{blame_kind()}; const auto r = ops::select_open_reports(kinds, d.corpus, - "eager_maxwell", {}, today); + "eager_maxwell", today); CHECK(r.configured); CHECK(r.environment == "eager_maxwell"); @@ -177,7 +177,7 @@ TEST_CASE("select_open_reports lists the open reports of a kind, the " CHECK(first.age == 9); const auto other = ops::select_open_reports(kinds, d.corpus, - "solid_dirac", {}, today); + "solid_dirac", today); CHECK(listed(other) == strings{ "* STARTED blame/e/blame_report.org", "* STARTED blame/a/blame_report.org", @@ -195,7 +195,7 @@ TEST_CASE("select_open_reports takes the kind's open states over the " kind.open = {"BLOCKED"}; const auto r = ops::select_open_reports(std::vector{kind}, d.corpus, - "x_y", {}, today); + "x_y", today); CHECK(listed(r) == strings{"* BLOCKED b.org"}); } @@ -215,7 +215,7 @@ TEST_CASE("select_open_reports names the reports it cannot judge as " "| State | STARTED |\n"); const auto r = ops::select_open_reports(std::vector{blame_kind()}, - d.corpus, "x_y", {}, today); + d.corpus, "x_y", today); REQUIRE(r.problems.size() == 3); CHECK(r.problems[0].starts_with("no_state.org: * Status has no State")); @@ -234,8 +234,7 @@ TEST_CASE("select_open_reports looks for nothing when no kind is named", documents d; d.add("a.org", page("blame_report", "Blame: a", "STARTED", "`x_y`")); - const auto r = ops::select_open_reports({}, d.corpus, "x_y", {}, - today); + const auto r = ops::select_open_reports({}, d.corpus, "x_y", today); CHECK_FALSE(r.configured); CHECK(r.reports.empty()); @@ -248,7 +247,7 @@ TEST_CASE("select_open_reports names each owner a blank or a mark spoils " "`solid dirac`, [[id:1][eager maxwell]], x_y")); const auto r = ops::select_open_reports(std::vector{blame_kind()}, - d.corpus, "x_y", {}, today); + d.corpus, "x_y", today); CHECK(r.problems == strings{ "a.org: the owner '`solid' is not an environment's name; fix: write " @@ -260,8 +259,8 @@ TEST_CASE("select_open_reports names each owner a blank or a mark spoils " CHECK(listed(r) == strings{"* STARTED a.org"}); } -TEST_CASE("select_open_reports gives a report to the environment whose " - "name is the owner after the owner prefix", "[ops_open_reports]") { +TEST_CASE("select_open_reports gives a report to the environment its owner " + "names, whole", "[ops_open_reports]") { documents d; d.add("a.org", page("blame_report", "Blame: a", "STARTED", "`eager_maxwell`")); @@ -271,20 +270,17 @@ TEST_CASE("select_open_reports gives a report to the environment whose " "`maxwell`")); const std::vector kinds{blame_kind()}; - const auto prefixed = ops::select_open_reports(kinds, d.corpus, - "ores_dev_eager_maxwell", "ores_dev_", today); - CHECK(prefixed.environment == "ores_dev_eager_maxwell"); - CHECK(listed(prefixed) == strings{"* STARTED a.org", "* STARTED b.org", + const auto short_name = ops::select_open_reports(kinds, d.corpus, + "eager_maxwell", today); + CHECK(short_name.environment == "eager_maxwell"); + CHECK(listed(short_name) == strings{"* STARTED a.org", " STARTED b.org", " STARTED c.org"}); - const auto bare = ops::select_open_reports(kinds, d.corpus, - "ores_dev_eager_maxwell", {}, today); - CHECK(listed(bare) == strings{"* STARTED b.org", " STARTED a.org", - " STARTED c.org"}); - - const auto other = ops::select_open_reports(kinds, d.corpus, - "eager_maxwell", "ores_dev_", today); - CHECK(listed(other) == strings{"* STARTED a.org", " STARTED b.org", + // A checkout still carrying the prefix in its name owns nothing: the + // environment's name is the short one its reports use. + const auto long_name = ops::select_open_reports(kinds, d.corpus, + "ores_dev_eager_maxwell", today); + CHECK(listed(long_name) == strings{"* STARTED b.org", " STARTED a.org", " STARTED c.org"}); } @@ -303,7 +299,7 @@ TEST_CASE("select_open_reports merges the open states of every #+todo: " d.add("d.org", two_lines("DONE")); const auto r = ops::select_open_reports(std::vector{blame_kind()}, - d.corpus, "x_y", {}, today); + d.corpus, "x_y", today); CHECK(r.problems.empty()); CHECK(listed(r) == strings{"* STARTED a.org", "* WAITING b.org"}); diff --git a/projects/compass.db/src/io/corpus.cpp b/projects/compass.db/src/io/corpus.cpp index 89fec03d..7356207f 100644 --- a/projects/compass.db/src/io/corpus.cpp +++ b/projects/compass.db/src/io/corpus.cpp @@ -287,6 +287,35 @@ write_result write_environment(connection& c, const types::environment& e, const auto text_search = text_search_of(c); transaction t(c); const changes_before changes(c); + // A project may stop naming a checkout by its directory and name it by + // the short name its reports use, which is the directory less its + // prefix. The old row and the records named after it move to the new + // name in this one transaction. A different name chosen on purpose for + // the same checkout is left alone. + if (e.root && !e.root->empty()) { + const auto previous = c.exec("SELECT name FROM corpus.environment " + "WHERE root = $1 AND name <> $2 ORDER BY length(name) DESC", + {*e.root, e.name}); + const auto taken = c.exec( + "SELECT 1 FROM corpus.environment WHERE name = $1", {e.name}); + std::string old_name; + for (std::size_t row = 0; row < previous.rows(); ++row) { + const std::string candidate(previous.value(row, 0)); + if (candidate.size() > e.name.size() && + candidate.ends_with(e.name)) { + old_name = candidate; + break; + } + } + if (!old_name.empty() && taken.rows() == 0) { + for (const auto table : {"activity.campaign", "activity.action", + "test.scenario", "agent.agent_session"}) + c.exec("UPDATE " + std::string(table) + " SET environment = " + "$2 WHERE environment = $1", {old_name, e.name}); + c.exec("UPDATE corpus.environment SET name = $2 WHERE name = $1", + {old_name, e.name}); + } + } const auto id = std::string(c.exec(R"sql( INSERT INTO corpus.environment (name, kind, root, head) VALUES ($1, $2, $3, $4) ON CONFLICT (name) DO UPDATE SET kind = EXCLUDED.kind, root = EXCLUDED.root diff --git a/projects/compass.db/tests/io_corpus_tests.cpp b/projects/compass.db/tests/io_corpus_tests.cpp index 56bc9ae9..3e99521c 100644 --- a/projects/compass.db/tests/io_corpus_tests.cpp +++ b/projects/compass.db/tests/io_corpus_tests.cpp @@ -331,3 +331,36 @@ TEST_CASE("a write analyses the tables it changed by more than a tenth", CHECK(analyses("corpus.document") == 3); CHECK(analyses("corpus.section") == 3); } + +TEST_CASE("a checkout renamed to its short name keeps its record", + "[io][corpus]") { + auto c = fresh(); + documents docs; + const std::vector batch{docs.add(page("Alpha", "Ranking by sections."))}; + const std::vector placed{{"a.org", batch[0].hash}}; + io::write_environment(c, worktree("ores_dev_eager_maxwell"), batch, + placed); + + const auto hash = compass::store::ops::to_hex(batch[0].hash); + c.exec("INSERT INTO activity.campaign (environment, story_id) VALUES " + "($1, 'S1')", {"ores_dev_eager_maxwell"}); + c.exec("INSERT INTO activity.action (operation, subject, environment) " + "VALUES ('sync_environment', 's', $1)", {"ores_dev_eager_maxwell"}); + c.exec("INSERT INTO test.scenario (hash, environment, definition) VALUES " + "(decode($1, 'hex'), $2, '\\x01')", + {hash, "ores_dev_eager_maxwell"}); + c.exec("INSERT INTO agent.agent_session (environment, subject, view_name) " + "VALUES ($1, 's', 'team')", {"ores_dev_eager_maxwell"}); + + auto short_name = worktree("eager_maxwell"); + short_name.root = "/work/ores_dev_eager_maxwell"; + io::write_environment(c, short_name, {}, placed, {}); + + CHECK(count(c, "corpus.environment") == 1); + CHECK(c.exec("SELECT name FROM corpus.environment").value(0, 0) == + "eager_maxwell"); + for (const auto table : {"activity.campaign", "activity.action", + "test.scenario", "agent.agent_session"}) + CHECK(c.exec("SELECT count(*) FROM " + std::string(table) + + " WHERE environment = 'eager_maxwell'").value(0, 0) == "1"); +}