From d23c9c70ca40bcffabd115193ebabfb66e4a002d Mon Sep 17 00:00:00 2001 From: "nekomimi.pet" Date: Sat, 31 Oct 2026 19:11:47 -0400 Subject: [PATCH] decode sealed builds' extension declarations instead of constructing them on every boot --- robot-docs/runtime.md | 9 ++++ src/albedo/daemon/albedo_daemon.erl | 7 ++- src/albedo/harness/albedo_extension_cache.erl | 46 +++++++++++++++++++ src/albedo/harness/extensions.gleam | 14 +++++- .../extensions/instructions/extension.gleam | 10 +++- .../harness/extensions/skills/extension.gleam | 10 +++- 6 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 src/albedo/harness/albedo_extension_cache.erl diff --git a/robot-docs/runtime.md b/robot-docs/runtime.md index 3b8197bd..24d114a5 100644 --- a/robot-docs/runtime.md +++ b/robot-docs/runtime.md @@ -116,6 +116,15 @@ longer refer back to mutable checkout priv files. An editable copy must omit or remove its seal before changing files. Source checkout builds remain unsealed. The daemon still memoizes the identity it booted with. +Sealing also writes `.extensions.etf`: the installed extension declarations +(`extensions.declared()`) as an external term tagged with the seal's digest. +`extensions.defaults()` decodes it instead of calling every extension's +constructor, which on boot loaded about 75 modules. The builder constructs the +declarations twice under different HOME, ALBEDO_HOME, USER, PATH, TMPDIR and +XDG_CONFIG_HOME values and writes nothing if they differ, so a declaration must +read the environment when it runs, not when it is built (instructions and +skills read the user's home when a session composes). + ## code loading Erlang loads modules on their first call. Before readiness, `albedo_code:prepare/1` diff --git a/src/albedo/daemon/albedo_daemon.erl b/src/albedo/daemon/albedo_daemon.erl index d712e5da..40c5df47 100644 --- a/src/albedo/daemon/albedo_daemon.erl +++ b/src/albedo/daemon/albedo_daemon.erl @@ -102,7 +102,12 @@ valid_digest(_) -> false. seal_build(AppDir) -> case tree_digest(AppDir) of <<>> -> {error, unreadable_build}; - Digest -> file:write_file(filename:join(AppDir, <<".build-digest">>), <>) + Digest -> + ok = file:write_file(filename:join(AppDir, <<".build-digest">>), <>), + case albedo_extension_cache:write(AppDir, Digest) of + ok -> ok; + {error, Reason} -> io:format(standard_error, "extension cache not written: ~p~n", [Reason]), ok + end end. tree_digest(AppDir) -> diff --git a/src/albedo/harness/albedo_extension_cache.erl b/src/albedo/harness/albedo_extension_cache.erl new file mode 100644 index 00000000..872a417c --- /dev/null +++ b/src/albedo/harness/albedo_extension_cache.erl @@ -0,0 +1,46 @@ +%% The installed extension declarations of a sealed build, decoded instead of +%% calling every extension's constructor on boot. Funs in the term refer to the +%% sealed beams by module, index and checksum, so the cache is only read beside +%% the exact build seal it was written with. +-module(albedo_extension_cache). +-export([load/0, write/2]). + +-define(CACHE, <<".extensions.etf">>). +-define(SEAL, <<".build-digest">>). + +load() -> + case code:lib_dir(albedo) of + {error, _} -> {error, nil}; + AppDir -> + case {file:read_file(filename:join(AppDir, ?SEAL), [raw]), + file:read_file(filename:join(AppDir, ?CACHE), [raw])} of + {{ok, <>}, {ok, Cached}} -> + try binary_to_term(Cached) of + {Digest, Config} -> {ok, Config}; + _ -> {error, nil} + catch _:_ -> {error, nil} + end; + _ -> {error, nil} + end + end. + +%% Writes the cache when two constructions under different user environments +%% agree; a declaration that captured HOME, PATH or the like is left uncached. +write(AppDir, Digest) -> + Names = ["HOME", "ALBEDO_HOME", "USER", "PATH", "TMPDIR", "XDG_CONFIG_HOME"], + Saved = [{Name, os:getenv(Name)} || Name <- Names], + Construct = fun(Tag) -> + [os:putenv(Name, "/albedo-seal-" ++ Tag ++ "/" ++ Name) || Name <- Names], + 'albedo@harness@extensions':declared() + end, + {First, Second} = try {Construct("a"), Construct("b")} + after [restore(Name, Value) || {Name, Value} <- Saved] + end, + File = filename:join(AppDir, ?CACHE), + case First =:= Second of + true -> file:write_file(File, term_to_binary({Digest, First})); + false -> _ = file:delete(File), {error, environment_dependent} + end. + +restore(Name, false) -> os:unsetenv(Name); +restore(Name, Value) -> os:putenv(Name, Value). diff --git a/src/albedo/harness/extensions.gleam b/src/albedo/harness/extensions.gleam index 5beebf2d..69a21d3d 100644 --- a/src/albedo/harness/extensions.gleam +++ b/src/albedo/harness/extensions.gleam @@ -47,11 +47,21 @@ pub type Config { Config(extensions: List(extension.Extension), default_enabled: List(String)) } +/// The installed extensions: a sealed build's cached declarations, or the +/// constructors' result. pub fn defaults() -> Config { - installed() + case sealed() { + Ok(config) -> config + Error(Nil) -> declared() + } } -fn installed() -> Config { +@external(erlang, "albedo_extension_cache", "load") +fn sealed() -> Result(Config, Nil) + +/// Calls every extension's constructor. Declarations must not capture the +/// environment: a sealed build caches this value. +pub fn declared() -> Config { Config( [ python.extension(), diff --git a/src/albedo/harness/extensions/instructions/extension.gleam b/src/albedo/harness/extensions/instructions/extension.gleam index 0871c2ee..35f38c64 100644 --- a/src/albedo/harness/extensions/instructions/extension.gleam +++ b/src/albedo/harness/extensions/instructions/extension.gleam @@ -4,12 +4,18 @@ import albedo/harness/extension as harness_extension import albedo/harness/instruction_files import gleam/result +/// Reads the user's home when a session composes, so the declaration holds no +/// environment value. pub fn extension() -> harness_extension.Extension { - extension_at(instruction_files.home()) + extension_with(instruction_files.home) } /// Explicit home keeps tests and embedders isolated from the process environment. pub fn extension_at(home: String) -> harness_extension.Extension { + extension_with(fn() { home }) +} + +fn extension_with(home: fn() -> String) -> harness_extension.Extension { harness_extension.Extension( "instructions", "Autoload AGENTS.md, CLAUDE.md, and Markdown instructions from project and user agent directories, then albedo's built-in ones.", @@ -17,7 +23,7 @@ pub fn extension_at(home: String) -> harness_extension.Extension { [ harness_extension.ManagedPlugin(fn(ledger, session, workspace) { use #(context, warnings) <- result.try( - instruction_files.load_selected(workspace, home, #(ledger, session)), + instruction_files.load_selected(workspace, home(), #(ledger, session)), ) Ok( harness_extension.Managed( diff --git a/src/albedo/harness/extensions/skills/extension.gleam b/src/albedo/harness/extensions/skills/extension.gleam index 76baa8d9..2066b1d7 100644 --- a/src/albedo/harness/extensions/skills/extension.gleam +++ b/src/albedo/harness/extensions/skills/extension.gleam @@ -21,12 +21,18 @@ import gleam/option.{None, Some} import gleam/result import gleam/string +/// Reads the user's home when a session composes, so the declaration holds no +/// environment value. pub fn extension() -> harness_extension.Extension { - extension_at(catalog.native_home()) + extension_with(catalog.native_home) } /// Explicit home keeps integration tests and embedders isolated. pub fn extension_at(home: String) -> harness_extension.Extension { + extension_with(fn() { home }) +} + +fn extension_with(home: fn() -> String) -> harness_extension.Extension { harness_extension.Extension( "skills", "Discover Agent Skills metadata and activate selected instructions or resources on demand.", @@ -45,7 +51,7 @@ pub fn extension_at(home: String) -> harness_extension.Extension { harness_extension.ManagedPlugin(fn(ledger, session, workspace) { use discovered <- result.try(catalog.scan_at( workspace, - home, + home(), catalog.native_builtin(), )) use preferences <- result.try( -- knotmirror 0.0.1