Provider subscription replies duplicate in Relay and do not write back to GitHub

Merged
AG
2mo ago
+807-46

Replay-key message writes and verify subscription provisioning

This is a partial fix for #1823. It does not fix the GitHub writeback
failure and does not close the ticket
— see "Not fixed here" below.

Channel posts and thread replies bypassed MCP request replay protection, so a
transport replay of one JSON-RPC request could create duplicate Relay rows.
post_message and reply_to_thread now use request replay and accept an
idempotency_key, matching spawn, add_agent, and send_dm. Native host
tools key on the model's toolCallId, which the AI SDK host now passes to
execute. Both surfaces resolve the acting agent client before scheduling the
write, so a register_agent that moves the session default cannot reroute or
drop an in-flight write.

Argument content deliberately does not participate in any key. Two deliberate
writes of the same text to the same target are two operations, and collapsing
them would silently drop one — the opposite failure to the reported one.

integration subscribe validates --events against the engine's own
SubscribableEventTypeSchema (now re-exported as SUBSCRIBABLE_EVENT_TYPES)
rather than a local subset, so supported events such as action.completed and
message.updated still provision, including through an explicit
--bridge-url. It de-duplicates names, reads back persisted coverage before
binding, and rolls back incomplete, inactive, or mismatched subscriptions.
GitHub setup reports missing/unknown identity authorization instead of
promising writeback. --list separates outbound configuration from delivery
state, reporting deliveryStatus: 'unavailable' explicitly so an absent value
is never read as "nothing failed", and labels inferred inbound activity. SDK
subscription responses share event normalization, and subscription prompts
clarify replying versus initiating provider actions.

Not fixed here

The writeback actor is relayfile-cloud's ingress, reached through the
per-channel writeback URL this CLI only configures. Nothing in this repo
performs the provider POST, and the installed @relayfile/client exposes no
delivery-attempt ledger read and no retry operation; the pinned
@relaycast/types subscription schema declares configuration only. Incident
credentials and the Relayfile daemon are also unavailable in this run.

Retained for the companion change, in order: read the incident binding and
subscription and inspect the cloud writeback ingress; route ingress through the
ops ledger keyed on the originating Relay message ID if it bypasses it; expose
durable failed/dead-lettered status and retry through the control-plane client
(replacing deliveryStatus: 'unavailable'); verify retry after both a provider
failure and a lost successful response with one resulting GitHub comment; then
build the inbound → assigned agent wake → one Relay reply → one GitHub
writeback test against those verified contracts. Existing broker reconnect
coverage is cited, not rerun (Cargo unavailable).

See investigation evidence for
verified contracts, limitations, and the required companion verification.

Review follow-ups

  • The review's switch-default reproduction now passes as written: the
    in-flight write stays with Alice and the next write goes to Bob.
  • Its explicit-alias reproduction intentionally no longer holds. It asserted
    that as: 'alice' joins a pending default-Alice write. Content-keyed
    coalescing is gone, so both spellings of one identity now behave
    identically — as two independent writes — which is the consistency the
    finding asked for. messaging-tools.protocol.test.ts covers implicit vs.
    explicit equivalence, a default-identity change mid-flight, independent
    equal-content writes, and keyed replay.

Checks

Relayflow ran this repository's checks (.relayflow/check.sh) and they passed.

What ran (.relayflow/check.sh)
#!/bin/sh
# Fresh-machine CI check for this repo, mirroring the `test` job in
# .github/workflows/test.yml (plus the build that `pretest` triggers).
set -e

cd "$(dirname "$0")/.."

# CI pins Node 22.14.0 via actions/setup-node (.nvmrc says 22); package.json
# requires >=22. We use whatever Node this machine provides.
export AGENT_RELAY_TELEMETRY_DISABLED=1

# 1. Install dependencies exactly as CI does.
npm ci

# CI adds this because npm's optional-dependency resolution can miss the
# platform rollup binary that vitest needs; failure is non-fatal there too.
npm install --no-save rollup || true

# 2. Build before testing. `npm test` runs this through its `pretest` hook;
#    doing it explicitly keeps the build a visible, separate failure point.
#    `build:rust` inside this script self-skips when cargo is absent (installs
#    get the broker binary from platform optional dependencies instead).
npm run build

# 3. Pre-test guards CI runs ahead of the vitest suite.
node --test \
  scripts/pr-proof/broker-transfer.test.mjs \
  scripts/pr-proof/run-cloud.test.mjs \
  scripts/flows/deploy-listeners.test.mjs \
  scripts/pr-proof/event-from-input.test.mjs
npm run test:subscriptions:proof

# 4. The test suite. This is `npm test` without re-running its `pretest`
#    build, which step 2 already did.
npx vitest run

# Deliberately not run here:
# - `cargo test` / `cargo clippy` / `cargo fmt` (.github/workflows/rust-ci.yml)
#   and `swift build` / `swift test` (test.yml swift-test job): no Rust or
#   Swift toolchain on this machine.
# - Codecov upload (test.yml coverage job): needs repository upload
#   credentials. `npm run test:coverage` also just re-runs the same suite with
#   coverage thresholds.
# - Windows credential ACL job (test.yml): windows-latest only.
# - `npm run lint` / `npm run format:check`: separate CI jobs, not part of the
#   test path.
# - `npm run codegen:models` (.github/workflows/codegen-models.yml): its own
#   workflow, triggered only by packages/utils/cli-registry.yaml changes, and
#   it rewrites tracked generated files rather than gating the tests.
# - `npm run test:e2e`, `npm run smoke:prod`, `npm run evals`, and the
#   fleet/cleanroom verify flows: these need a live Relay broker, cloud
#   credentials, or real agent CLI API keys.

Refs #1823


[!NOTE]
Medium Risk
Changes deduplication and subscription provisioning paths used for provider writeback setup; behavior is carefully bounded but mistakes could drop or duplicate messages or tear down bindings incorrectly.

Overview
Partial fix for duplicate Relay replies and safer subscription setup — it does not claim to fix GitHub writeback (documented in docs/evidence/provider-subscription-replies.md).

MCP post_message and reply_to_thread now go through McpRequestReplay like send_dm, with an optional idempotency_key forwarded to Relaycast. Coalescing is pending-only (JSON-RPC request ID per session/tool, or explicit key); identical text on separate calls stays separate. Native host tools dedupe only a replay of the same model toolCallId, which the AI SDK harness now passes into execute.

integration subscribe validates --events against engine SUBSCRIBABLE_EVENT_TYPES, de-duplicates events, read-backs persisted subscription coverage before binding, and rolls back incomplete/inactive/mismatched subscriptions. Success output and --list distinguish configured writeback vs delivery proof and report GitHub PR identity authorization honestly. SDK subscription responses are normalized across events / event_types field shapes.

Regression tests cover MCP/native replay boundaries, subscribe verification/rollback, and SDK normalization.

Reviewed by Cursor Bugbot for commit e6bd9d64df5f098f2076704cf7766fb3692b254c. Bugbot is set up for automated code reviews on this repo. Configure here.