fix(relay): accept delegated thread IDs in activity routes (#12484)

This commit is contained in:
Julius Marminge authored and GitHub committed 2026-09-18 11:13:18 -07:00
1 parent 4c61e3d7ec
commit 80c1f771b3
6 files changed
+1628 -11

No files matched your search

@@ -0,0 +1,2 @@
ALTER TABLE "relay_agent_activity_rows" ALTER COLUMN "thread_id" SET DATA TYPE varchar(512) USING "thread_id"::varchar(512);--> statement-breakpoint
ALTER TABLE "relay_delivery_attempts" ALTER COLUMN "thread_id" SET DATA TYPE varchar(512) USING "thread_id"::varchar(512);
File diff suppressed because it is too large. Load diff
+100 -9
View File
@@ -1,4 +1,15 @@
import * as HttpServer from "effect/unstable/http/HttpServer";
import {
RelayClientAuth,
RelayClientPrincipal,
type RelayClientDeviceRecord,
} from "@t3tools/contracts/relay";
import * as EnvironmentLinker from "../environments/EnvironmentLinker.ts";
import * as RelayTokens from "../auth/RelayTokens.ts";
import * as Devices from "../agentActivity/Devices.ts";
import { createClerkClient, verifyToken } from "@clerk/backend";
import * as NodeHttpPlatform from "@effect/platform-node/NodeHttpPlatform";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { describe, expect, it } from "@effect/vitest";
import { vi } from "vite-plus/test";
import * as Context from "effect/Context";
@@ -12,22 +23,21 @@ import * as Predicate from "effect/Predicate";
import * as Redacted from "effect/Redacted";
import * as TestClock from "effect/testing/TestClock";
import * as Tracer from "effect/Tracer";
import * as Etag from "effect/unstable/http/Etag";
import * as HttpRouter from "effect/unstable/http/HttpRouter";
import * as HttpServerRequest from "effect/unstable/http/HttpServerRequest";
import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
import * as HttpServer from "effect/unstable/http/HttpServer";
import * as HttpApiBuilder from "effect/unstable/httpapi/HttpApiBuilder";
import * as HttpApi from "effect/unstable/httpapi/HttpApi";
import * as HttpApiBuilder from "effect/unstable/httpapi/HttpApiBuilder";
import { EnvironmentId } from "@t3tools/contracts";
import {
RelayApi,
RelayClientAuth,
RelayClientPrincipal,
RelayEnvironmentAuth,
type RelayClientDeviceRecord,
RelayEnvironmentPrincipal,
RelayApi,
} from "@t3tools/contracts/relay";
import {
RELAY_HTTP_ROUTER_CONFIG,
RELAY_REQUEST_DEADLINE_MS,
clientApi,
relayCors,
@@ -36,6 +46,7 @@ import {
relayNotFoundRoute,
relayDpopFailureReason,
revokeEnvironmentLinkRecord,
serverApi,
traceRelayHttpRequestWith,
unlinkEnvironmentRecord,
verifyRelayClientBearerToken,
@@ -46,9 +57,8 @@ import * as RelayDb from "../db.ts";
import * as EnvironmentCredentials from "../environments/EnvironmentCredentials.ts";
import * as EnvironmentLinks from "../environments/EnvironmentLinks.ts";
import * as ManagedEndpointProvider from "../environments/ManagedEndpointProvider.ts";
import * as EnvironmentLinker from "../environments/EnvironmentLinker.ts";
import * as RelayTokens from "../auth/RelayTokens.ts";
import * as Devices from "../agentActivity/Devices.ts";
import * as AgentActivityPublisher from "../agentActivity/AgentActivityPublisher.ts";
import * as EnvironmentPublishSignatures from "../environments/EnvironmentPublishSignatures.ts";
vi.mock("@clerk/backend", () => ({
createClerkClient: vi.fn(),
@@ -606,6 +616,87 @@ describe("relay request tracing", () => {
});
describe("relay routing fallback", () => {
it.effect("publishes activity for escaped delegated thread IDs longer than 100 characters", () =>
Effect.gen(function* () {
const environmentId = "environment-1";
const environmentPublicKey = "environment-public-key";
const published: Array<
Parameters<AgentActivityPublisher.AgentActivityPublisher["Service"]["publish"]>[0]
> = [];
const verified: Array<
Parameters<
EnvironmentPublishSignatures.EnvironmentPublishSignatures["Service"]["verify"]
>[0]
> = [];
const publisher = Layer.succeed(AgentActivityPublisher.AgentActivityPublisher, {
publish: (input) =>
Effect.sync(() => {
published.push(input);
return { ok: true as const, deliveries: [] };
}),
replayForLiveActivityRegistration: () => Effect.succeed(null),
});
const signatures = Layer.succeed(EnvironmentPublishSignatures.EnvironmentPublishSignatures, {
verify: (input) =>
Effect.sync(() => {
verified.push(input);
}),
});
const auth = Layer.succeed(RelayEnvironmentAuth, {
environmentBearer: (effect) =>
effect.pipe(
Effect.provideService(RelayEnvironmentPrincipal, {
environmentId,
environmentPublicKey,
}),
),
});
const routes = HttpApiBuilder.layer(
HttpApi.make("RelayApi").add(RelayApi.groups.server),
).pipe(
Layer.provide(serverApi.pipe(Layer.provide([publisher, signatures]))),
Layer.provide(auth),
Layer.provide([NodeServices.layer, NodeHttpPlatform.layer, Etag.layerWeak]),
);
const httpEffect = yield* HttpRouter.toHttpEffect(
Layer.mergeAll(routes, relayNotFoundRoute, relayCors),
).pipe(Effect.provideService(HttpRouter.RouterConfig, RELAY_HTTP_ROUTER_CONFIG));
const threadIds = [
"b7c8c522-d244-43dc-875f-7224fce79912",
"t".repeat(512),
"thread:delegated-task:command%3Amcp%3Ab7c8c522-d244-43dc-875f-7224fce79912%3Adelegate-task%3Agreet-subagent-20260813",
];
for (const threadId of threadIds) {
const request = HttpServerRequest.fromWeb(
new Request(
`https://relay.test/v1/environments/${environmentId}/threads/${encodeURIComponent(threadId)}/agent-activity`,
{
method: "POST",
headers: {
authorization: "Bearer environment-credential",
"content-type": "application/json",
},
body: '{"state":null,"proof":"signed-proof"}',
},
),
);
const response = yield* httpEffect.pipe(
Effect.provideService(HttpServerRequest.HttpServerRequest, request),
);
expect(response.status).toBe(200);
}
expect(published).toEqual(
threadIds.map((threadId) => ({
environmentId,
environmentPublicKey,
threadId,
state: null,
})),
);
expect(verified.map((input) => input.threadId)).toEqual(threadIds);
}).pipe(Effect.scoped),
);
it.effect("redirects the relay root to the API docs", () =>
Effect.gen(function* () {
const request = HttpServerRequest.fromWeb(new Request("https://relay.test/"));
+6
View File
@@ -71,6 +71,12 @@ import * as MobileRegistrations from "../agentActivity/MobileRegistrations.ts";
import { withSpanAttributes } from "../observability.ts";
import * as RelayDb from "../db.ts";
// Delegated thread IDs carry escaped command provenance and exceed the router's
// default 100-character path parameter limit. Match the environment server.
export const RELAY_HTTP_ROUTER_CONFIG = {
maxParamLength: 512,
} as const;
const relayCorsAllowedMethods = ["GET", "POST", "DELETE", "OPTIONS"] as const;
const relayCorsAllowedHeaders = [
"authorization",
+2 -2
View File
@@ -138,7 +138,7 @@ export const relayAgentActivityRows = pgTable(
{
environmentId: varchar("environment_id", { length: 191 }).notNull(),
environmentPublicKey: text("environment_public_key").notNull(),
threadId: varchar("thread_id", { length: 191 }).notNull(),
threadId: varchar("thread_id", { length: 512 }).notNull(),
stateJson: jsonb("state_json").notNull().$type<RelayAgentActivityState>(),
updatedAt: varchar("updated_at", { length: 64 }).notNull(),
createdAt: varchar("created_at", { length: 64 }).notNull(),
@@ -156,7 +156,7 @@ export const relayDeliveryAttempts = pgTable(
createdAt: varchar("created_at", { length: 64 }).notNull(),
userId: varchar("user_id", { length: 255 }),
environmentId: varchar("environment_id", { length: 191 }),
threadId: varchar("thread_id", { length: 191 }),
threadId: varchar("thread_id", { length: 512 }),
deviceId: varchar("device_id", { length: 255 }),
kind: varchar("kind", { length: 64 }).notNull(),
sourceJobId: varchar("source_job_id", { length: 64 }),
+2
View File
@@ -23,6 +23,7 @@ import {
healthApi,
metadataApi,
mobileApi,
RELAY_HTTP_ROUTER_CONFIG,
relayClientAuthLayer,
relayDpopClientAuthLayer,
relayCors,
@@ -345,6 +346,7 @@ export const ApiLive = Api.make(
relayNotFoundRoute,
).pipe(
HttpRouter.toHttpEffect,
Effect.provideService(HttpRouter.RouterConfig, RELAY_HTTP_ROUTER_CONFIG),
withoutCapturedParentSpan,
Effect.flatMap((httpEffect) => traceRelayHttpRequestWith(httpEffect, relayTraceLayer)),
);