Skip to content
This repository was archived by the owner on Sep 30, 2026. It is now read-only.

fix(server): dedup re-sent user submissions via a stable submission nonce - #14

Merged
yjuyjuy merged 1 commit into
masterfrom
fm/duplicate-message-dedup
Aug 12, 2026
Merged

yjuyjuy merged 1 commit into
masterfrom
fm/duplicate-message-dedup

Conversation

@yjuyjuy

@yjuyjuy yjuyjuy commented Aug 12, 2026

Copy link
Copy Markdown
Owner

Problem

On a busy shared server, a single user message can be injected many times (the ~16 duplicate user turns seen in rewind history). Root cause: no inbound-message idempotency plus the client busy-requeue loop. When a queued follow-up races a still-running turn, the server rejects it with Already processing a message; the client's anti-loss recovery (issue 1jehuang#391) re-queues the same content and re-adopts a running-turn state, so each accepted re-dispatch appends another identical user turn. Full diagnosis: server rejects before append, so the amplification is client re-sends with no idempotency key, not a server double-append.

Fix

Server-side idempotency, mirroring the existing swarm-spawn request_nonce dedup pattern (wire.rs / handle_comm_spawn):

  • Protocol (crates/jcode-protocol/src/wire.rs): Request::Message gains an optional submission_nonce (backward-compatible default None; older clients keep prior behavior, skip_serializing_if).
  • Server (crates/jcode-app-core/src/server/message_nonce_dedup.rs + client_lifecycle.rs): each connection owns a bounded FIFO MessageNonceTracker. A duplicate nonce is acknowledged with Done and never appended or run again. The nonce is recorded only once a submission passes the reload and busy gates and is actually accepted, so a busy-rejected send (which appends nothing) does not poison the set. The duplicate check sits at the top of the message arm, short-circuiting the busy-recovery amplifier itself.
  • Client (crates/jcode-tui): begin_remote_send resolves a stable nonce per logical submission and preserves it across every busy/disconnect re-send of the same content - the in-flight pending message, the busy-recovery re-queue stash (queue_recovery.rs), and the reload save/restore path (state_ui.rs). Genuinely new content mints a fresh nonce; an empty reminder poke carries none.
  • ACP bridge (src/cli/acp.rs): stamps each single-shot prompt with a fresh nonce.

Tests

  • MessageNonceTracker unit tests: absent nonce never dedups, record/detect, idempotent double-record, FIFO eviction beyond capacity.
  • Server regression test duplicate_submission_nonce_appends_exactly_one_user_turn: an accepted submission appends its user turn, and a same-nonce re-send is detected as a duplicate and appends nothing.
  • Extended wire roundtrip tests (both protocol crates) covering the new field.

Validation

  • cargo build + cargo clippy clean for jcode-protocol, jcode-app-core, jcode-tui, and the jcode binary.
  • cargo test: nonce-tracker + regression tests pass; 56 protocol roundtrip tests pass.

MVP is the server nonce guard; the client nonce-preservation makes the guard actually engage across the retry storm. Not the money/model hot path.

…once

On a busy shared server a client that sends a queued follow-up while a turn
is still running is rejected with 'Already processing a message'. The
client's anti-loss recovery (issue 1jehuang#391) re-queues the same content and
re-adopts a running-turn state, so a transient busy race turns one logical
submission into several accepted, appended user turns - the ~16 duplicate
user turns observed in rewind history.

Add server-side idempotency, mirroring the existing swarm-spawn request_nonce
dedup pattern:

- Protocol: Request::Message gains an optional submission_nonce
  (backward-compatible default None; older clients keep prior behavior).
- Server: each connection keeps a bounded per-connection MessageNonceTracker.
  A duplicate nonce is acknowledged with Done and never appended or run a
  second time; the nonce is recorded only once a submission passes the reload
  and busy gates and is actually accepted.
- Client (TUI): begin_remote_send resolves a stable nonce for each logical
  submission and preserves it across every busy/disconnect re-send of the
  same content (in-flight pending message, the busy-recovery re-queue stash,
  and the reload save/restore path), so retries reuse the original nonce.
  Genuinely new content mints a fresh nonce; an empty reminder poke carries
  none.
- ACP bridge: stamps each single-shot prompt with a fresh nonce.

Tests: MessageNonceTracker unit tests (absent nonce never dedups, record/
detect, idempotent record, FIFO eviction), a server regression test proving
a duplicate-nonce re-send appends exactly one user turn, and extended wire
roundtrip tests covering the new field.

cargo build + clippy clean for jcode-protocol, jcode-app-core, jcode-tui, and
the jcode binary.
@yjuyjuy
yjuyjuy merged commit 25b3b52 into master Aug 12, 2026
4 of 11 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants