Bootstrap initialization. Idempotent across multiple callers. Substeps (each profiled): 1. ``apply_safe_config_environment_variables`` — pre-trust env subset. 2. ``setup_graceful_shutdown`` — SIGINT/SIGTERM handlers. 3. ``start_api_preconnect`` — DNS+TLS warmup (moved from cli.main)
()
| 61 | |
| 62 | @cache |
| 63 | def init() -> None: |
| 64 | """Bootstrap initialization. Idempotent across multiple callers. |
| 65 | |
| 66 | Substeps (each profiled): |
| 67 | 1. ``apply_safe_config_environment_variables`` — pre-trust env subset. |
| 68 | 2. ``setup_graceful_shutdown`` — SIGINT/SIGTERM handlers. |
| 69 | 3. ``start_api_preconnect`` — DNS+TLS warmup (moved from cli.main). |
| 70 | 4. Placeholder for plan-phase-2: remote-managed-settings init. |
| 71 | 5. Placeholder for plan-phase-2: policy-limits init. |
| 72 | |
| 73 | The placeholder substeps are no-ops for plan phase 1; they exist |
| 74 | to mark the seam where future work plugs in. |
| 75 | """ |
| 76 | profile_checkpoint("init_function_start") |
| 77 | |
| 78 | # ch02 round-2 PR-1 (G1): consume the keychain + MDM prefetches that |
| 79 | # ``src/cli.py`` fires at module import. The Popens are already |
| 80 | # running; ``wait_and_read_*`` blocks for at most the supplied |
| 81 | # timeout. Both helpers return ``None`` on any failure — silent |
| 82 | # degrade is intentional (TS behavior). |
| 83 | _logger.info("init: consuming keychain + MDM prefetches") |
| 84 | stash_keychain_credentials( |
| 85 | wait_and_read_keychain(get_or_start_keychain_prefetch(), timeout=5.0) |
| 86 | ) |
| 87 | mdm_payload = wait_and_read_mdm(get_or_start_mdm_raw_read(), timeout=2.0) |
| 88 | mdm_env = extract_mdm_env(mdm_payload) |
| 89 | profile_checkpoint("init_after_prefetch_consumption") |
| 90 | |
| 91 | # ch02 round-3: the pre-trust pass applies the TRUSTED tiers in full |
| 92 | # (global config env — including stored API keys like TAVILY_API_KEY — |
| 93 | # and user settings env), the SAFE subset of the project-scoped tiers, |
| 94 | # and the MDM policy env last. Project/local tiers apply in full only |
| 95 | # after trust (run_pre_action seeding or a trust-gate accept). The old |
| 96 | # secret_store startup applier is gone — it copied the MERGED env |
| 97 | # (committable project tiers included) with no trust gate. |
| 98 | _logger.info("init: applying pre-trust env (trusted tiers + safe subset)") |
| 99 | apply_safe_config_environment_variables(extra_env=mdm_env) |
| 100 | profile_checkpoint("init_safe_env_vars_applied") |
| 101 | |
| 102 | _logger.info("init: setting up graceful shutdown") |
| 103 | setup_graceful_shutdown() |
| 104 | profile_checkpoint("init_after_graceful_shutdown") |
| 105 | |
| 106 | _logger.info("init: starting API preconnect") |
| 107 | start_api_preconnect() |
| 108 | profile_checkpoint("init_after_api_preconnect") |
| 109 | |
| 110 | _placeholder_initialize_remote_managed_settings() |
| 111 | _placeholder_initialize_policy_limits() |
| 112 | profile_checkpoint("init_function_end") |
| 113 | |
| 114 | |
| 115 | def _placeholder_initialize_remote_managed_settings() -> None: |