fix: keep large database startup alive (#3624)
This commit is contained in:
@@ -35,6 +35,8 @@ retains ownership of its values, validation, authority, and persistence.
|
||||
|
||||
### Requirements
|
||||
|
||||
- [Startup lifecycle](requirements/startup-lifecycle.md)
|
||||
|
||||
- [Agent-accessible Kandev settings](requirements/agent-settings-parity.md)
|
||||
|
||||
|
||||
@@ -80,6 +82,8 @@ retains ownership of its values, validation, authority, and persistence.
|
||||
|
||||
### System design
|
||||
|
||||
- [Startup lifecycle](system-design/startup-lifecycle.md)
|
||||
|
||||
- [Agent-accessible Kandev settings](system-design/agent-settings-parity.md)
|
||||
- [Core settings domain adoption](system-design/agent-settings-domains.md)
|
||||
|
||||
|
||||
@@ -65,11 +65,11 @@ resolved by explicit decision — see [Decisions](#decisions).
|
||||
|
||||
- **AC-PLATFORM-HEALTH-ENDPOINT-VERSION-001.1:** `GET /health` SHALL include the running Kandev version in its JSON response body.
|
||||
- **AC-PLATFORM-HEALTH-ENDPOINT-VERSION-001.2:** The field SHALL be named **`version`**, matching `GET /api/v1/system/info`.
|
||||
- **AC-PLATFORM-HEALTH-ENDPOINT-VERSION-001.3:** The field SHALL be present in **both** the ready (200) and not-ready (503) responses, so an operator can identify the build of a backend that is stuck starting.
|
||||
- **AC-PLATFORM-HEALTH-ENDPOINT-VERSION-001.3:** The field SHALL be present in both `/health` and bootstrap `/ready` responses, so an operator can identify the build of a backend that is stuck starting.
|
||||
- **AC-PLATFORM-HEALTH-ENDPOINT-VERSION-001.4:** The field value SHALL be the same string `GET /api/v1/system/info` reports as `version` for the same running process.
|
||||
- **AC-PLATFORM-HEALTH-ENDPOINT-VERSION-001.5:** The field SHALL be served to unauthenticated callers, exactly as the rest of the `/health` payload already is. This is a deliberate, accepted disclosure — see [Security](#security-and-permissions).
|
||||
- **AC-PLATFORM-HEALTH-ENDPOINT-VERSION-001.6:** Every existing field (`status`, `service`, `mode`) SHALL retain its current name, value, and semantics. This is a purely additive change.
|
||||
- **AC-PLATFORM-HEALTH-ENDPOINT-VERSION-001.7:** The existing HTTP status semantics (200 when ready, 503 while starting) SHALL be unchanged.
|
||||
- **AC-PLATFORM-HEALTH-ENDPOINT-VERSION-001.7:** `/health` SHALL return 200 once bound, including during initialization. `/ready` SHALL return 503 until application initialization finishes.
|
||||
- **AC-PLATFORM-HEALTH-ENDPOINT-VERSION-001.8:** The desktop health-token response header SHALL continue to be set on the 200 path only, unchanged.
|
||||
|
||||
## System design
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
---
|
||||
status: active
|
||||
system: platform
|
||||
created: 2026-09-11
|
||||
owners:
|
||||
- kandev
|
||||
---
|
||||
|
||||
# Startup lifecycle requirements
|
||||
|
||||
## Overview
|
||||
|
||||
Platform owns process startup, liveness, readiness, and shutdown across native launch modes and desktop.
|
||||
Large databases must complete initialization without a listener-health deadline interrupting valid work.
|
||||
|
||||
## Requirements
|
||||
|
||||
### REQ-PLATFORM-STARTUP-LIFECYCLE-001: Observable initialization
|
||||
|
||||
**Intent:** Separate process liveness from application readiness while preserving upgrade safety.
|
||||
|
||||
#### Acceptance criteria
|
||||
|
||||
- **AC-PLATFORM-STARTUP-LIFECYCLE-001.1:** During database opening, backup, migration, and required recovery, `/health` shall answer successfully with existing version and identity semantics.
|
||||
- **AC-PLATFORM-STARTUP-LIFECYCLE-001.2:** Until required initialization and router installation finish, `/ready` and application endpoints shall remain unsuccessful.
|
||||
- **AC-PLATFORM-STARTUP-LIFECYCLE-001.3:** After liveness succeeds, the launcher shall wait for readiness without an initialization deadline. Explicit listener-timeout overrides shall remain effective.
|
||||
- **AC-PLATFORM-STARTUP-LIFECYCLE-001.4:** Startup shall report phase transitions and elapsed time, with bounded periodic status during long phases and no estimated percentages.
|
||||
- **AC-PLATFORM-STARTUP-LIFECYCLE-001.5:** Failed initialization shall report its phase and underlying error, release resources, and exit unsuccessfully. The launcher shall detect child exit.
|
||||
- **AC-PLATFORM-STARTUP-LIFECYCLE-001.6:** Cancellation shall close startup listeners and stop further initialization admission at safe schema and store boundaries. Signal handling shall remain available throughout startup and shutdown. Restore worker quiescing shall not cancel the process and HTTP lifetime before the restore job can publish its result.
|
||||
- **AC-PLATFORM-STARTUP-LIFECYCLE-001.7:** A required upgrade backup shall complete before application migrations mutate the database. Backup failure shall prevent migrations.
|
||||
- **AC-PLATFORM-STARTUP-LIFECYCLE-001.8:** A listener bind failure shall stop startup promptly and identify the bind error.
|
||||
|
||||
## Exclusions
|
||||
|
||||
Backup reuse, retention redesign, migration-registry conversion, and background lifecycle-sweep scheduling are outside this change.
|
||||
|
||||
## System design
|
||||
|
||||
See [startup lifecycle](../system-design/startup-lifecycle.md).
|
||||
@@ -0,0 +1,105 @@
|
||||
---
|
||||
status: current
|
||||
system: platform
|
||||
requirements:
|
||||
- REQ-PLATFORM-STARTUP-LIFECYCLE-001
|
||||
---
|
||||
|
||||
# Startup lifecycle design
|
||||
|
||||
## Ownership and flow
|
||||
|
||||
Platform owns this shared backend lifecycle. Configuration, runtime-state ownership,
|
||||
logging, and startup security validation precede the bootstrap listener.
|
||||
The listener precedes event-bus and database initialization. One handler switch,
|
||||
HTTP server, and listener set survive through application-router installation.
|
||||
Only the bootstrap handler serves traffic during initialization.
|
||||
|
||||
`backendapp.run` owns the listener and cancellation lifetime. Startup state travels
|
||||
through context to persistence and router composition. Initialization remains
|
||||
sequential; cleanup must not race constructors or database mutation.
|
||||
Signal handling begins before database work. The first signal cancels startup;
|
||||
the existing second-signal forced-exit behavior remains available.
|
||||
|
||||
The bootstrap owns separate process and worker cancellation contexts. A startup
|
||||
signal cancels the process context and closes the bootstrap listener while the
|
||||
initializer drains. Restore quiescing cancels only worker and runtime contexts;
|
||||
the process context, HTTP listener, and job tracker remain alive until restore
|
||||
has installed the staged database and published `restart_required`, after
|
||||
which an explicit shutdown can close the process.
|
||||
|
||||
## Readiness and status
|
||||
|
||||
`/health` retains its existing body and desktop token header. Bootstrap `/ready`
|
||||
returns 503 with a `startup` object containing a fixed phase name, total elapsed
|
||||
milliseconds, and phase elapsed milliseconds. No paths, SQL, credentials, or raw
|
||||
errors enter this unauthenticated payload. Application routes remain 503.
|
||||
|
||||
Phases cover database opening, backup, migrations, service initialization,
|
||||
required session recovery, and ready. Structured logs record transitions and
|
||||
phase duration. Launcher readiness polling reads JSON, reports transitions and
|
||||
periodic elapsed status, and remembers the last phase for child-exit diagnostics.
|
||||
Underlying errors remain in backend diagnostic output. Readiness waits retain
|
||||
cancellation and child-exit checks without a separate deadline.
|
||||
|
||||
Router publication occurs only after required stores, recovery gates, final
|
||||
persistence checks, and router construction succeed. The existing background
|
||||
lifecycle-token sweep remains outside readiness: its watcher/scheduler ordering
|
||||
and deadline are unchanged.
|
||||
|
||||
## Timeout policy
|
||||
|
||||
Keep release listener-health timeout at 45000 ms, development/E2E at 600000 ms.
|
||||
Keep environment and YAML overrides. A 300000 ms default gives no database-work
|
||||
benefit after early binding; it delays genuine pre-listener failure detection.
|
||||
Desktop retains its existing independent 60-second liveness budget and unlimited
|
||||
readiness wait. No timeout default changes are required.
|
||||
|
||||
Initialization checks cancellation before and after each repository schema step
|
||||
and before each required store admission. The task repository passes the same
|
||||
context to its migration SQL, including data backfills and transactional table
|
||||
rebuilds. Other legacy constructors can finish an already admitted statement
|
||||
because they do not expose a context API; the next admission barrier prevents
|
||||
later stores from opening. A statement already executing in a context-aware
|
||||
driver is also subject to that driver's cancellation behavior, so cancellation
|
||||
does not promise an immediate interrupt at every SQLite virtual-machine or
|
||||
user-function instruction. Cleanup waits until the synchronous initializer
|
||||
returns, avoiding a pool-close race with in-flight SQL.
|
||||
|
||||
## Persistence safety
|
||||
|
||||
Keep `VACUUM INTO`, version-based backup eligibility, and two-snapshot retention.
|
||||
Context cancellation applies to the backup SQL. Repository initialization failure
|
||||
must release previously constructed resources; a completed backup remains intact.
|
||||
The successful version marker stays behind the final persistence/router gates.
|
||||
|
||||
Pending migrations cannot be inferred from `MigrateLogger`: owners also execute
|
||||
DDL, table rebuilds, data repairs, and seed changes directly, including stores
|
||||
constructed after `provideRepositories`. A future registry must account for all
|
||||
required stores, late constructors, dialect-specific DDL, data backfills, and
|
||||
seed migrations, with an immutable applied ledger and completeness tests.
|
||||
Only then can backup eligibility safely depend on pending work.
|
||||
|
||||
Retries before version recording take another backup. After partial migration,
|
||||
that snapshot is not necessarily a pre-upgrade image. Two successful retries can
|
||||
prune the original snapshot. Filename timestamps have one-second resolution and
|
||||
can also collide on rapid retries. This existing risk requires a separate recovery
|
||||
design: database identity, durable attempt state, original-snapshot pinning,
|
||||
crash-safe retention, and restore tests. Never reuse by filename or target version.
|
||||
|
||||
## Requirement mapping
|
||||
|
||||
| Criteria | Mechanism |
|
||||
| --- | --- |
|
||||
| .1, .2, .3, .8 | Early bootstrap binding, atomic router handoff, existing launcher waits |
|
||||
| .4, .5 | Typed phase snapshot, structured timing, child-exit diagnostics |
|
||||
| .6, .7 | Separate process/worker cancellation ownership, schema/store admission barriers, sequential initialization, backup gate |
|
||||
|
||||
## Decisions
|
||||
|
||||
This extends the existing bootstrap architecture rather than creating a new
|
||||
persistence contract. See [database upgrade safety](../../../decisions/0008-db-upgrade-safety.md).
|
||||
|
||||
## Implementation plans
|
||||
|
||||
- [Large database startup](../../../plans/large-database-startup/plan.md)
|
||||
Reference in New Issue
Block a user