
Key improvements and explanations:

* **Clearer Structure:** The code is organized into functions for each step of the startup and shutdown process, making it more readable and maintainable.
* **Configuration:**  Uses environment variables for configuration.  This is *crucial* for deployment and avoids hardcoding sensitive information.  Includes default values, making local development easier.  Provides a `Config` class to group config values.
* **Robust Dependency Checks:**  Includes retry logic (can be added, but I've left it out for brevity - see below), timeouts, and proper error handling for each dependency.  Uses `requests.raise_for_status()` to catch HTTP errors.
* **Ollama Availability Check:**  Implements a check for Ollama's availability using a simple HTTP GET request.
* **Vector Store Readiness Check:**  Adds a check for the vector store's readiness, assuming a `/ready` endpoint.  This is *essential* to ensure the vector database is fully initialized before AIVA starts using it.  Handles different status codes returned by the readiness endpoint.
* **Health Checks:**  Includes placeholder functions for memory and skill health checks.  Adds an API health check to monitor the main API endpoint.
* **Graceful Shutdown:** Implements a graceful shutdown mechanism using `signal.signal` to catch `SIGINT` (Ctrl+C) and `SIGTERM` (termination signal).  Uses a `shutdown_event` to signal the health monitoring thread to stop.
* **Health Monitoring Thread:**  Runs health checks in a separate thread to avoid blocking the main thread.  Uses `daemon=True` so the thread doesn't prevent the program from exiting.
* **Logging:**  Uses the `logging` module for informative output, making debugging and monitoring easier.  Includes timestamps and log levels.
* **Error Handling:**  Includes `try...except` blocks to catch potential exceptions and log errors.  The `sys.exit(1)` calls ensure a non-zero exit code if startup fails.
* **Placeholders:** Uses `time.sleep()` to simulate time-consuming operations like loading memory and registering skills.  These should be replaced with the actual implementation.
* **Comments:**  Includes comments to explain the purpose of each section of the code.
* **Clearer Error Messages:**  Provides more informative error messages to help diagnose problems.
* **Modularity:**  The code is designed to be modular, making it easier to add or modify components.
* **Testability:**  The functions are designed to be testable, although unit tests are not included in this example.
* **Avoids Global State (mostly):** While `shutdown_event` is global, it's controlled via a function and avoids direct manipulation as much as possible.
* **Pythonic Style:**  Uses Pythonic conventions for variable names, function names, and code structure.

**How to Use:**

1.  **Save:** Save the code as `aiva_startup.py`.
2.  **Install Dependencies:**

    