This document describes the shared startup-initialization helpers exposed from src/uagent/runtime_init.py.
runtime_init.py is a compatibility/re-export layer. The concrete implementations live in:
runtime_workdir.pyruntime_banner.pyruntime_env.pyruntime_memory.py
What it covers (shared across CLI/Web/GUI):
- decide and validate
workdir(--workdir/-C,UAGENT_WORKDIR, or auto) - validate startup environment when needed (
validate_or_exit_startup_env(context=...)) - create the directory and
chdir - build startup banner text
- append personal long-term memory and shared memory as system messages
- load
.envand.env.secfrom the current working directory at import time whenpython-dotenvis available
Design policy:
runtime_init.pydoes not print by itself; helpers return values and UI code decides how to display them.- Import-time environment loading is best-effort.
.envis loaded first withoverride=False, then.env.secis decrypted and loaded withoverride=True. - If
.uagent.keyexists in the current working directory, it is used to decrypt.env.sec.
runtime_init.py loads environment files from the current working directory as soon as it is imported.
Load order:
.envif it exists (override=False).env.secif it exists (decrypt and thenoverride=True)
Notes:
.env.secis decrypted viauag_envsec.secret_core.decrypt_text.- If
.uagent.keyexists in the current working directory, it is used as the key file. - If decryption fails, a warning is printed to stderr:
[WARN] Failed to decrypt .env.sec: ...
decide_workdir() resolves the working directory in this order:
- CLI argument:
--workdir/-C - Environment variable:
UAGENT_WORKDIR - Auto: the current directory (
./as an absolute path)
- If the resolved path already exists and is a file,
decide_workdir()raisesNotADirectoryError.
decide_workdir(cli_workdir: Optional[str], env_workdir: Optional[str]) -> WorkdirDecision
WorkdirDecision contains:
chosen: the original selection (CLI / ENV / auto)chosen_source:"CLI"/"ENV(UAGENT_WORKDIR)"/"auto"chosen_expanded: theexpanduser()-resolved path
apply_workdir() creates the directory and changes the current process directory.
os.makedirs(..., exist_ok=True)os.chdir(...)
API:
apply_workdir(decision: WorkdirDecision) -> None
build_startup_banner() generates the INFO/WARN lines shown during startup.
Representative output:
[INFO] workdir = ... (source: ...)[INFO] provider = ...- provider-specific lines:
azure:base_url+api_versionopenai/openrouter/grok/nvidia/bedrock/ollama:base_urlvertexai:project+location
- If
UAGENT_RESPONSES=1is set and the selected provider is not one ofazure,openai,bedrock,openrouter, orollama(excludinggemini,claude, andvertexai), a warning is appended:[WARN] UAGENT_RESPONSES=1 is set, but provider '...' does not support Responses API. Falling back to ChatCompletions.
[INFO] LLM streaming = enabledordisabled
API:
build_startup_banner(core, workdir: str, workdir_source: str) -> str
Notes:
- Secrets such as API keys are never printed.
core.normalize_url()is used when available; otherwise URLs are trimmed conservatively.build_startup_banner()does not print the Responses/ChatCompletions mode line itself. CLI/Web/GUI may print that separately after the banner.
append_long_memory_system_messages() consolidates the personal/shared long-term memory loading path.
- Personal long-term memory is loaded via
tools.long_memory - Shared memory is loaded via
tools.shared_memoryonly when enabled - Any generated system message is appended to
messages - Added messages are also passed to
core.log_message()
API:
append_long_memory_system_messages(...) -> Dict[str, bool]
Returned flags:
shared_enabled: whether shared memory is enabled (shared_memory_mod.is_enabled())
Notes:
- The function does not print.
- The function swallows internal exceptions and leaves warning handling to the caller.
- The current implementation does not prefix shared-memory content with a special label.
- The current implementation does not return
personal_appended/shared_appendedflags.
- CLI:
cli.pyuses these helpers inside startup capture before the main session loop. - Web:
web.pyuses the same helpers during startup and history initialization. - GUI:
gui.pyuses the same helpers during startup and worker initialization.
runtime_init.py re-exports the common helpers used by the UIs:
WorkdirDecisionapply_workdirdecide_workdirbuild_startup_bannervalidate_or_exit_startup_envappend_long_memory_system_messages