Runtime: reset stale Core baseline on upgrade

This commit is contained in:
faumaray committed 2026-07-26 18:11:28 +03:00
1 parent 978930f4d2
commit 2fba7d2dea
5 files changed
+310 -31

No files matched your search

@@ -5,10 +5,27 @@
#include <filesystem>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
namespace Echoion::Standalone
{
struct ManagedCoreStartupSelection {
std::filesystem::path corePath;
std::optional<std::filesystem::path> archivedStore;
bool runtimeBaselineChanged = false;
};
// Installed runtime releases own their bundled Core baseline. A runtime
// version change archives the previous managed store; Core-only updates
// remain active across ordinary restarts of the same runtime.
[[nodiscard]] Api::Result<ManagedCoreStartupSelection>
selectManagedCoreForStartup(
const std::filesystem::path& stateDirectory,
const std::filesystem::path& bundledCore,
std::optional<std::string_view> installedRuntimeVersion);
// Roll back any uncommitted managed-plugin activation before Core loads and
// discovers plugin `current` links. This must run before PluginHost
// startup.
+201
View File
@@ -3162,6 +3162,207 @@ namespace Echoion::Standalone
return impl_->handle(method, requestJson);
}
Api::Result<ManagedCoreStartupSelection>
selectManagedCoreForStartup(const std::filesystem::path& stateDirectory,
const std::filesystem::path& bundledCore,
const std::optional<std::string_view>
installedRuntimeVersion)
{
const auto components = stateDirectory / "updates" / "components";
const auto storeRoot = components / "core";
const auto markerPath = components / "core-runtime-version";
const auto selectCurrent =
[&]() -> Api::Result<ManagedCoreStartupSelection> {
Void::Update::VersionStore store(storeRoot);
if (auto initialized = store.initialize(); !initialized) {
return Api::fail(Api::ErrorCode::UnknownError,
initialized.error().message());
}
if (auto reconciled = store.reconcile(); !reconciled) {
return Api::fail(Api::ErrorCode::UnknownError,
reconciled.error().message());
}
auto current = store.currentVersion();
if (!current) {
return Api::fail(Api::ErrorCode::UnknownError,
current.error().message());
}
if (!*current) {
return ManagedCoreStartupSelection{
.corePath = bundledCore,
.archivedStore = std::nullopt,
.runtimeBaselineChanged = false};
}
const auto managed =
store.versionPath(**current) / bundledCore.filename();
std::error_code error;
const auto status = std::filesystem::symlink_status(managed, error);
if (!error && std::filesystem::is_regular_file(status) &&
!std::filesystem::is_symlink(status)) {
return ManagedCoreStartupSelection{
.corePath = managed,
.archivedStore = std::nullopt,
.runtimeBaselineChanged = false};
}
if (error != std::errc::no_such_file_or_directory && error) {
return Api::fail(
Api::ErrorCode::PermissionDenied,
std::format("cannot inspect managed Core '{}': {}",
managed.string(), error.message()));
}
return ManagedCoreStartupSelection{.corePath = bundledCore,
.archivedStore = std::nullopt,
.runtimeBaselineChanged = false};
};
// Development trees do not have a release manifest and retain the
// historical behavior: the current managed Core, if any, wins.
if (!installedRuntimeVersion) {
return selectCurrent();
}
if (installedRuntimeVersion->empty() ||
!safeIdentifier(*installedRuntimeVersion)) {
return Api::fail(Api::ErrorCode::InvalidArgument,
"installed runtime version is not a safe "
"identifier");
}
std::error_code error;
std::filesystem::create_directories(components, error);
if (error) {
return Api::fail(
Api::ErrorCode::PermissionDenied,
std::format("cannot prepare managed Core directory '{}': {}",
components.string(), error.message()));
}
std::optional<std::string> recordedRuntime;
const auto markerStatus =
std::filesystem::symlink_status(markerPath, error);
if (!error &&
markerStatus.type() != std::filesystem::file_type::not_found) {
if (!std::filesystem::is_regular_file(markerStatus) ||
std::filesystem::is_symlink(markerStatus)) {
return Api::fail(
Api::ErrorCode::PermissionDenied,
"managed Core runtime marker must be a regular file");
}
auto marker = Void::FileSystem::read_file(markerPath);
if (!marker) {
return Api::fail(Api::ErrorCode::PermissionDenied,
marker.error().message());
}
while (!marker->empty() &&
(marker->back() == '\n' || marker->back() == '\r')) {
marker->pop_back();
}
if (!marker->empty() && !safeIdentifier(*marker)) {
return Api::fail(Api::ErrorCode::ParseError,
"managed Core runtime marker is invalid");
}
recordedRuntime = std::move(*marker);
} else if (error != std::errc::no_such_file_or_directory && error) {
return Api::fail(
Api::ErrorCode::PermissionDenied,
std::format("cannot inspect managed Core runtime marker: {}",
error.message()));
}
if (recordedRuntime && *recordedRuntime == *installedRuntimeVersion) {
return selectCurrent();
}
std::optional<std::filesystem::path> archivedStore;
error.clear();
const auto storeStatus =
std::filesystem::symlink_status(storeRoot, error);
if (!error &&
storeStatus.type() != std::filesystem::file_type::not_found) {
if (!std::filesystem::is_directory(storeStatus) ||
std::filesystem::is_symlink(storeStatus)) {
return Api::fail(Api::ErrorCode::PermissionDenied,
"managed Core store must be a real directory");
}
const auto archiveRoot = components / "core-archives";
std::filesystem::create_directories(archiveRoot, error);
if (error) {
return Api::fail(
Api::ErrorCode::PermissionDenied,
std::format("cannot prepare managed Core archive: {}",
error.message()));
}
const auto archiveStatus =
std::filesystem::symlink_status(archiveRoot, error);
if (error || !std::filesystem::is_directory(archiveStatus) ||
std::filesystem::is_symlink(archiveStatus)) {
return Api::fail(
Api::ErrorCode::PermissionDenied,
"managed Core archive root must be a real directory");
}
const auto label =
recordedRuntime && safeIdentifier(*recordedRuntime)
? *recordedRuntime
: std::string{"legacy"};
for (std::uint32_t suffix = 0; suffix < 10000; ++suffix) {
auto candidate =
archiveRoot /
(suffix == 0 ? label : std::format("{}.{}", label, suffix));
error.clear();
const auto candidateStatus =
std::filesystem::symlink_status(candidate, error);
if (error == std::errc::no_such_file_or_directory ||
candidateStatus.type() ==
std::filesystem::file_type::not_found) {
error.clear();
std::filesystem::rename(storeRoot, candidate, error);
if (error) {
return Api::fail(
Api::ErrorCode::PermissionDenied,
std::format(
"cannot archive the previous managed Core "
"store: {}",
error.message()));
}
archivedStore = std::move(candidate);
break;
}
if (error) {
return Api::fail(
Api::ErrorCode::PermissionDenied,
std::format("cannot inspect managed Core archive: {}",
error.message()));
}
}
if (!archivedStore) {
return Api::fail(Api::ErrorCode::AlreadyExists,
"managed Core archive namespace is exhausted");
}
} else if (error != std::errc::no_such_file_or_directory && error) {
return Api::fail(
Api::ErrorCode::PermissionDenied,
std::format("cannot inspect managed Core store: {}",
error.message()));
}
Void::Update::VersionStore freshStore(storeRoot);
if (auto initialized = freshStore.initialize(); !initialized) {
return Api::fail(Api::ErrorCode::UnknownError,
initialized.error().message());
}
if (auto written = Void::FileSystem::atomic_write(
markerPath, std::string{*installedRuntimeVersion});
!written) {
return Api::fail(Api::ErrorCode::PermissionDenied,
written.error().message());
}
return ManagedCoreStartupSelection{.corePath = bundledCore,
.archivedStore =
std::move(archivedStore),
.runtimeBaselineChanged = true};
}
Api::Result<void> reconcileManagedPluginUpdates(
const std::filesystem::path& stateDirectory)
{
+32 -29
View File
@@ -17,7 +17,6 @@
#include <Void/System/Process.hpp>
#include <Void/System/SignalWatcher.hpp>
#include <Void/TeamSpeak/Identity.hpp>
#include <Void/Update/Update.hpp>
#include <algorithm>
#include <array>
@@ -245,30 +244,6 @@ namespace
return std::nullopt;
}
[[nodiscard]] std::filesystem::path
reconcileManagedCore(const std::filesystem::path& stateDirectory,
const std::filesystem::path& fallback)
{
Void::Update::VersionStore store(stateDirectory / "updates" /
"components" / "core");
if (auto initialized = store.initialize(); !initialized) {
Void::Log::warn("main", "Cannot initialize managed Core store: {}",
initialized.error().message());
return fallback;
}
if (auto reconciled = store.reconcile(); !reconciled) {
Void::Log::warn("main", "Cannot reconcile managed Core store: {}",
reconciled.error().message());
return fallback;
}
auto current = store.currentVersion();
if (!current || !*current) {
return fallback;
}
const auto managed = store.versionPath(**current) / fallback.filename();
return std::filesystem::is_regular_file(managed) ? managed : fallback;
}
[[nodiscard]] Echoion::Api::Result<std::string>
invokeRuntimeHelper(const std::filesystem::path& helper,
const std::filesystem::path& root,
@@ -661,10 +636,38 @@ int main(int argc, char** argv)
configuredCoreOverride &&
absolutePath(*configuredCoreOverride) !=
absolutePath(installRoot / "lib" / "Echoion.ts");
const auto corePathResolved =
customCoreOverride
? configuredCorePath
: reconcileManagedCore(stateDirectory, configuredCorePath);
auto corePathResolved = configuredCorePath;
if (!customCoreOverride) {
const auto installedRuntimeVersion =
runningManifest ? std::optional<std::string_view>{runtimeVersion}
: std::nullopt;
auto selection = Echoion::Standalone::selectManagedCoreForStartup(
stateDirectory, configuredCorePath, installedRuntimeVersion);
if (!selection) {
Void::Log::warn(
"main",
"Cannot reconcile managed Core startup state; using the "
"bundled Core: {}",
selection.error().message);
} else {
corePathResolved = selection->corePath;
if (selection->runtimeBaselineChanged) {
if (selection->archivedStore) {
Void::Log::info(
"main",
"Runtime {} selected its bundled Core and archived "
"the previous managed Core store at {}",
runtimeVersion,
selection->archivedStore->string());
} else {
Void::Log::info(
"main",
"Runtime {} selected its bundled Core baseline",
runtimeVersion);
}
}
}
}
if (const auto workingDirectory =
Echoion::Standalone::runtimeWorkingDirectory(installRoot,
executableDirectory)) {
+9 -2
View File
@@ -1,6 +1,7 @@
#include "echoion/host/CoreLoader.hpp"
#include <cstdlib>
#include <ctime>
#include <filesystem>
#include <fstream>
#include <iostream>
@@ -34,11 +35,17 @@ int main()
::setenv("HOME", root.c_str(), 1);
const auto stateDirectory = root / "runtime-state";
::setenv("ECHOION_STATE_DIR", stateDirectory.c_str(), 1);
const auto ytDlp = root / ".local/share/echoion/yt-dlp/yt-dlp";
const auto dataDirectory = stateDirectory / "data";
::setenv("XDG_DATA_HOME", dataDirectory.c_str(), 1);
const auto ytDlp = dataDirectory / "echoion" / "yt-dlp" / "yt-dlp";
std::filesystem::create_directories(ytDlp.parent_path());
{
std::ofstream output{ytDlp};
output << "#!/bin/sh\necho 2026.01.01\n";
output << "#!/bin/sh\necho 9999.01.01\n";
}
{
std::ofstream output{ytDlp.parent_path() / ".last_update"};
output << std::time(nullptr);
}
std::filesystem::permissions(ytDlp,
std::filesystem::perms::owner_read |
+51
View File
@@ -250,6 +250,57 @@ int main()
};
TemporaryTree tree;
const auto startupState = tree.path() / "startup-state";
const auto bundledStartupCore =
tree.path() / "startup-runtime" / "lib" / "Echoion.ts";
check(writeFile(bundledStartupCore, "runtime-core"),
"failed to write startup Core fixture");
const auto startupStore = startupState / "updates" / "components" / "core";
check(publishHistoricalVersion(startupStore, "1.1.0", "Echoion.ts",
"stale-managed-core"),
"failed to publish stale managed Core fixture");
check(activatePublishedVersion(startupStore, "1.1.0"),
"failed to activate stale managed Core fixture");
auto firstRuntimeSelection =
Echoion::Standalone::selectManagedCoreForStartup(
startupState, bundledStartupCore, std::string_view{"2.0.0"});
check(firstRuntimeSelection.has_value(),
"runtime Core baseline selection failed");
if (firstRuntimeSelection) {
check(firstRuntimeSelection->corePath == bundledStartupCore,
"new runtime did not prefer its bundled Core");
check(firstRuntimeSelection->runtimeBaselineChanged,
"new runtime did not report a Core baseline change");
check(firstRuntimeSelection->archivedStore.has_value() &&
std::filesystem::is_regular_file(
*firstRuntimeSelection->archivedStore / "versions" /
"1.1.0" / "Echoion.ts"),
"stale managed Core store was not archived");
}
check(publishHistoricalVersion(startupStore, "1.1.0", "Echoion.ts",
"runtime-core"),
"failed to seed the new runtime Core store");
check(activatePublishedVersion(startupStore, "1.1.0"),
"failed to activate the new runtime Core baseline");
auto sameRuntimeSelection =
Echoion::Standalone::selectManagedCoreForStartup(
startupState, bundledStartupCore, std::string_view{"2.0.0"});
check(sameRuntimeSelection.has_value() &&
sameRuntimeSelection->corePath ==
startupStore / "versions" / "1.1.0" / "Echoion.ts" &&
!sameRuntimeSelection->runtimeBaselineChanged,
"same runtime did not retain its managed Core selection");
auto nextRuntimeSelection =
Echoion::Standalone::selectManagedCoreForStartup(
startupState, bundledStartupCore, std::string_view{"2.1.0"});
check(nextRuntimeSelection.has_value() &&
nextRuntimeSelection->corePath == bundledStartupCore &&
nextRuntimeSelection->runtimeBaselineChanged,
"next runtime did not invalidate the prior managed Core baseline");
const auto state = tree.path() / "state";
const auto incoming = tree.path() / "incoming";
const auto currentCore = tree.path() / "installed" / "Echoion.ts";