## Summary Speeds up **time-to-HTTP-ready** for `fn dashboard` and `fn serve` after the PostgreSQL cutover without reintroducing the historical 3s cwd-engine race that degraded webhooks. - **Dashboard store share (serve parity):** inject the factory-booted `TaskStore` as `externalTaskStore` so cwd `ensureEngine` does not open a second pool; share only when store root matches project working directory (multi-project safe). - **Serve multi-project:** stop awaiting `startAll()` before listen; await only the primary engine; background the rest + reconciliation. - **Defer non-route-critical engine work:** ordered OAuth (refresh → monitor), automation schedule syncs, and auto-merge **enqueue** after the engine handle is returnable. - **Critical-path merge status clear:** still clear stale `merging`/`merging-pr` before ready so manual merge is not blocked after crash. - **Serve `--paused`:** apply `enginePaused` before `ensureEngine`/`startAll` (dashboard ordering). - **Stop safety:** generation counter so deferred tails cannot resume after `stop()` clears `shuttingDown`. - **Phase timing:** shared `phaseTime` helper, factory substep logs, serve time-to-listen. Plan: `docs/plans/2026-07-14-001-feat-faster-startup-plan.md` ## Test plan - [x] `packages/engine` — `project-engine-manager.test.ts` (path-matched external store) - [x] `packages/engine` — `project-engine-deferred-startup.test.ts` (status clear, OAuth order, stop generation) - [x] `packages/cli` — `startup-phase.test.ts` - [x] `packages/cli` — `serve.test.ts` (60 tests, including `--paused`) - [ ] Local: warm `fn dashboard` / `fn serve` and compare `startup phase *` / `time-to-listen` logs - [ ] `pnpm smoke:boot` (real serve `/api/health` on ephemeral port) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Performance** * Improved dashboard and serve startup times, including faster time-to-listen and time-to-ready. * Moved non-essential background initialization off the critical startup path. * Parallelized dashboard service initialization where possible. * **Reliability** * Improved multi-project startup handling and project selection. * Prevented cross-project task-store sharing. * Added safer shutdown behavior for partially completed startup. * **Diagnostics** * Added startup phase timing logs to help identify performance bottlenecks. * **Tests** * Expanded coverage for deferred startup, shutdown, project isolation, and startup timing. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
30 lines
920 B
TypeScript
30 lines
920 B
TypeScript
/**
|
|
* Shared startup phase timing for CLI surfaces (dashboard, serve).
|
|
*
|
|
* FNXC:FasterStartup 2026-07-14-23:55:
|
|
* Operators and developers need wall-clock labels for each boot phase so
|
|
* time-to-listen regressions are attributable. Dashboard already had an
|
|
* inline phaseTime helper; serve and factory/engine paths need the same
|
|
* cheap pattern without inventing a separate metrics product.
|
|
*/
|
|
|
|
export type StartupPhaseLogger = (message: string, scope?: string) => void;
|
|
|
|
/**
|
|
* Time an async or sync startup phase and log `startup phase <label>: Nms`.
|
|
* Always logs in `finally` so failures still surface their duration.
|
|
*/
|
|
export async function phaseTime<T>(
|
|
label: string,
|
|
fn: () => Promise<T> | T,
|
|
log: StartupPhaseLogger,
|
|
scope = "startup",
|
|
): Promise<T> {
|
|
const t0 = Date.now();
|
|
try {
|
|
return await fn();
|
|
} finally {
|
|
log(`startup phase ${label}: ${Date.now() - t0}ms`, scope);
|
|
}
|
|
}
|