Skip to content

Daemon does not suppress same-origin WatchNodes echo — frontend guesses via content-comparison heuristic instead of authoritative client identity - #1691

Merged
malibio merged 4 commits into
mainfrom
worktree-issue-1689-daemon-echo-suppression
Jul 18, 2026
Merged

malibio merged 4 commits into
mainfrom
worktree-issue-1689-daemon-echo-suppression

Conversation

@malibio

@malibio malibio commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

Closes #1689

…euristic (closes #1689)

The daemon now tags each gRPC connection's writes with an x-ns-client-id
header, scoped through NodeService::with_client(), and WatchNodes drops any
broadcast whose source_client_id matches the subscribing connection's own id
before it ever reaches the client. This replaces isPlausibleOwnEcho and its
lastPersistedContent cache, a client-side content-comparison guess that
required three rounds of tightening (#1437, #1586, #1684) for the same
underlying reason ADR-026's C5 amendment already rejected a prior
client-side conflict heuristic: guessing instead of adjudicating is
inherently racy across an async round-trip.

Every database-sourced broadcast the frontend's skip-while-editing guard now
sees is guaranteed to be a genuine foreign write, so it always raises a
conflict notification instead of trying to classify content.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review Summary

This is a clean, well-scoped architectural fix that removes a client-side guessing heuristic in favor of daemon-side authoritative adjudication, exactly per ADR-026 C5's original intent. The implementation correctly threads client identity through the write path (route() → with_client()) and separately through the subscribe path (watch_nodes reading the header directly, pre-route()), which is the subtle part of this change and it's handled correctly. All four acceptance-criteria-adjacent concerns I was asked to dig into check out. Verified: cargo test -p nodespace-daemon (31/31 unit tests incl. 3 new echo-suppression tests, plus the real-transport integration test) and bun run test (166 files / 4095 tests) both pass; cargo clippy on daemon/proto is clean.

This is a net positive and I see no blockers.

Findings

Critical Issues

None.

Suggested Improvements

  • packages/daemon/src/services/node_service.rs (route(), watch_nodes): The write-scoping vs. subscribe-filtering split is correct but relies on every future RPC handler continuing to call route() first and every future streaming handler continuing to read the header before calling route(). This is well-documented in comments but not structurally enforced — a future handler that reads the client-id header only via route()'s side effect (rather than the standalone client_id_header() helper) would silently break. Not a blocker; worth a one-line note in NodeServiceImpl's doc comment steering future RPC additions toward the existing pattern, since this is the kind of thing that's easy to get subtly wrong by copy-pasting the wrong handler as a template.

  • packages/desktop-app/src-tauri/src/services/grpc_client.rs: client_id is threaded as a required MetadataValue<Ascii> parameter through DatabaseIdInterceptor::none()/for_id(), generated once in from_channel() via generate_client_id(). This is correct and the "one id per process/window lifetime" invariant is well-documented, but it's asserted only by convention (comments) — nothing stops a future call site from constructing a second DatabaseIdInterceptor with a freshly-generated id and accidentally splitting one window's writes/subscription across two identities, silently defeating suppression for that window. Given the existing GrpcClientInner.client_id field already centralizes this, the risk is low; flagging only because the failure mode (stale client-id ⇒ own writes stop being suppressed ⇒ old bug quietly returns) is exactly the class of bug this PR is designed to eliminate.

  • CLI foreign-write correctness (point c in the review brief): packages/cli/src/lib.rs has its own, separate DatabaseIdInterceptor (not the one modified in grpc_client.rs) that never stamps x-ns-client-id. That's correct today — CLI writes carry no source_client_id and are therefore never suppressed on any subscriber's stream, so a future second desktop window will correctly see them as foreign. Worth calling out explicitly since it means the CLI and desktop-app now have two independently-maintained interceptor structs with the same name in different crates; a future contributor adding client-id support to one is likely to assume it covers both. Consider a shared helper or at least a cross-reference comment between the two, though this is a pre-existing duplication (not introduced by this PR) and not a blocker.

Nitpicks

  • Nit: packages/daemon/src/services/node_service.rs::client_id_header boxes the Status error to satisfy clippy::result_large_err — reasonable and well-commented, matches the pattern in db_routing.
  • Nit: The new integration test's helper naming (window_a/window_b as clones of the same client) reads slightly oddly since they're literally the same tonic client value with different per-request headers, not independent connections — a one-line comment clarifying that a single tonic client is being reused to simulate two logical windows (since the header, not the transport connection, is what the daemon keys on) would help a future reader who expects two separate channels.

Verification of the four specific concerns raised

(a) write-scoping vs. subscribe-filtering split in route()/watch_nodes: Correct. watch_nodes reads x-ns-client-id via the standalone client_id_header() helper before calling self.route(&request), specifically because route()'s use of the same header is for scoping the returned node_service for writes, which watch_nodes never performs. Confirmed via the doc comments on both call sites and the passing watch_nodes_untagged_write_is_delivered_to_tagged_subscriber test, which specifically isolates this from the write-tagging path.

(b) removing serverConfirmedVersions/OCC-stash — no reintroduced OCC-defeat: Confirmed safe. The old stash was a client-side "avoid a conflict I predict is spurious" optimization that was only safe when the own-echo classification was correct — and that classification was the exact recurring bug (#1437/#1586/#1684). computeOccVersionForUpdate now always reads the local node's own version; the next UpdateNode RPC either succeeds normally or the daemon's real OCC check (isVersionConflict/x-version-conflict) surfaces a genuine conflict. This is strictly more correct than the old stash-based shortcut, not a regression — the daemon's version check was always the actual source of truth; the stash only ever changed whether a real conflict got preempted by (potentially wrong) client guesswork.

(c) CLI writes still treated as foreign by a future second desktop window: Confirmed. See "Suggested Improvements" above — the CLI's interceptor is untouched and never stamps the header, so its writes carry no source_client_id and are never suppressed.

(d) local-agent writes correctly NOT tagged with the frontend's own client id: Confirmed. packages/daemon/src/services/local_agent_service.rs calls self.inner.node_service directly — it never goes through the gRPC NodeServiceImpl::route() path or reads x-ns-client-id, so agent writes never carry a source_client_id and will always appear as foreign writes on any WatchNodes subscription, including the desktop window that spawned the agent session. This is the correct behavior per the issue's design (the frontend must still see agent-originated writes).

Acceptance Criteria (issue #1689)

All checked:

  • Daemon assigns a stable identity per client connection, threaded through NodeService::with_client() for writes
  • watch_nodes suppresses same-origin events so the frontend no longer content-compares
  • isPlausibleOwnEcho/lastPersistedContent deleted (not just reduced to a fallback)
  • No regression to genuine foreign-write conflict detection — verified via passing tests and the real-OCC-check argument above
  • Debounced-write echo race eliminated by construction (no content comparison exists anymore)
  • Companion ADR-026 amendment (nodespace-docs commit 12acef5, already pushed)
  • quality:fix clean (clippy clean, git diff --stat shows only the intended files)
  • test:all-equivalent passing (4095/4095 frontend, 31/31 + 1 integration daemon tests)

malibio and others added 2 commits July 18, 2026 10:27
…p interceptor cross-reference

- NodeServiceImpl doc comment now steers future unary vs. streaming RPC
  handlers toward the correct client-id plumbing pattern (route() for
  writes, standalone client_id_header() pre-route() for subscriptions).
- Cross-referenced the CLI's and desktop-app's independent, same-named
  DatabaseIdInterceptor structs so a future contributor doesn't assume
  updating one covers both; documented why the CLI's copy intentionally
  never stamps x-ns-client-id (one-shot process, never opens WatchNodes,
  so its writes should stay unsuppressed/foreign to any subscriber).
- Clarified the real-transport test's window-a/window-b naming: both are
  the same tonic client reused with different per-request headers, since
  the daemon keys suppression on the header, not the transport connection.

Skipped (non-blocking, from PR #1691 review):
- Making the "one client id per GrpcClient lifetime" invariant type-safe
  instead of convention-enforced — the field is already centralized in
  GrpcClientInner and the risk was called out as low; a structural fix
  would mean restructuring interceptor construction for marginal benefit
  in a single-process desktop app with exactly one GrpcClient instance.
- Merging the CLI and desktop-app DatabaseIdInterceptor structs into a
  shared helper — pre-existing duplication not introduced by this PR,
  out of scope; the reviewer explicitly called it "not a blocker."

Co-Authored-By: Claude <[email protected]>
The pre-push gate's real-transport ADR-048 tests caught a downstream
consequence of this change: optimistic_echo_race_test.rs drove writes
and watched for their echo through the SAME GrpcClient, which is now
architecturally impossible — the daemon suppresses a connection's own
writes on its own WatchNodes stream. Rewrote both tests to use two
independent GrpcClients (two TauriTestApp::connect calls against the
same daemon, matching the existing pattern in
cross_window_hierarchy_sync_test.rs), so a watching window observes a
genuinely foreign writer's broadcasts — the scenario that remains real
after this PR, per the issue's explicit multi-client carve-out.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator Author

Addressed Review Feedback

Addressed (4):

  • Added a steering note in NodeServiceImpl's doc comment for future RPC handlers on the write-scoping (route()) vs. subscribe-filtering (client_id_header() pre-route()) split.
  • Cross-referenced the CLI's and desktop-app's independent, same-named DatabaseIdInterceptor structs, documenting why the CLI's copy intentionally never stamps x-ns-client-id.
  • Clarified the real-transport test's window_a/window_b naming (same tonic client, different per-request headers — the daemon keys suppression on the header, not the transport connection).
  • Fixed grammar in the grpc_client.rs doc comment.

Skipped (2, both explicitly non-blocking per the review):

  • Making the "one client id per GrpcClient lifetime" invariant type-safe instead of convention-enforced — the field is already centralized in GrpcClientInner, the review called the risk low, and a structural fix is disproportionate for a single-process desktop app with exactly one GrpcClient instance.
  • Merging the CLI's and desktop-app's DatabaseIdInterceptor structs into a shared helper — pre-existing duplication not introduced by this PR, out of scope for this fix.

Also found and fixed during verification (not from the review, but surfaced by the pre-push gate's real-transport test suite): optimistic_echo_race_test.rs drove writes and watched for their echo through the same GrpcClient — a scenario the daemon-side suppression in this PR makes architecturally impossible (a connection's own writes are now never echoed back to it). Rewrote both tests in that file to use two independent GrpcClients (matching the existing pattern in cross_window_hierarchy_sync_test.rs), so the watching window observes a genuinely foreign writer's broadcasts instead — the scenario that remains real per the issue's multi-client carve-out.

Re-Review Decision

Decision: NO RE-REVIEW NEEDED

Rationale: All changes in this round were either (a) documentation-only steering comments, (b) a straightforward two-client rewrite of an existing test file following an established pattern already used elsewhere in the same test directory, or (c) grammar fixes. No production logic changed. Full test:all (166 files / 4095 tests) and the full nodespace-app ADR-048 Tauri-seam integration suite (incl. the rewritten optimistic_echo_race_test.rs) pass, and the pre-push gate's complete gRPC round-trip + Tauri-seam suite passed on push.

…t fix

PR #1693 landed on main while this branch was in review and independently
re-added a version-comparison guard (incomingIsNewer) to decideRemoteUpdate
to fix "conflict toast on every keystroke" — but via a different, still-
needed mechanism than this PR's daemon-side fix: #1693's guard protects
against stale echoes from the sync service (nodespace-sync writes
in-process via NodeService::with_client("sync-service"), a path this PR's
x-ns-client-id gRPC header does not cover), while this PR's daemon-side
suppression eliminates same-gRPC-connection echoes entirely.

Resolution: kept the incomingIsNewer version guard (still needed for
sync-service stale replays) but dropped the content-based
isPlausibleOwnEcho/stashVersion machinery it was paired with (unreachable
now that same-connection echoes never arrive at all). Updated
remote-update-policy.ts's doc comment and tests to describe both
mechanisms and why each is still necessary post-merge.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator Author

Merge conflict resolved

Rebasing on `main` surfaced a substantive conflict with #1693 (merged after this PR was reviewed): #1693 independently added a version-comparison guard (`incomingIsNewer`) to `decideRemoteUpdate` to fix the same visible symptom ("conflict toast on every keystroke"), but via a different mechanism targeting a different echo source — stale replays from `nodespace-sync`'s in-process writes (`NodeService::with_client("sync-service")`, ADR-027), which this PR's `x-ns-client-id` gRPC header does not cover (the sync service never opens a gRPC connection).

Resolution: kept #1693's `incomingIsNewer` version guard (still required — it's the only remaining protection against stale sync-service replays) but dropped the content-based `isPlausibleOwnEcho`/`stashVersion` machinery it was paired with, since that's now unreachable — same-gRPC-connection echoes never arrive at all after this PR's daemon-side fix. Updated `remote-update-policy.ts`'s doc comment and tests to describe both mechanisms and why each is independently still necessary.

Full `test:all` (166 files / 4098 tests) and the complete pre-push gate (gRPC round-trip + Tauri-seam ADR-048 suites) pass post-merge.

@malibio
malibio merged commit 6cf2e92 into main Jul 18, 2026
@malibio
malibio deleted the worktree-issue-1689-daemon-echo-suppression branch July 18, 2026 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Daemon does not suppress same-origin WatchNodes echo — frontend guesses via content-comparison heuristic instead of authoritative client identity

1 participant