hosting_metadata

One-shot hosting provider and geographic location for the system prompt.

Probed asynchronously at bot startup and merged into PromptRenderer default_extras.

async hosting_metadata.probe_hosting_metadata()[source]

Detect cloud provider / region and geographic location (best effort).

Opens one shared aiohttp.ClientSession and races the four cloud metadata probes (_aws_probe(), _oci_probe(), _gcp_probe(), _azure_probe()) concurrently via asyncio.gather(), picking the first provider that responds in gather order (AWS, then OCI, GCP, Azure). It then layers on an IP-based geolocation – _fetch_public_ipv4() feeding _geolocate_ipapi() – and reconciles the two location sources with _merge_location(). All network failures are absorbed by the individual probes, so this function itself never raises and degrades to the placeholder strings "Unknown" / "Location unavailable".

Performs only outbound HTTP (link-local IMDS endpoints plus the public ipify/ipapi.co services); no Redis, KG, or filesystem access. Called by ensure_hosting_metadata_in_prompt(), and exercised directly by tests/test_hosting_metadata.py.

Returns:

A mapping with hosting_provider (a human-readable provider label, or "Unknown") and server_location (the merged geo/region string, or "Location unavailable"), suitable for the renderer’s Jinja default_extras.

Return type:

dict[str, str]

async hosting_metadata.ensure_hosting_metadata_in_prompt(renderer)[source]

Run probe_hosting_metadata() once per process; update renderer extras.

Probes the host’s cloud provider and location a single time and folds the resulting hosting_provider / server_location values into the renderer’s Jinja default_extras so the system prompt can surface where the bot is running. Guarded by the module-level _HOSTING_PROBE_LOCK and the _HOSTING_APPLIED flag, making it idempotent and concurrency-safe across overlapping service-startup calls; if the probe raises it is caught and placeholder extras are applied instead, so the flag is always set and the work runs at most once.

Mutates renderer.default_extras in place (the only side effect besides the one-shot HTTP performed inside probe_hosting_metadata()). Intended to be awaited from a service entry point during startup; currently invoked directly only by tests/test_hosting_metadata.py (no in-repo production caller imports this module yet).

Parameters:

renderer (Any) – The prompt renderer whose default_extras dict receives the hosting_provider and server_location keys; typed loosely as Any to avoid importing prompt_renderer.PromptRenderer.

Return type:

None

hosting_metadata.reset_hosting_metadata_probe_for_tests()[source]

Reset the process-level idempotency flag so the probe can rerun (tests only).

Clears _HOSTING_APPLIED to False so a subsequent ensure_hosting_metadata_in_prompt() will probe again instead of short-circuiting on the one-shot guard. Pure in-process state mutation with no I/O. Called only from tests/test_hosting_metadata.py (fixture setup) to give each test a clean starting state; not used in production.

Return type:

None