Skip to content

Add daemon-ready tasks and a channel-rebuilt hook to AppExtensions #3307

Closed

Description

@mstomar125

Part of #3322 (Tauri extension points for the desktop app).

Summary

Add the two daemon-side Tauri hooks from ADR-082 (proposed) §3.4 and §3.5:

  • AppExtensions::on_daemon_ready(task). The task is spawned once per process with DaemonReady { app, grpc, shutdown }. It starts from core's startup task, after the daemon start attempt finishes and the watcher and token stream are wired. It is never awaited.
  • AppExtensions::on_channel_rebuilt(hook). Awaited in probe_and_recover_channel, after GrpcClient::reconnect() and before the re-probe.
    • Hooks run in registration order, and each has a 2-second timeout.
    • A timeout or panic is logged, and recovery continues.

This PR also rewords the GrpcClient channel docs so they describe extensions generically, and lowers the Pro-boundary baselines to match.

These are the seams #3334 uses to move the startup Pro probe and ProClient::rebind out of core.

Context

Startup task

In run()'s setup (lib.rs 488-665 at af8ddb6), in order:

  • ensure_daemon_running (502-536);
  • the managed GrpcClient (596) and its channel (597);
  • watcher::spawn (600);
  • start_token_stream_subscription (602-605), which moves grpc_client;
  • the Pro probe (607-635). It is awaited, with two sequential 2-second timeouts, so today it can delay the reachability check;
  • the reachability check that emits daemon-status: not_running (637-664).

Child tokens come from shutdown_token_for_setup.child_token(), created before the spawn (496-498).

#3318 adds a data-plane round trip in the same region. If it has merged, rebase on it.

Wedge recovery

probe_and_recover_channel (commands/nodes.rs 571-607):

  1. probes with GetNode under a 5-second timeout;
  2. on a timeout, calls client.reconnect() (599);
  3. rebinds ProClient through app.try_state (600-605);
  4. re-probes (606).

It is the only caller of reconnect() (grpc_client.rs 448-463). Anything that cached the old channel keeps riding the dead connection, which is why an extension that caches a client needs a rebuild hook.

GrpcClient docs

Two docs still name the Pro client:

  • the channel field doc (170-176): "Pro-tier services" and "ProClient's separately-built channel";
  • the channel() doc (430-435): "Used by ProClient so the Pro-tier service…".

Rewording them lowers proProtocol and proWording in scripts/check-pro-boundary.ts.

Harness

  • GrpcClient::connect_lazy() builds a client without I/O. It must be called inside a tokio runtime.
  • The seam harness is SpawnedDaemon, TauriTestApp and hold_connect_mutex_and_socket_env, in app-lib/test-support.

Scope

app-lib/src/extensions/

DaemonReady

pub struct DaemonReady<R: tauri::Runtime = tauri::Wry> {
    pub app: tauri::AppHandle<R>,
    pub grpc: GrpcClient,
    pub shutdown: CancellationToken,
}

grpc is a clone of the managed client. shutdown is a child of core's shutdown token.

Builder methods

pub fn on_daemon_ready<F, Fut>(self, task: F) -> Self
where
    F: FnOnce(DaemonReady<R>) -> Fut + Send + 'static,
    Fut: Future<Output = ()> + Send + 'static;

pub fn on_channel_rebuilt<F, Fut>(self, hook: F) -> Self
where
    F: Fn(tauri::AppHandle<R>, Channel) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = ()> + Send + 'static;

Hook state. pub(crate) struct ExtensionHooks<R>, which assemble always manages (empty for none()):

  • daemon_ready: Mutex<Vec<…>>, taken once;
  • channel_rebuilt: Vec<Arc<…>>.

#[doc(hidden)] pub fn spawn_daemon_ready_tasks<R>(app: &AppHandle<R>, grpc: GrpcClient, shutdown: &CancellationToken)

  • Takes the tasks, so a second call runs nothing.
  • Spawns each with tauri::async_runtime::spawn, each with its own shutdown.child_token().
  • Logs a panicking task with tracing::error!, without affecting the others.
  • Returns without awaiting.

pub const CHANNEL_REBUILT_HOOK_TIMEOUT: Duration = Duration::from_secs(2);

#[doc(hidden)] pub async fn run_channel_rebuilt_hooks<R>(app: &AppHandle<R>, channel: Channel)

  • Reads ExtensionHooks with try_state. Without it, does nothing.
  • Runs each hook as its own tokio::spawned task, awaited under tokio::time::timeout. The caller is always inside a runtime.
  • On a timeout, it aborts the task and logs. On a panic, it logs. Either way it continues with the next hook.

Rustdoc for both hooks (ADR-082 §3.4 and §3.5) covers:

  • when each runs;
  • that the daemon may be unreachable, and tasks must tolerate that;
  • that extensions ride GrpcClient::channel() and never dial the socket themselves;
  • that a hook caching a client or holding a stream uses on_channel_rebuilt;
  • that shutdown arrives through DaemonReady::shutdown.

Re-exports. If #3304 didn't, re-export tonic::transport::Channel and tokio_util::sync::CancellationToken from extensions.

app-lib/src/lib.rs, run()'s setup

app-lib/src/commands/nodes.rs, probe_and_recover_channel

app-lib/src/services/grpc_client.rs

Reword the docs, naming no product:

  • The channel field. It is held so extension services can ride the same HTTP/2 connection through GrpcClient::channel(). Opening a parallel channel to the same socket failed with "Service was not ready: transport error" after a stream was dropped.
  • channel(). It is for extension services. A clone handed out before reconnect() still points at the old connection, so a caller that caches one registers AppExtensions::on_channel_rebuilt.
  • reconnect(). probe_and_recover_channel runs the channel-rebuilt hooks after it.

Test harness

  • In app-lib/test-support, add TauriTestApp::connect_assembled(daemon, timeout, extensions: AppExtensions<MockRuntime>). It builds with assemble(tauri::test::mock_builder(), extensions).build(tauri::test::mock_context(tauri::test::noop_assets())), then manages the connected client.
  • Add app-lib/tests/it/extension_hooks_test.rs, and mod extension_hooks_test; in tests/it/main.rs.

Out of scope

Steps

  1. Run the CLAUDE.md startup sequence. Self-assign at pickup; the worktree is issue-<N>-daemon-ready-and-channel-rebuilt-hooks.
  2. Add the types, hook state, builder methods and the two runners, with their unit tests.
  3. Wire both hooks, and extract probe_and_recover.
  4. Reword the GrpcClient docs.
  5. Add the seam tests.
  6. Run bun run scripts/check-pro-boundary.ts, then lower BASELINES to the printed counts.
  7. Run bun run quality:fix, then bun run test:changed.

Acceptance criteria

  • Daemon-ready tasks:
    • run once per process, and are spawned, not awaited;
    • receive the app, the managed client, and a child shutdown token that is cancelled when core's token is;
    • a panicking task doesn't stop the others.
  • Channel-rebuilt hooks:
    • run in registration order, with the new channel;
    • a hanging hook times out after CHANNEL_REBUILT_HOOK_TIMEOUT, and the next hook still runs;
    • a panicking hook doesn't stop recovery.
  • Against a real daemon, a wedged channel makes probe_and_recover return true, and a hook's RPC on its channel completed before that.
  • With AppExtensions::none(), startup and probe_and_recover_channel behave as before, and the existing Tauri-seam suite passes.
  • The GrpcClient docs no longer name ProClient or the Pro tier.
  • bun run scripts/check-pro-boundary.ts passes, with BASELINES lowered to the exact new counts. The doc rewording should lower proProtocol and proWording. No count rises.
  • The Pro-mode compile check passes.
  • No product names or GitHub issue numbers in new code or docs.

Tests and verification

Unit tests in extensions/fixture_tests.rs (MockRuntime, a connect_lazy() client, no daemon):

  • daemon_ready_tasks_are_spawned_not_awaited
  • daemon_ready_task_gets_the_app_the_client_and_a_child_shutdown_token
  • a_panicking_daemon_ready_task_does_not_stop_the_others
  • daemon_ready_tasks_run_once
  • channel_rebuilt_hooks_run_in_registration_order
  • a_hanging_channel_rebuilt_hook_times_out_and_the_next_still_runs (#[tokio::test(start_paused = true)])
  • a_panicking_channel_rebuilt_hook_does_not_stop_recovery
  • hooks_are_a_no_op_without_extension_state

Source-text tests

  • In lib.rs, sliced to run: spawn_daemon_ready_tasks( comes after start_token_stream_subscription( and before nodespaced unreachable after startup, and its statement isn't .awaited.
  • In nodes.rs: run_channel_rebuilt_hooks( comes after client.reconnect().await and before the final probe(.

Seam tests in tests/it/extension_hooks_test.rs:

  • daemon_ready_task_reaches_the_daemon_on_the_shared_channel: a DatabaseService List RPC on ready.grpc.channel() succeeds.
  • a_wedged_channel_rebuild_runs_the_hooks_before_recovery_returns:
    1. In a tempdir, bind a silent UnixListener that accepts connections and never replies.
    2. Under the connect mutex, with NODESPACED_SOCKET pointing at the silent socket, build a connect_lazy() client.
    3. Point NODESPACED_SOCKET at a real SpawnedDaemon.
    4. Build the app with connect_assembled and a hook that runs List on its channel and records the result.
    5. Assert that probe_and_recover(&handle, &client) returns true, and that the hook's RPC succeeded before it returned.

Mutation-check each new test: break the code path it covers, confirm the test fails, then restore.

cargo test --lib -p nodespace-app-lib -- --test-threads=2
cargo build --bin nodespaced && NODESPACED_TEST_BIN="$PWD/target/debug/nodespaced" cargo test -p nodespace-app-lib --test it extension_hooks_test:: -- --test-threads=1
cargo clippy -p nodespace-app-lib -p nodespace-app-test-support --all-targets -- -D warnings -D clippy::unused-async
NODESPACE_PRO_SUPABASE_URL=https://example.invalid NODESPACE_PRO_ANON_KEY=dummy cargo check -p nodespace-app
bun run scripts/check-pro-boundary.ts
bun run test:changed

Dependencies

Risks and rollback

  • Recovery latency. Each hook can add up to 2 seconds to probe_and_recover_channel, which the frontend awaits. The timeout bounds it.
  • Env mutation in the wedge test. Hold the connect mutex for the whole test, and restore NODESPACED_SOCKET with EnvGuard in reverse order.
  • Duplicate work during the migration. Until Delete the pro_* commands, ProClient, startup Pro probe and Pro rebind from core #3334, core's own probe and rebind still run alongside any sync hook doing the same work. That is harmless, because the types are distinct.
  • Rollback: revert. Core registers no hooks.

Plan key: C2.3 (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