From 6bcd445a7098fe637279bde689d558a05add0263 Mon Sep 17 00:00:00 2001 From: Cameron Pfiffer Date: Mon, 10 Aug 2026 15:53:48 -0700 Subject: [PATCH] Verify X subscription creates by readback. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Treat the provider create response as an acknowledgment when it omits a subscription record, then require one exact live match before continuing the mutation plan. 👾 Generated with [Letta Code](https://letta.com) Co-Authored-By: Letta Code --- src/connectors/x-client.ts | 19 +++++++++++++++++-- test/x-client.test.ts | 3 ++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/connectors/x-client.ts b/src/connectors/x-client.ts index 5670358..79e53ce 100644 --- a/src/connectors/x-client.ts +++ b/src/connectors/x-client.ts @@ -130,8 +130,23 @@ export class XApiClient { tag: boundedTag(subscription.tag), webhook_id: xNumericIdSchema.parse(subscription.webhookId), }, signal); - const data = z.object({ data: z.union([subscriptionSchema, z.array(subscriptionSchema).min(1)]) }).passthrough().parse(response).data; - return normalizeSubscription(Array.isArray(data) ? data[0]! : data); + const parsed = z.object({ + data: z.union([subscriptionSchema, z.array(subscriptionSchema).min(1)]), + }).passthrough().safeParse(response); + if (parsed.success) { + const data = parsed.data.data; + return normalizeSubscription(Array.isArray(data) ? data[0]! : data); + } + const matches = (await this.listSubscriptions(signal)).filter((candidate) => ( + candidate.eventType === subscription.eventType + && candidate.userId === subscription.userId + && candidate.tag === subscription.tag + && candidate.webhookId === subscription.webhookId + )); + if (matches.length !== 1) { + throw new Error("X subscription creation response was not a subscription and exact readback did not converge"); + } + return matches[0]!; } async updateSubscription( diff --git a/test/x-client.test.ts b/test/x-client.test.ts index fdfac98..c4e572a 100644 --- a/test/x-client.test.ts +++ b/test/x-client.test.ts @@ -68,6 +68,7 @@ describe("X management client", () => { "GET /2/users/by/username/karpathy", "POST /2/activity/subscriptions", "GET /2/activity/subscriptions", + "GET /2/activity/subscriptions", `PUT /2/activity/subscriptions/${subscription.id}`, "POST /2/webhooks/replay", `DELETE /2/activity/subscriptions/${subscription.id}`, @@ -233,7 +234,7 @@ async function xApiFixture() { updated_at: "2026-08-10T10:00:00.000Z", }; subscriptions.push(record); - send(response, 200, { data: record }); + send(response, 200, { data: { accepted: true } }); return; } const subscriptionMatch = /^\/2\/activity\/subscriptions\/([0-9]+)$/.exec(url.pathname); -- 2.51.2