Description
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 everyswitchTo(). - 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
cleanupProSyncat 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, commitsactiveDatabaseId, and callspinWindowDatabaseandactivateProSync(resolved)(L234). - It then calls
evictAndReloadActiveDatabase()for a non-default database, orrefreshDatabaseSettings()for the default one (L237-247).
switchTo()(L424):- After
set_active_databaseand the commit, it callsactivateProSync(id)(L455), thenevictAndReloadActiveDatabase()(L457), then the workspace reset (clearAllTabs(), L463). - A superseded switch returns at the
seqchecks.
- After
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).
- It calls
services/debug-channel.ts:
captureStoreDump()(L124) has one try/catch block per store, using dynamic imports.proSyncis dumped at L156-161.- The PII note is at L119-121.
Why the hook fires after eviction:
- The Pro extension will call
databases.refreshDatabaseSettings()from this hook (Stop driving Pro sync lifecycle from the app shell, database store and debug channel #3329). - Before
clearAll(), that refresh's pin would be wiped, and the epoch check would drop the fetched node. - Moving the hook later, once Publish the extension host API: services, ui and testing entries, version policy #3315 has published the API, would be a breaking change to its timing. So it lands in its final place now.
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
onDatabaseActivatedsynchronously, 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, addstopExtensions = startExtensions();. - In the cleanup, add
stopExtensions?.();next tocleanupProSync?.().
database.svelte.ts:
- In
load(): after the restoreif/else(L237-247), inside the first-resolution block, addif (resolved !== null) notifyDatabaseActivated(resolved);. It runs afterevictAndReloadActiveDatabase()for a non-default database, and afterrefreshDatabaseSettings()for the default one. - In
switchTo(): addnotifyDatabaseActivated(id);right afterthis.evictAndReloadActiveDatabase();and beforeclearAllTabs(). - 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.
- in browser dev mode, where there is no Tauri bridge (the same rule as
debug-channel.ts, captureStoreDump():
- After the existing blocks, dynamically import the lifecycle module, and set
stores.extensionsto the dumps. - Serialize one extension at a time with
toDumpable. A value that cannot be serialized becomeserror: <message>for that extension only. - Keep the
proSyncblock; 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.
onDatabaseActivatedfires synchronously right after the eviction, so an extension clears its own per-database caches there.- For the Pro extension, that means calling
membership.invalidateForDatabaseSwitch()(Stop driving Pro sync lifecycle from the app shell, database store and debug channel #3329). - On the default-database branch of
load(), nothing was evicted. Invalidating an empty cache there is harmless.
5. Fixture
src/tests/fixtures/test-extension/ gains, each switchable by an option:
- a
lifecyclewith:- a recording
startthat returns a cleanup; - an optionally deferred
start; - a recording
onDatabaseActivated;
- a recording
- a
debugDump.
6. Pro-boundary ratchet
Paste the printed BASELINES. No increase is expected.
Out of scope
- Moving these onto the new hooks, and deleting the direct calls (Stop driving Pro sync lifecycle from the app shell, database store and debug channel #3329):
proSync.start();activateProSyncandproSyncSettled();- the membership invalidation;
- the
refreshDatabaseSettings()calls; - the
proSyncdump.
- The
pro:tier-detectedreload (Add a neutral daemon:data-plane-ready event and stop reloading on pro:tier-detected #3318).
Steps
- Start up as core's
CLAUDE.mddescribes: a worktree namedissue-<N>-extension-lifecycle, and self-assign at pickup. - Add the types and the lifecycle module, with unit tests.
- Wire the app shell, the database store and the debug channel.
- Add the store and debug-channel tests, update the ratchet, and run the verification.
Acceptance criteria
Start and cleanup
-
startExtensionsstarts extensions in registration order and cleans up in reverse, including astart()that resolves after dispose. - A failing
startor cleanup does not affect the others.
Database activation
-
onDatabaseActivatedfires once per committed activation: on the first resolution inload()(both branches) and on eachswitchTo(). - It never fires in browser dev mode, for a superseded switch, or for a later
load(). - In
switchTo(), it fires aftersharedNodeStore.clearAll()and beforeclearAllTabs(). - In
load()restoring a non-default database, it fires afterclearAll(). - A throwing hook never aborts the switch or
load().
Debug dump
-
captureStoreDumpincludesextensions[<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.tsandtest:free-userspass. -
bun run test:changedpasses, andquality:fixis clean. -
bun run scripts/check-pro-boundary.tsexits 0, andBASELINESequal 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.sveltecallsstartExtensions()andstopExtensions?.(), using the same approach asshared-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 inafterEach. - Reuse the file's existing
clearAllandclearAllTabsmocks.
Cases:
load()restoring a non-default database: the hook fires once, with that database's id, andclearAll.mock.invocationCallOrder[0]is lower than the hook's.load()on the default database: the hook fires once, andclearAllis not called.switchTo(id): the hook fires once, withid, afterclearAlland beforeclearAllTabs.- A superseded switch: only the winning id fires.
- Browser dev mode: the hook does not fire.
- A throwing hook:
activeDatabaseIdis still committed, andclearAllTabsstill 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
- Adopt ADR-082's contribution contract: ids, when() predicates, keyed and isolated outlets #3310: the registry contract and the fixture.
Risks and rollback
- Timing is part of the API. The firing points are documented in the module and pinned by tests. Changing them after Publish the extension host API: services, ui and testing entries, version policy #3315 would be a major version change.
- Pro activation moves after the eviction once Stop driving Pro sync lifecycle from the app shell, database store and debug channel #3329 uses the hook. It stays in the same synchronous turn of
switchTo(), and Stop driving Pro sync lifecycle from the app shell, database store and debug channel #3329's own tests cover the serialization. - Rollback. Revert the PR. With no hooks implemented, the change is purely additive.
Plan key: C3.4 (pro-boundary DAG).
🤖 Generated with Claude Code
Activity
mstomar125 commentedon Sep 30, 2026
Completed in #3398 (merged). Auto-close did not fire; closing manually.