Skip to content

Add start, onDatabaseActivated and debugDump extension lifecycle hooks #3313

Closed

Description

@mstomar125

Summary

Add the lifecycle hooks from ADR-082 §2.5 (proposed), and call them from core at their final positions. #3329 then only has to delete the Pro calls.

  • lifecycle.start()
    • Runs once per webview load, when the app shell mounts.
    • Runs only when the Tauri bridge is present.
    • May return a cleanup, either synchronously or through a promise.
  • lifecycle.onDatabaseActivated(databaseId)
    • Synchronous.
    • Fires after the active database is committed and the previous database's caches have been evicted.
    • Fires on the first resolution in load() and on every switchTo().
    • The extension serializes its own async work.
  • A top-level debugDump(), included in the debug channel's store dump.

The existing Pro calls stay. With no extension hooks registered, behaviour is unchanged.

Context

Paths are under packages/desktop-app/src/lib/. Line numbers are at af8ddb61.

components/layout/app-shell.svelte, onMount (from L212):

  • The handles are declared around L240-250, including cleanupProSync at L248.
  • The Tauri-bridge branch (L252-255) calls proSync.start() (L256-263).
  • The cleanup returned at L572 calls cleanupProSync?.() (L605).

stores/database.svelte.ts:

  • load() (L169):
    • In browser mode it returns early (L177-184).
    • On the first resolution, the Tauri path awaits set_active_database, commits activeDatabaseId, and calls pinWindowDatabase and activateProSync(resolved) (L234).
    • It then calls evictAndReloadActiveDatabase() for a non-default database, or refreshDatabaseSettings() for the default one (L237-247).
  • switchTo() (L424):
    • After set_active_database and the commit, it calls activateProSync(id) (L455), then evictAndReloadActiveDatabase() (L457), then the workspace reset (clearAllTabs(), L463).
    • A superseded switch returns at the seq checks.
  • evictAndReloadActiveDatabase() (L489) is synchronous:
    • It calls sharedNodeStore.clearAll() first (L497). That drops every pin and bumps the epoch.
    • It then runs the per-database reloads and membership.invalidateForDatabaseSwitch() (L520).

services/debug-channel.ts:

  • captureStoreDump() (L124) has one try/catch block per store, using dynamic imports.
  • proSync is dumped at L156-161.
  • The PII note is at L119-121.

Why the hook fires after eviction:

Scope

1. Types, in plugins/ui-extensions.ts

export interface ExtensionLifecycle {
  start?(): void | (() => void) | Promise<void | (() => void)>;
  onDatabaseActivated?(databaseId: string): void;
}

NodespaceExtension gains lifecycle?: ExtensionLifecycle and debugDump?: () => unknown.

2. New src/lib/plugins/extension-lifecycle.ts

Plain TypeScript. It imports only ./ui-extensions and the logger. It must never import ui-extensions.svelte.ts; that keeps the transitional Pro import out of the database store's import graph.

startExtensions(): () => void

  • Calls each registered extension's start(), in registration order.
  • A throw or rejection is logged, and the other extensions still start.
  • Collects the cleanups and returns a disposer.
  • The disposer runs the cleanups in reverse registration order, each in its own try/catch.
  • If a start() resolves after the disposer has run, its cleanup runs immediately.

notifyDatabaseActivated(databaseId: string): void

  • Calls each onDatabaseActivated synchronously, in registration order, each in its own try/catch.
  • If a hook returns a promise anyway, its rejection is caught and logged. It never goes unhandled.

collectExtensionDebugDumps(): Record<string, unknown>

  • Returns { [extension id]: debugDump() }.
  • A throw is recorded as the string error: <message>.

