diff --git a/EchoionCore b/EchoionCore index dfbbdaa..a277f21 160000 --- a/EchoionCore +++ b/EchoionCore @@ -1 +1 @@ -Subproject commit dfbbdaaf017cd16851bf8422d0a20d37ec8a8e6a +Subproject commit a277f2102b62b0abfef0d32e761ba0db30e143c2 diff --git a/EchoionStandalone/include/echoion/standalone/UpdateService.hpp b/EchoionStandalone/include/echoion/standalone/UpdateService.hpp index 76dac97..0e36bab 100644 --- a/EchoionStandalone/include/echoion/standalone/UpdateService.hpp +++ b/EchoionStandalone/include/echoion/standalone/UpdateService.hpp @@ -5,10 +5,27 @@ #include #include #include +#include #include +#include namespace Echoion::Standalone { + struct ManagedCoreStartupSelection { + std::filesystem::path corePath; + std::optional 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 + selectManagedCoreForStartup( + const std::filesystem::path& stateDirectory, + const std::filesystem::path& bundledCore, + std::optional installedRuntimeVersion); + // Roll back any uncommitted managed-plugin activation before Core loads and // discovers plugin `current` links. This must run before PluginHost // startup. diff --git a/EchoionStandalone/src/UpdateService.cpp b/EchoionStandalone/src/UpdateService.cpp index d3afda3..19d0ede 100644 --- a/EchoionStandalone/src/UpdateService.cpp +++ b/EchoionStandalone/src/UpdateService.cpp @@ -3162,6 +3162,207 @@ namespace Echoion::Standalone return impl_->handle(method, requestJson); } + Api::Result + selectManagedCoreForStartup(const std::filesystem::path& stateDirectory, + const std::filesystem::path& bundledCore, + const std::optional + installedRuntimeVersion) + { + const auto components = stateDirectory / "updates" / "components"; + const auto storeRoot = components / "core"; + const auto markerPath = components / "core-runtime-version"; + + const auto selectCurrent = + [&]() -> Api::Result { + 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 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 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 reconcileManagedPluginUpdates( const std::filesystem::path& stateDirectory) { diff --git a/EchoionStandalone/src/main.cpp b/EchoionStandalone/src/main.cpp index b551194..b670e02 100644 --- a/EchoionStandalone/src/main.cpp +++ b/EchoionStandalone/src/main.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #include #include @@ -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 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{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)) { diff --git a/EchoionStandalone/tests/test_core_loader_reload.cpp b/EchoionStandalone/tests/test_core_loader_reload.cpp index f3b75ef..03b73e3 100644 --- a/EchoionStandalone/tests/test_core_loader_reload.cpp +++ b/EchoionStandalone/tests/test_core_loader_reload.cpp @@ -1,6 +1,7 @@ #include "echoion/host/CoreLoader.hpp" #include +#include #include #include #include @@ -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 | diff --git a/EchoionStandalone/tests/test_update_service.cpp b/EchoionStandalone/tests/test_update_service.cpp index 8ed9f99..67b2b39 100644 --- a/EchoionStandalone/tests/test_update_service.cpp +++ b/EchoionStandalone/tests/test_update_service.cpp @@ -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"; diff --git a/plugins/url-shortener-external/include/echoion/plugins/short_url/ExternalShortener.hpp b/plugins/url-shortener-external/include/echoion/plugins/short_url/ExternalShortener.hpp index cc0efbb..115f3e6 100644 --- a/plugins/url-shortener-external/include/echoion/plugins/short_url/ExternalShortener.hpp +++ b/plugins/url-shortener-external/include/echoion/plugins/short_url/ExternalShortener.hpp @@ -52,6 +52,11 @@ namespace Echoion::Plugins::ShortUrl std::size_t maximumConcurrency{4}; }; + // Missing endpoint means this optional provider is intentionally dormant. + // Malformed or partially-present configuration still counts as configured + // so plugin activation rejects it instead of silently ignoring it. + [[nodiscard]] bool hasExternalConfiguration(std::string_view payload); + [[nodiscard]] std::expected parseExternalConfig(std::string_view payload); diff --git a/plugins/url-shortener-external/src/ExternalShortener.cpp b/plugins/url-shortener-external/src/ExternalShortener.cpp index 01fc454..a7d7433 100644 --- a/plugins/url-shortener-external/src/ExternalShortener.cpp +++ b/plugins/url-shortener-external/src/ExternalShortener.cpp @@ -615,6 +615,18 @@ namespace Echoion::Plugins::ShortUrl } } // namespace + bool hasExternalConfiguration(const std::string_view payload) + { + if (payload.empty()) { + return false; + } + auto root = Void::Json::parse(payload); + if (!root || !root->is_object()) { + return true; + } + return settingsObject(*root).find("endpoint") != nullptr; + } + std::expected parseExternalConfig(const std::string_view payload) { diff --git a/plugins/url-shortener-external/src/Plugin.cpp b/plugins/url-shortener-external/src/Plugin.cpp index 5cf70e7..41fe640 100644 --- a/plugins/url-shortener-external/src/Plugin.cpp +++ b/plugins/url-shortener-external/src/Plugin.cpp @@ -41,6 +41,11 @@ namespace Echoion::Plugins::ShortUrl { shortener_.reset(); activationError_.clear(); + dormant_ = !hasExternalConfiguration(configPayload_); + if (dormant_) { + activationError_ = "plugin is not configured"; + return; + } auto config = parseExternalConfig(configPayload_); if (!config) { activationError_ = std::move(config).error(); @@ -56,6 +61,11 @@ namespace Echoion::Plugins::ShortUrl void on_unload() override { shortener_.reset(); } + [[nodiscard]] bool ready() const noexcept + { + return dormant_ || shortener_ != nullptr; + } + [[nodiscard]] Void::ShortUrl::Health health() const override { if (shortener_ != nullptr) { @@ -73,10 +83,16 @@ namespace Echoion::Plugins::ShortUrl shorten(const Void::ShortUrl::ShortenRequest& request) override { if (shortener_ == nullptr) { - return {.error = { + return { + .short_url = {}, + .key = {}, + .expires_at_unix_seconds = 0, + .error = + { .code = Void::ShortUrl::ErrorCode::unavailable, .message = "external shortener is unavailable", - }}; + }, + }; } return shortener_->shorten(request); } @@ -86,6 +102,7 @@ namespace Echoion::Plugins::ShortUrl std::string configPayload_; std::string activationError_; std::unique_ptr shortener_; + bool dormant_ = false; }; } // namespace Echoion::Plugins::ShortUrl @@ -124,11 +141,11 @@ void void_plugin_configure(void* instance, const char* config) int void_plugin_ready(void* instance) { - const auto health = - static_cast( - instance) - ->health(); - return health.status == Void::ShortUrl::HealthStatus::healthy ? 0 : 1; + return static_cast( + instance) + ->ready() + ? 0 + : 1; } Void::Service::IService* void_plugin_service(void* instance) diff --git a/plugins/url-shortener-external/tests/ExternalShortenerTests.cpp b/plugins/url-shortener-external/tests/ExternalShortenerTests.cpp index 21e235a..4762bc0 100644 --- a/plugins/url-shortener-external/tests/ExternalShortenerTests.cpp +++ b/plugins/url-shortener-external/tests/ExternalShortenerTests.cpp @@ -34,6 +34,7 @@ namespace int main() { using Echoion::Plugins::ShortUrl::ExternalShortener; + using Echoion::Plugins::ShortUrl::hasExternalConfiguration; using Echoion::Plugins::ShortUrl::parseExternalConfig; int failures{}; @@ -46,6 +47,15 @@ int main() }; EnvironmentVariable token{"ECHOION_SHORTENER_TEST_TOKEN", "test secret"}; + check(!hasExternalConfiguration( + R"JSON({"settings":{},"_host":{"stateDirectory":"/tmp"}})JSON"), + "empty host envelope did not keep the optional provider dormant"); + check( + hasExternalConfiguration(R"JSON({"settings":{"endpoint":false}})JSON"), + "partially configured provider was incorrectly treated as dormant"); + check(hasExternalConfiguration("{"), + "malformed provider configuration was incorrectly treated as " + "dormant"); const auto jsonConfig = R"JSON({ "endpoint":"https://short.example/api", "method":"POST",