Skip to content

Deduplicate Slack mirror replies and reuse its client - #331

Open
joei4cm wants to merge 1 commit into
CopilotKit:mainfrom
joei4cm:fix/152-slack-mirror-delivery-dedup
Open

joei4cm wants to merge 1 commit into
CopilotKit:mainfrom
joei4cm:fix/152-slack-mirror-delivery-dedup

Conversation

@joei4cm

@joei4cm joei4cm commented Dec 3, 2026

Copy link
Copy Markdown

What changed

#150 made each ticket's Slack thread idempotent, but replies were not: when Slack accepted a reply and the queue retried before it could record success, the same message could be posted again. Long replies could also be cut inside an emoji, and each job constructed a new Slack WebClient.

  • Added an additive SlackMirrorDelivery table keyed uniquely by source message. A worker atomically claims the row before posting, records delivery after Slack accepts it, and skips already delivered messages. An unexpired claim prevents concurrent posts. If another execution still owns the claim, the queue honors a minimum retry delay until the lease expires. Claims use the database clock and Slack requests have a timeout; a stalled execution can still outlive its lease.
  • Added a nullable one-to-one relation with Message; the migration only creates a new table, its unique index, and its foreign key, so existing rows and older workers remain compatible during rollout.
  • Changed truncation to count grapheme clusters, preserving emoji and other astral characters.
  • Cache one Slack WebClient per token. A token change replaces the cached client.

Delivery guarantee

Slack and Postgres cannot commit the message post and the delivery marker in one transaction. A process crash, or an ambiguous post failure, after Slack accepts a reply but before the database marker commits can therefore still lead to a duplicate after the 120-second claim lease expires. The ledger prevents normal queue retries, concurrent attempts, and retries after the marker is saved; it does not claim exactly-once delivery across those two systems.

Validation

  • Prisma schema validation and client generation passed; all migrations applied to an empty PostgreSQL 16 database, and prisma migrate diff reported no drift.
  • Outpost build and typecheck passed.
  • Queue tests: 16 files, 573 tests passed, including reply dedup, active-claim retry scheduling, Unicode truncation, and client reuse.
  • Outpost ESLint completed with no errors (existing warnings remain).
  • Prettier check and git diff --check passed.

Refs #152

🤖 Generated with Claude Code

The Slack mirror recorded a ticket's thread identity, but not which
replies it had already posted. A retried reply job could therefore post
the same message again. Add a per-message delivery ledger with an
atomic claim lease, mark delivery after Slack accepts the post, and skip
retries for messages already delivered.

The claim is taken and judged against the database clock, so lease
expiry does not depend on worker clock skew. A handler can now ask for
a minimum delay before its next attempt, and the worker honours it
when it is longer than the exponential backoff; that is how a job waits
out a lease held by another execution. A permanent Slack rejection
releases the claim so the message is not stranded; an ambiguous failure
keeps it, because Slack may already have accepted the post.

Slack requests get a timeout and the WebClient is cached per token, so
a stalled request cannot outlive the job without bound and successive
jobs reuse one client.

Truncation now counts grapheme clusters rather than UTF-16 code units,
so an emoji is never cut in half.

The ledger is a new additive table related to Message, so rolling
workers can continue using the existing schema. The delivery marker
and the Slack post cannot be committed atomically; a process crash in
between can still duplicate a post after the lease expires.

Refs CopilotKit#152

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.

1 participant