Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
231 changes: 201 additions & 30 deletions apps/server/src/ws.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
} from "@t3tools/contracts";
import { SOCKET_FRAME_PARTS_QUERY_PARAM } from "@t3tools/shared/socketFrameParts";
import * as Deferred from "effect/Deferred";
import * as Option from "effect/Option";
import type { ProviderInstance } from "@t3tools/provider-core/server/driver";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
Expand All @@ -65,7 +66,9 @@ import * as FetchHttpClient from "effect/http/FetchHttpClient";
import * as VcsProcess from "./vcs/VcsProcess.ts";
import * as ProviderInstallationGate from "./provider/ProviderInstallationGate.ts";
import * as Ws from "./ws.ts";
import { RpcGroup, RpcMessage, RpcServer } from "effect/rpc";
import { RpcClient, RpcGroup, RpcMessage, RpcSerialization, RpcServer } from "effect/rpc";
import * as Socket from "effect/socket/Socket";
import * as NodeSocket from "@effect/platform-node/NodeSocket";
import * as RpcTest from "effect/rpc/RpcTest";
import * as SqlClient from "effect/sql/SqlClient";
import * as Statement from "effect/sql/Statement";
Expand Down Expand Up @@ -2652,12 +2655,41 @@ it.effect.each([
{ label: "a revoked session", expiresInMs: 3_600_000, listed: false, admitted: false },
] as const)(
"a native placement admission writes only for $label",
({ expiresInMs, listed, admitted }) =>
Effect.gen(function* () {
({ expiresInMs, listed, admitted }) => {
const asked: Array<{ readonly sessionId: string; readonly authorized: boolean }> = [];
// Captured here and passed in. The RPC layer does not get this service, so a request-time
// serviceOption admits None.
const delegation = Layer.mock(RemoteDelegation.RemoteDelegation)({
admit: (caller, input) =>
caller.stillAuthorized.pipe(
Effect.tap((authorized) =>
Effect.sync(() => asked.push({ sessionId: caller.sessionId, authorized })),
),
Effect.flatMap((authorized) =>
authorized
? Effect.succeed({
environmentId: EnvironmentId.make("environment-box"),
projectId: input.projectId,
reason: "capacity" as const,
resumed: false,
})
: Effect.fail(
new OrchestrationV2ThreadPlacementAdmitError({
commandId: input.commandId,
reason: "unavailable",
detail: "signed out",
}),
),
),
),
});
return Effect.gen(function* () {
const sessionId = AuthSessionId.make("web-session");
const now = yield* DateTime.now;
const asked: Array<{ readonly sessionId: string; readonly authorized: boolean }> = [];
const exits = yield* Queue.unbounded<Exit.Exit<unknown, unknown>>();
const remoteDelegation = yield* Effect.serviceOption(RemoteDelegation.RemoteDelegation).pipe(
Effect.provide(delegation),
);
const services = Layer.mergeAll(
layerUnusedConfigRpcServices,
Layer.mock(ExternalLauncher.ExternalLauncher)({}),
Expand Down Expand Up @@ -2695,31 +2727,6 @@ it.effect.each([
: [],
),
}),
// The delegation itself is covered with real links; here, what the socket hands it.
Layer.mock(RemoteDelegation.RemoteDelegation)({
admit: (caller, input) =>
caller.stillAuthorized.pipe(
Effect.tap((authorized) =>
Effect.sync(() => asked.push({ sessionId: caller.sessionId, authorized })),
),
Effect.flatMap((authorized) =>
authorized
? Effect.succeed({
environmentId: EnvironmentId.make("environment-box"),
projectId: input.projectId,
reason: "capacity" as const,
resumed: false,
})
: Effect.fail(
new OrchestrationV2ThreadPlacementAdmitError({
commandId: input.commandId,
reason: "unavailable",
detail: "signed out",
}),
),
),
),
}),
);
const server = yield* RpcServer.makeNoSerialization(WsRpcGroup, {
...WS_RPC_SERVER_OPTIONS,
Expand All @@ -2738,6 +2745,7 @@ it.effect.each([
yield* ServerBrowser.ServerBrowser,
() => Effect.void,
{ secretReferences: true },
{ remoteDelegation, managedPolicy: Option.none() },
).pipe(
Layer.provide(services),
Layer.merge(RpcAuthorization.layer([AuthOrchestrationOperateScope])),
Expand Down Expand Up @@ -2774,7 +2782,8 @@ it.effect.each([
Layer.mock(ServerBrowser.ServerBrowser)({}),
),
),
),
);
},
);

it.effect("a native placement admission needs operate access before anything is asked", () =>
Expand Down Expand Up @@ -2850,3 +2859,165 @@ it.effect("a native placement admission needs operate access before anything is
),
),
);

it.live(
"a route-layer placement capture admits when the request context has no RemoteDelegation",
() =>
Effect.gen(function* () {
let asked = 0;
let modelAvailable: boolean | undefined;
const delegation = Layer.mock(RemoteDelegation.RemoteDelegation)({
admit: (_caller, input) =>
input.here.pipe(
Effect.tap((here) =>
Effect.sync(() => {
asked += 1;
modelAvailable = here.modelAvailable;
}),
),
Effect.as({
environmentId: EnvironmentId.make("environment-box"),
projectId: input.projectId,
reason: "capacity" as const,
resumed: false,
}),
),
});
// A ceiling that full-access fails. Visible only while Ws.layer builds, not on the request.
const policy = ManagedPolicy.layerTest({
schemaVersion: 1,
organization: "Closura",
limits: { maxRuntimeMode: "approval-required" },
});
const services = Layer.mergeAll(
layerUnusedConfigRpcServices,
Layer.mock(ServerBrowser.ServerBrowser)({
profileReporter: Effect.succeed(() => Effect.void),
}),
Layer.mock(EnvironmentAuth.EnvironmentAuth)({
authenticateWebSocketUpgrade: () =>
Effect.succeed({
sessionId: AuthSessionId.make("placement-capture"),
subject: "browser",
method: "browser-session-cookie",
scopes: [AuthOrchestrationOperateScope],
}),
}),
Layer.mock(SessionStore.SessionStore)({
cookieName: "placement-capture",
legacyCookieName: undefined,
markConnected: () => Effect.void,
markDisconnected: () => Effect.void,
awaitInvalidation: () => Effect.never,
}),
Layer.mock(AnalyticsService.AnalyticsService)({
recordOwners: () => Effect.void,
record: () => Effect.void,
}),
Layer.mock(ServerRuntimeStartup.ServerRuntimeStartup)({
enqueueCommand: (effect) => effect,
}),
Layer.mock(ProviderRegistry.ProviderRegistry)({
getProviders: Effect.succeed([
{
instanceId: ProviderInstanceId.make("claudeAgent"),
driver: ProviderDriverKind.make("claudeAgent"),
enabled: true,
installed: true,
status: "ready",
version: "test",
auth: { status: "authenticated" },
checkedAt: "2026-01-01T00:00:00.000Z",
models: [
{
slug: "claude-sonnet-4-6",
name: "Sonnet",
aliases: [],
isCustom: false,
capabilities: null,
},
],
slashCommands: [],
skills: [],
} as ServerProvider,
]),
}),
Layer.mock(HostResources.HostResources)({
read: Effect.succeed({
sampledAt: 0,
cpuUtilization: null,
cpuCount: 1,
availableMemoryBytes: 1,
totalMemoryBytes: 1,
}),
}),
Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({
namedProjectsRoot: process.cwd(),
}),
Layer.mock(ScratchThreadPromotion.ScratchThreadPromotion)({}),
Layer.mock(ServerEnvironment.ServerEnvironment)({}),
Layer.mock(ExternalLauncher.ExternalLauncher)({}),
Layer.mock(RemoteOpenTargets.RemoteOpenTargets)({}),
Layer.mock(DirectEndpoints.DirectEndpoints)({}),
Layer.mock(UsageLimitSources.UsageLimitSources)({}),
Layer.mock(VcsProcess.VcsProcess)({
run: (input) =>
Effect.fail(
new VcsProcessSpawnError({
operation: input.operation,
command: input.command,
cwd: input.cwd,
cause: "No VCS commands in the placement fixture",
}),
),
}),
ProviderInstallationGate.layer,
FetchHttpClient.layer,
);
const context = yield* Layer.build(
HttpRouter.serve(Ws.layer.pipe(Layer.provide(delegation), Layer.provide(policy)), {
disableListenLog: true,
}).pipe(
Layer.provideMerge(
NodeHttpServer.layer(NodeHttp.createServer, { host: "127.0.0.1", port: 0 }),
),
Layer.provide(services),
Layer.provide(layerConfigSources),
),
);
const server = Context.get(context, HttpServer.HttpServer);
const origin = HttpServer.formatAddress(server.address);
const admitted = yield* Effect.gen(function* () {
const client = yield* RpcClient.make(WsRpcGroup);
return yield* client[ORCHESTRATION_V2_WS_METHODS.admitThreadPlacement]({
commandId: CommandId.make("web-launch"),
threadId: ThreadId.make("thread:web-launch"),
projectId: ProjectId.make("project:app"),
modelSelection: {
instanceId: ProviderInstanceId.make("claudeAgent"),
model: "claude-sonnet-4-6",
},
runtimeMode: "full-access",
interactionMode: "default",
candidateEnvironmentIds: [EnvironmentId.make("environment-box")],
});
}).pipe(
Effect.provide(
RpcClient.layerProtocolSocket().pipe(
Layer.provide(
Socket.layerWebSocket(
`${origin.replace(/^http/, "ws")}/ws?${ORCHESTRATION_PROTOCOL_QUERY_PARAM}=${ORCHESTRATION_PROTOCOL_VERSION}`,
{ openTimeout: "10 seconds" },
).pipe(Layer.provide(NodeSocket.layerWebSocketConstructor)),
),
Layer.provide(RpcSerialization.layerJson),
),
),
);
assert.equal(asked, 1);
// The captured policy removes full access. A request-time read would see no policy.
assert.equal(modelAvailable, false);
assert.equal(admitted.environmentId, EnvironmentId.make("environment-box"));
assert.equal(admitted.resumed, false);
}).pipe(Effect.scoped),
);
20 changes: 18 additions & 2 deletions apps/server/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,17 @@ export const layerWsRpc = (
serverBrowser: ServerBrowser.ServerBrowser["Service"],
reportProfiles: ServerBrowser.ProfileReporter,
settingsClient: { readonly secretReferences: boolean },
/**
* Placement services captured while `Ws.layer` builds. The socket handler runs later, in the
* HTTP server context, which does not include services provided only to the route layer.
*/
placementServices: {
readonly remoteDelegation: Option.Option<RemoteDelegation.RemoteDelegation["Service"]>;
readonly managedPolicy: Option.Option<ManagedPolicy.ManagedPolicy["Service"]>;
} = {
remoteDelegation: Option.none(),
managedPolicy: Option.none(),
},
typingPresence: TypingPresence = idleTypingPresence,
) =>
Layer.effectContext(
Expand Down Expand Up @@ -1599,8 +1610,8 @@ export const layerWsRpc = (
const processDiagnostics = yield* ProcessDiagnostics.ProcessDiagnostics;
const triage = yield* TriageService.TriageService;
const hostResources = yield* HostResources.HostResources;
const remoteDelegation = yield* Effect.serviceOption(RemoteDelegation.RemoteDelegation);
const managedPolicy = yield* Effect.serviceOption(ManagedPolicy.ManagedPolicy);
const remoteDelegation = placementServices.remoteDelegation;
const managedPolicy = placementServices.managedPolicy;
const placementContext = yield* Effect.context<
| ProviderRegistry.ProviderRegistry
| ServerSettings.ServerSettingsService
Expand Down Expand Up @@ -3743,6 +3754,10 @@ export const layer = Layer.unwrap(
const peerLinkRequests = yield* PeerLinkRequests.PeerLinkRequests;
const sql = yield* SqlClient.SqlClient;
const typingPresence = yield* makeTypingPresence;
// Read once here. A request-time serviceOption sees the HTTP server context, which never
// receives RemoteDelegation.layer, so every server would admit None.
const remoteDelegation = yield* Effect.serviceOption(RemoteDelegation.RemoteDelegation);
const managedPolicy = yield* Effect.serviceOption(ManagedPolicy.ManagedPolicy);
return HttpRouter.add(
"GET",
"/ws",
Expand Down Expand Up @@ -3817,6 +3832,7 @@ export const layer = Layer.unwrap(
serverBrowser,
reportProfiles,
readSettingsClient(url.value),
{ remoteDelegation, managedPolicy },
typingPresence,
).pipe(
Layer.provideMerge(RpcSerialization.layerJson),
Expand Down
Loading