Skip to content

Fix the sync echo guard and apply inbound changes - #332

Open
joei4cm wants to merge 1 commit into
CopilotKit:mainfrom
joei4cm:fix/139-sync-echo-detection
Open

joei4cm wants to merge 1 commit into
CopilotKit:mainfrom
joei4cm:fix/139-sync-echo-detection

Conversation

@joei4cm

@joei4cm joei4cm commented Dec 3, 2026

Copy link
Copy Markdown

Summary

Echo detection in the sync engine could never fire, and inbound changes from a tracker were dropped while the engine reported success. This fixes the guard, makes the hash direction-independent, records the echo before the enqueue, and applies or explicitly reports every inbound change.

Closes #139

What was wrong

1. The guard queried the reverse direction. A push is recorded with the plugin it was pushed to as the target (source='outpost', target=<tracker>), but shouldSync looked for the reverse pair (source=<tracker>, target='outpost') — a pair no push has ever produced. The guard was structurally unreachable, not merely wrong.

2. targetPlugin was part of payloadHash. The same logical change hashed differently depending on where it was going, so even a corrected query could not have matched a change coming back from a tracker.

3. The check ran at enqueue time but the record was written at execution time. Two changes arriving inside that window both passed the check, producing duplicate TRACKER_SYNC jobs.

4. Inbound changes were discarded and reported as success. onWebhookReceived fanned the change out to the other trackers but never applied it to the local ticket, and SyncResult.success stayed true — a sync that did nothing looked like a sync that worked.

5. computeHash stripped nested keys. The top-level key list was passed as the JSON.stringify replacer, and an array replacer applies to every nested object too, so any nested key outside the top-level list was dropped and distinct changes could collide.

What changed

  • EchoGuard.shouldSync now matches the recorded direction (entityId + targetPlugin + payloadHash + a status of pending or success within the echo window). A failure deliberately does not block, so a failed push stays retryable.
  • computeHash is direction-independent and covers nested keys. The payload is serialised with its keys sorted at every level, so the hash depends on content only. undefined is dropped, so an absent field and an unset field hash the same.
  • The echo record is written before the enqueue. The pending row exists before the job does, so two rapid changes cannot both pass the guard. If the enqueue fails, the row is resolved to failure instead of being left saying it is in flight. A push therefore leaves one row behind, not two.
  • Inbound changes are applied to the local ticket. status_change, close, priority_change, assignee_change (mapped through the plugin's mapUserToMember) and comment (recorded as a Message) are applied. A change the local model cannot express — label_change, new_issue, an unknown action, an assignee with no matching team member — is reported as unsupported: … in SyncResult.errors, and success becomes false. Nothing is dropped silently any more.
  • The echo check moved to the fan-out. It now runs in SyncEngine.onTicketChange for inbound changes, and the check in the TRACKER_SYNC handler was removed. Checking there was wrong twice over: it looked for the reverse direction, and once fixed it would fire on the very record the job itself created, so the push would never happen. A job is a promise to push, and the handler keeps it — the operator-facing force-sync route enqueues jobs directly and expects every one of them to push.
  • The guard applies to inbound changes only. It compares the content of two changes, so applying it to local changes would drop a legitimate "reopen and resolve again" inside the echo window. A local change is always pushed.

Tests

  • echo-guard.test.ts — asserts the query matches the direction pushes are recorded in, that a failure does not block the retry, that pending is create-if-absent and resolves the existing row rather than adding a second one, and that computeHash is key-order independent and covers nested keys.
  • sync-engine.test.ts — the pending record is written before the job is enqueued (asserted with invocationCallOrder), an enqueue failure resolves the row to failure, both targets get the same hash, inbound status/close/priority/assignee/comment changes are applied locally, label_change and an unmappable assignee are reported as unsupported, and a change coming back from a tracker is not fanned out again.
  • sync-integration.test.ts — the end-to-end loop case: a push recorded for a tracker is recognised as an echo and the fan-out is blocked.
  • tracker-sync.test.ts — the handler pushes what the job says, without consulting an echo guard.

pnpm test (5015 tests), pnpm typecheck, pnpm lint and the prettier format check all pass.

Note for reviewers

The inbound application covers what the local model can express. label_change is reported as unsupported because Outpost tickets have no labels field; adding one is a separate schema change and out of scope here.

🤖 Generated with Claude Code

The echo guard could never fire. A push is recorded with the plugin it
was pushed to as the target, but shouldSync queried the reverse pair,
which no push has ever produced. The target plugin was also part of
the payload hash, so the same change hashed differently in each
direction and could never have matched either. The check ran at enqueue
time while the record was written at execution time, so two rapid
changes both passed it. Inbound webhook changes were never applied to
the local ticket and the result still reported success. And computeHash
passed the top-level key list as the JSON.stringify replacer, which
strips nested keys and lets distinct changes collide.

The guard now matches the recorded direction and hashes the change's
content only. The pending record is written before the enqueue and
resolved on completion, so one push leaves one row and two rapid
changes cannot both pass. Inbound changes are applied where the local
model supports it, and reported as unsupported otherwise. The echo check
moved to the fan-out: checked in the TRACKER_SYNC handler it would
fire on the very record the job created, so the push would never
happen.

Refs CopilotKit#139

Co-Authored-By: Claude Code <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

Sync engine: echo detection can never fire, and inbound changes are silently discarded

1 participant