Repository navigation
Description
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 withDaemonReady { 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 inprobe_and_recover_channel, afterGrpcClient::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 movesgrpc_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):
- probes with
GetNodeunder a 5-second timeout; - on a timeout, calls
client.reconnect()(599); - rebinds
ProClientthroughapp.try_state(600-605); - 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
channelfield doc (170-176): "Pro-tier services" and "ProClient's separately-built channel"; - the
channel()doc (430-435): "Used byProClientso 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,TauriTestAppandhold_connect_mutex_and_socket_env, inapp-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 ownshutdown.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
ExtensionHookswithtry_state. Without it, does nothing. - Runs each hook as its own
tokio::spawned task, awaited undertokio::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
- Next to
session_token(~496), addlet extension_shutdown = shutdown_token_for_setup.child_token();. - In the startup task:
- clone the client before
start_token_stream_subscriptionmoves it; - directly after that call (and after Add a neutral daemon:data-plane-ready event and stop reloading on pro:tier-detected #3318's
daemon:data-plane-readyblock, if it has landed), callextensions::spawn_daemon_ready_tasks(&app_handle, grpc_for_extensions, &extension_shutdown);. Do not await it.
- clone the client before
- Leave the Pro probe block untouched; Delete the pro_* commands, ProClient, startup Pro probe and Pro rebind from core #3334 removes it.
app-lib/src/commands/nodes.rs, probe_and_recover_channel
- Move the body into
pub async fn probe_and_recover<R: tauri::Runtime>(app: &tauri::AppHandle<R>, client: &GrpcClient) -> bool. The command becomesOk(probe_and_recover(&app, client.inner()).await). - In the body, after the existing
ProClientrebind block (leave it for Delete the pro_* commands, ProClient, startup Pro probe and Pro rebind from core #3334) and before the final re-probe, addcrate::extensions::run_channel_rebuilt_hooks(app, client.channel().await).await;. - Add one generic sentence to the doc: after a rebuild, extensions' channel-rebuilt hooks run before the re-probe. Leave the Pro paragraph for Delete the pro_* commands, ProClient, startup Pro probe and Pro rebind from core #3334.
app-lib/src/services/grpc_client.rs
Reword the docs, naming no product:
- The
channelfield. It is held so extension services can ride the same HTTP/2 connection throughGrpcClient::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 beforereconnect()still points at the old connection, so a caller that caches one registersAppExtensions::on_channel_rebuilt.reconnect().probe_and_recover_channelruns the channel-rebuilt hooks after it.
Test harness
- In
app-lib/test-support, addTauriTestApp::connect_assembled(daemon, timeout, extensions: AppExtensions<MockRuntime>). It builds withassemble(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, andmod extension_hooks_test;intests/it/main.rs.
Out of scope
- Removing the Pro probe,
pro:tier-detectedorProClient::rebind(Delete the pro_* commands, ProClient, startup Pro probe and Pro rebind from core #3334). - The update source (Let an app built on core supply its update source through AppExtensions #3320).
- The daemon profile (Let an app built on core supply its DaemonProfile via AppExtensions::daemon_profile #3347).
Steps
- Run the CLAUDE.md startup sequence. Self-assign at pickup; the worktree is
issue-<N>-daemon-ready-and-channel-rebuilt-hooks. - Add the types, hook state, builder methods and the two runners, with their unit tests.
- Wire both hooks, and extract
probe_and_recover. - Reword the
GrpcClientdocs. - Add the seam tests.
- Run
bun run scripts/check-pro-boundary.ts, then lowerBASELINESto the printed counts. - Run
bun run quality:fix, thenbun 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_recoverreturntrue, and a hook's RPC on its channel completed before that. - With
AppExtensions::none(), startup andprobe_and_recover_channelbehave as before, and the existing Tauri-seam suite passes. - The
GrpcClientdocs no longer nameProClientor the Pro tier. -
bun run scripts/check-pro-boundary.tspasses, withBASELINESlowered to the exact new counts. The doc rewording should lowerproProtocolandproWording. 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_awaiteddaemon_ready_task_gets_the_app_the_client_and_a_child_shutdown_tokena_panicking_daemon_ready_task_does_not_stop_the_othersdaemon_ready_tasks_run_oncechannel_rebuilt_hooks_run_in_registration_ordera_hanging_channel_rebuilt_hook_times_out_and_the_next_still_runs(#[tokio::test(start_paused = true)])a_panicking_channel_rebuilt_hook_does_not_stop_recoveryhooks_are_a_no_op_without_extension_state
Source-text tests
- In
lib.rs, sliced torun:spawn_daemon_ready_tasks(comes afterstart_token_stream_subscription(and beforenodespaced unreachable after startup, and its statement isn't.awaited. - In
nodes.rs:run_channel_rebuilt_hooks(comes afterclient.reconnect().awaitand before the finalprobe(.
Seam tests in tests/it/extension_hooks_test.rs:
daemon_ready_task_reaches_the_daemon_on_the_shared_channel: aDatabaseServiceListRPC onready.grpc.channel()succeeds.a_wedged_channel_rebuild_runs_the_hooks_before_recovery_returns:- In a tempdir, bind a silent
UnixListenerthat accepts connections and never replies. - Under the connect mutex, with
NODESPACED_SOCKETpointing at the silent socket, build aconnect_lazy()client. - Point
NODESPACED_SOCKETat a realSpawnedDaemon. - Build the app with
connect_assembledand a hook that runsListon its channel and records the result. - Assert that
probe_and_recover(&handle, &client)returnstrue, and that the hook's RPC succeeded before it returned.
- In a tempdir, bind a silent
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_SOCKETwithEnvGuardin 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
Activity