1+ import * as HttpServer from "effect/unstable/http/HttpServer" ;
2+ import {
3+ RelayClientAuth ,
4+ RelayClientPrincipal ,
5+ type RelayClientDeviceRecord ,
6+ } from "@t3tools/contracts/relay" ;
7+ import * as EnvironmentLinker from "../environments/EnvironmentLinker.ts" ;
8+ import * as RelayTokens from "../auth/RelayTokens.ts" ;
9+ import * as Devices from "../agentActivity/Devices.ts" ;
110import { createClerkClient , verifyToken } from "@clerk/backend" ;
11+ import * as NodeHttpPlatform from "@effect/platform-node/NodeHttpPlatform" ;
12+ import * as NodeServices from "@effect/platform-node/NodeServices" ;
213import { describe , expect , it } from "@effect/vitest" ;
314import { vi } from "vite-plus/test" ;
415import * as Context from "effect/Context" ;
@@ -12,22 +23,21 @@ import * as Predicate from "effect/Predicate";
1223import * as Redacted from "effect/Redacted" ;
1324import * as TestClock from "effect/testing/TestClock" ;
1425import * as Tracer from "effect/Tracer" ;
26+ import * as Etag from "effect/unstable/http/Etag" ;
1527import * as HttpRouter from "effect/unstable/http/HttpRouter" ;
1628import * as HttpServerRequest from "effect/unstable/http/HttpServerRequest" ;
1729import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse" ;
18- import * as HttpServer from "effect/unstable/http/HttpServer" ;
19- import * as HttpApiBuilder from "effect/unstable/httpapi/HttpApiBuilder" ;
2030import * as HttpApi from "effect/unstable/httpapi/HttpApi" ;
31+ import * as HttpApiBuilder from "effect/unstable/httpapi/HttpApiBuilder" ;
2132import { EnvironmentId } from "@t3tools/contracts" ;
2233import {
23- RelayApi ,
24- RelayClientAuth ,
25- RelayClientPrincipal ,
2634 RelayEnvironmentAuth ,
27- type RelayClientDeviceRecord ,
35+ RelayEnvironmentPrincipal ,
36+ RelayApi ,
2837} from "@t3tools/contracts/relay" ;
2938
3039import {
40+ RELAY_HTTP_ROUTER_CONFIG ,
3141 RELAY_REQUEST_DEADLINE_MS ,
3242 clientApi ,
3343 relayCors ,
@@ -36,6 +46,7 @@ import {
3646 relayNotFoundRoute ,
3747 relayDpopFailureReason ,
3848 revokeEnvironmentLinkRecord ,
49+ serverApi ,
3950 traceRelayHttpRequestWith ,
4051 unlinkEnvironmentRecord ,
4152 verifyRelayClientBearerToken ,
@@ -46,9 +57,8 @@ import * as RelayDb from "../db.ts";
4657import * as EnvironmentCredentials from "../environments/EnvironmentCredentials.ts" ;
4758import * as EnvironmentLinks from "../environments/EnvironmentLinks.ts" ;
4859import * as ManagedEndpointProvider from "../environments/ManagedEndpointProvider.ts" ;
49- import * as EnvironmentLinker from "../environments/EnvironmentLinker.ts" ;
50- import * as RelayTokens from "../auth/RelayTokens.ts" ;
51- import * as Devices from "../agentActivity/Devices.ts" ;
60+ import * as AgentActivityPublisher from "../agentActivity/AgentActivityPublisher.ts" ;
61+ import * as EnvironmentPublishSignatures from "../environments/EnvironmentPublishSignatures.ts" ;
5262
5363vi . mock ( "@clerk/backend" , ( ) => ( {
5464 createClerkClient : vi . fn ( ) ,
@@ -606,6 +616,86 @@ describe("relay request tracing", () => {
606616} ) ;
607617
608618describe ( "relay routing fallback" , ( ) => {
619+ it . effect ( "publishes activity for escaped delegated thread IDs longer than 100 characters" , ( ) =>
620+ Effect . gen ( function * ( ) {
621+ const environmentId = "environment-1" ;
622+ const environmentPublicKey = "environment-public-key" ;
623+ const published : Array <
624+ Parameters < AgentActivityPublisher . AgentActivityPublisher [ "Service" ] [ "publish" ] > [ 0 ]
625+ > = [ ] ;
626+ const verified : Array <
627+ Parameters <
628+ EnvironmentPublishSignatures . EnvironmentPublishSignatures [ "Service" ] [ "verify" ]
629+ > [ 0 ]
630+ > = [ ] ;
631+ const publisher = Layer . succeed ( AgentActivityPublisher . AgentActivityPublisher , {
632+ publish : ( input ) =>
633+ Effect . sync ( ( ) => {
634+ published . push ( input ) ;
635+ return { ok : true as const , deliveries : [ ] } ;
636+ } ) ,
637+ replayForLiveActivityRegistration : ( ) => Effect . succeed ( null ) ,
638+ } ) ;
639+ const signatures = Layer . succeed ( EnvironmentPublishSignatures . EnvironmentPublishSignatures , {
640+ verify : ( input ) =>
641+ Effect . sync ( ( ) => {
642+ verified . push ( input ) ;
643+ } ) ,
644+ } ) ;
645+ const auth = Layer . succeed ( RelayEnvironmentAuth , {
646+ environmentBearer : ( effect ) =>
647+ effect . pipe (
648+ Effect . provideService ( RelayEnvironmentPrincipal , {
649+ environmentId,
650+ environmentPublicKey,
651+ } ) ,
652+ ) ,
653+ } ) ;
654+ const routes = HttpApiBuilder . layer (
655+ HttpApi . make ( "RelayApi" ) . add ( RelayApi . groups . server ) ,
656+ ) . pipe (
657+ Layer . provide ( serverApi . pipe ( Layer . provide ( [ publisher , signatures ] ) ) ) ,
658+ Layer . provide ( auth ) ,
659+ Layer . provide ( [ NodeServices . layer , NodeHttpPlatform . layer , Etag . layerWeak ] ) ,
660+ ) ;
661+ const httpEffect = yield * HttpRouter . toHttpEffect (
662+ Layer . mergeAll ( routes , relayNotFoundRoute , relayCors ) ,
663+ ) . pipe ( Effect . provideService ( HttpRouter . RouterConfig , RELAY_HTTP_ROUTER_CONFIG ) ) ;
664+ const threadIds = [
665+ "b7c8c522-d244-43dc-875f-7224fce79912" ,
666+ "thread:delegated-task:command%3Amcp%3Ab7c8c522-d244-43dc-875f-7224fce79912%3Adelegate-task%3Agreet-subagent-20260813" ,
667+ ] ;
668+ for ( const threadId of threadIds ) {
669+ const request = HttpServerRequest . fromWeb (
670+ new Request (
671+ `https://relay.test/v1/environments/${ environmentId } /threads/${ encodeURIComponent ( threadId ) } /agent-activity` ,
672+ {
673+ method : "POST" ,
674+ headers : {
675+ authorization : "Bearer environment-credential" ,
676+ "content-type" : "application/json" ,
677+ } ,
678+ body : '{"state":null,"proof":"signed-proof"}' ,
679+ } ,
680+ ) ,
681+ ) ;
682+ const response = yield * httpEffect . pipe (
683+ Effect . provideService ( HttpServerRequest . HttpServerRequest , request ) ,
684+ ) ;
685+ expect ( response . status ) . toBe ( 200 ) ;
686+ }
687+ expect ( published ) . toEqual (
688+ threadIds . map ( ( threadId ) => ( {
689+ environmentId,
690+ environmentPublicKey,
691+ threadId,
692+ state : null ,
693+ } ) ) ,
694+ ) ;
695+ expect ( verified . map ( ( input ) => input . threadId ) ) . toEqual ( threadIds ) ;
696+ } ) . pipe ( Effect . scoped ) ,
697+ ) ;
698+
609699 it . effect ( "redirects the relay root to the API docs" , ( ) =>
610700 Effect . gen ( function * ( ) {
611701 const request = HttpServerRequest . fromWeb ( new Request ( "https://relay.test/" ) ) ;
0 commit comments