TypeScript
perf(relay): avoid repeated activity decoding (#9708)
Theo Brownec75299ee2085
diff --git a/infra/relay/src/agentActivity/AgentActivityPublisher.test.ts b/infra/relay/src/agentActivity/AgentActivityPublisher.test.tsindex 4215f834654..a70a85a8e6e 100644--- a/infra/relay/src/agentActivity/AgentActivityPublisher.test.ts+++ b/infra/relay/src/agentActivity/AgentActivityPublisher.test.ts@@ -517,7 +517,7 @@ describe("AgentActivityPublisher", () => {});it.effect(- "does not build Live Activity aggregates for links with Live Activities disabled",+ "delivers notifications without querying activity rows when Live Activities are disabled",() => {const notificationState: RelayAgentActivityState = {...state,@@ -549,13 +549,7 @@ describe("AgentActivityPublisher", () => {AgentActivityRows.AgentActivityRows,makeAgentActivityRows({listForUser: () =>- Effect.succeed([- {- ...state,- environmentId: "other-env" as RelayAgentActivityState["environmentId"],- threadId: "other-thread" as RelayAgentActivityState["threadId"],- },- ]),+ Effect.die("Notification-only delivery must not read rows"),}),),Layer.succeed(diff --git a/infra/relay/src/agentActivity/AgentActivityPublisher.ts b/infra/relay/src/agentActivity/AgentActivityPublisher.tsindex 9b3bcf2298d..05ad4fd87b1 100644--- a/infra/relay/src/agentActivity/AgentActivityPublisher.ts+++ b/infra/relay/src/agentActivity/AgentActivityPublisher.ts@@ -58,7 +58,9 @@ export const make = Effect.gen(function* () {readonly state: RelayAgentActivityState | null;readonly nowMs: number;}) {- const activeStates = yield* rows.listForUser({ userId: input.deliveryUser.userId });+ const activeStates = input.deliveryUser.liveActivitiesEnabled+ ? yield* rows.listForUser({ userId: input.deliveryUser.userId })+ : [];const liveActivityAggregate = input.deliveryUser.liveActivitiesEnabled? makeAggregateState({activeStates,diff --git a/infra/relay/src/agentActivity/AgentActivityRows.test.ts b/infra/relay/src/agentActivity/AgentActivityRows.test.tsindex 65b0c2a0a90..cd57f515f72 100644--- a/infra/relay/src/agentActivity/AgentActivityRows.test.ts+++ b/infra/relay/src/agentActivity/AgentActivityRows.test.ts@@ -19,6 +19,42 @@ const state: RelayAgentActivityState = {};describe("AgentActivityRows", () => {+ it.effect("validates database JSON objects and ignores invalid activity rows", () => {+ const queryRows = [+ { stateJson: null },+ { stateJson: JSON.stringify(state) },+ { stateJson: { ...state, phase: "invalid" } },+ { stateJson: { ...state, unrelated: "ignored" } },+ ];+ const db = {+ select: () => ({+ from: () => ({+ innerJoin: () => ({+ where: () => ({+ orderBy: () => Effect.succeed(queryRows),+ }),+ }),+ }),+ }),+ } as unknown as RelayDb.RelayDb["Service"];++ return Effect.gen(function* () {+ const rows = yield* AgentActivityRows.AgentActivityRows;+ expect(yield* rows.listForUser({ userId: "user-2" })).toEqual([state]);+ expect(+ yield* rows.getForUserThread({+ userId: "user-2",+ environmentId: "env-1",+ threadId: "thread-1",+ }),+ ).toEqual(state);+ }).pipe(+ Effect.provide(+ AgentActivityRows.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, db))),+ ),+ );+ });+it.effect("preserves activity context on persistence failures", () => {const cause = new Error("database unavailable");const failingDb = {diff --git a/infra/relay/src/agentActivity/AgentActivityRows.ts b/infra/relay/src/agentActivity/AgentActivityRows.tsindex 54ba1df5060..032a61a383e 100644--- a/infra/relay/src/agentActivity/AgentActivityRows.ts+++ b/infra/relay/src/agentActivity/AgentActivityRows.ts@@ -92,15 +92,12 @@ export class AgentActivityRows extends Context.Service<>()("t3code-relay/agentActivity/AgentActivityRows") {}const decodeJsonString = Schema.decodeEffect(Schema.fromJsonString(Schema.Unknown));-const encodeJsonValue = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));const encodeRelayAgentActivityStateJson = Schema.encodeEffect(Schema.fromJsonString(RelayAgentActivityStateSchema),);-const decodeRelayAgentActivityStateJson = Schema.decodeUnknownOption(- Schema.fromJsonString(RelayAgentActivityStateSchema),-);+const decodeRelayAgentActivityState = Schema.decodeUnknownOption(RelayAgentActivityStateSchema);export const make = Effect.gen(function* () {const db = yield* RelayDb.RelayDb;@@ -229,13 +226,8 @@ export const make = Effect.gen(function* () {).orderBy(desc(relayAgentActivityRows.updatedAt)).pipe(- Effect.flatMap((rows) =>- Effect.forEach(rows, (row) => encodeJsonValue(row.stateJson), {- concurrency: "unbounded",- }),- ),Effect.map((rows) =>- rows.flatMap((row) => Option.toArray(decodeRelayAgentActivityStateJson(row))),+ rows.flatMap((row) => Option.toArray(decodeRelayAgentActivityState(row.stateJson))),),Effect.mapError((cause) =>@@ -271,14 +263,9 @@ export const make = Effect.gen(function* () {).orderBy(desc(relayAgentActivityRows.updatedAt)).pipe(- Effect.flatMap((rows) =>- Effect.forEach(rows, (row) => encodeJsonValue(row.stateJson), {- concurrency: "unbounded",- }),- ),Effect.map((rows) => {for (const row of rows) {- const decoded = decodeRelayAgentActivityStateJson(row);+ const decoded = decodeRelayAgentActivityState(row.stateJson);if (Option.isSome(decoded)) {return decoded.value;}