Module doc. Document the exact firing points listed in §3. They are part of the versioned extension API (see ADR-082's compatibility policy).

3. Wiring

This is additive: every existing Pro call stays.

app-shell.svelte:

  • Declare let stopExtensions: (() => void) | null = null; with the other handles.
  • In the Tauri-bridge branch, right after the proSync.start() chain, add stopExtensions = startExtensions();.
  • In the cleanup, add stopExtensions?.(); next to cleanupProSync?.().

database.svelte.ts:

  • In load(): after the restore if/else (L237-247), inside the first-resolution block, add if (resolved !== null) notifyDatabaseActivated(resolved);. It runs after evictAndReloadActiveDatabase() for a non-default database, and after refreshDatabaseSettings() for the default one.
  • In switchTo(): add notifyDatabaseActivated(id); right after this.evictAndReloadActiveDatabase(); and before clearAllTabs().
  • The hook does not fire:
    • in browser dev mode, where there is no Tauri bridge (the same rule as start);
    • for a superseded switch;
    • for a later load() that keeps the current selection.

debug-channel.ts, captureStoreDump():

  • After the existing blocks, dynamically import the lifecycle module, and set stores.extensions to the dumps.
  • Serialize one extension at a time with toDumpable. A value that cannot be serialized becomes error: <message> for that extension only.
  • Keep the proSync block; Stop driving Pro sync lifecycle from the app shell, database store and debug channel #3329 removes it.
  • Extend the PII note: extension dumps may also contain user content and personal data.

4. No databaseCachesCleared hook

ADR-082 defines no such hook, so this change does not add one.

5. Fixture

src/tests/fixtures/test-extension/ gains, each switchable by an option:

  • a lifecycle with:
    • a recording start that returns a cleanup;
    • an optionally deferred start;
    • a recording onDatabaseActivated;
  • a debugDump.

6. Pro-boundary ratchet

Paste the printed BASELINES. No increase is expected.

Out of scope

Steps

  1. Start up as core's CLAUDE.md describes: a worktree named issue-<N>-extension-lifecycle, and self-assign at pickup.
  2. Add the types and the lifecycle module, with unit tests.
  3. Wire the app shell, the database store and the debug channel.
  4. Add the store and debug-channel tests, update the ratchet, and run the verification.

Acceptance criteria

Start and cleanup

  • startExtensions starts extensions in registration order and cleans up in reverse, including a start() that resolves after dispose.
  • A failing start or cleanup does not affect the others.

Database activation

  • onDatabaseActivated fires once per committed activation: on the first resolution in load() (both branches) and on each switchTo().
  • It never fires in browser dev mode, for a superseded switch, or for a later load().
  • In switchTo(), it fires after sharedNodeStore.clearAll() and before clearAllTabs().
  • In load() restoring a non-default database, it fires after clearAll().
  • A throwing hook never aborts the switch or load().

Debug dump

  • captureStoreDump includes extensions[<id>].
  • A throwing or unserializable dump does not break the store dump.

No behaviour change

  • With no hooks registered, behaviour is unchanged: database.test.ts, debug-channel.test.ts and test:free-users pass.
  • bun run test:changed passes, and quality:fix is clean.
  • bun run scripts/check-pro-boundary.ts exits 0, and BASELINES equal the printed counts.

Tests and verification

New src/tests/plugins/extension-lifecycle.test.ts, covering:

  • start order, and cleanup in reverse;
  • a late async start is cleaned up immediately;
  • a throwing start, a rejecting start and a throwing cleanup;
  • hook order;
  • a throwing hook;
  • a hook that returns a rejected promise is logged, not unhandled;
  • dumps are keyed by extension id, with error strings for failures;
  • every function is a no-op when nothing is registered;
  • a source assertion that app-shell.svelte calls startExtensions() and stopExtensions?.(), using the same approach as shared-node-store-pin-guard.test.ts.

src/tests/stores/database.test.ts: a new describe, 'extension lifecycle hooks'.

Setup:

  • Register the fixture with onDatabaseActivated: vi.fn(), and unregister it in afterEach.
  • Reuse the file's existing clearAll and clearAllTabs mocks.

Cases:

  • load() restoring a non-default database: the hook fires once, with that database's id, and clearAll.mock.invocationCallOrder[0] is lower than the hook's.
  • load() on the default database: the hook fires once, and clearAll is not called.
  • switchTo(id): the hook fires once, with id, after clearAll and before clearAllTabs.
  • A superseded switch: only the winning id fires.
  • Browser dev mode: the hook does not fire.
  • A throwing hook: activeDatabaseId is still committed, and clearAllTabs still runs.
  • A second load(): the hook does not fire again.

src/tests/services/debug-channel.test.ts:

  • extensions['test-extension'] holds the fixture's dump.
  • A throwing dump and a circular dump each become an error string.
bun run test:changed
bun run --cwd packages/desktop-app test src/tests/plugins src/tests/stores/database.test.ts src/tests/services/debug-channel.test.ts
bun run --cwd packages/desktop-app test:free-users
bun run --cwd packages/desktop-app quality:fix
bun run scripts/check-pro-boundary.ts

Manual check in dev:tauri: launch, switch databases A → B → A, and close the window. Everything behaves as before.

Dependencies

Risks and rollback


Plan key: C3.4 (pro-boundary DAG).

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

Labels

pro-boundary

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions