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
7 changes: 6 additions & 1 deletion .github/workflows/closura-arc-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ jobs:
apps/web/src/composerDraftStore.test.ts \
apps/web/src/components/chat/composerSend.logic.test.ts \
apps/web/src/components/chat/chatViewSend.test.ts \
apps/web/src/components/chat/composerImagePersistence.test.ts
apps/web/src/components/chat/composerImagePersistence.test.ts \
apps/web/src/components/chat/draftPlacement.test.ts \
apps/web/src/components/chat/draftAttachmentUploads.test.ts \
apps/web/src/components/chat/draftUploadReconcile.test.ts \
apps/web/src/lib/attachmentUploadQueue.test.ts \
apps/server/src/ws.test.ts

- name: Build native T3 server
shell: bash
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/auth/RpcAuthorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const RPC_REQUIRED_SCOPES = {
[ORCHESTRATION_V2_WS_METHODS.getThreadProjection]: AuthOrchestrationReadScope,
[ORCHESTRATION_V2_WS_METHODS.getTurnItem]: AuthOrchestrationReadScope,
[ORCHESTRATION_V2_WS_METHODS.launchThread]: AuthOrchestrationOperateScope,
[ORCHESTRATION_V2_WS_METHODS.admitThreadPlacement]: AuthOrchestrationOperateScope,
[ORCHESTRATION_V2_WS_METHODS.subscribeShell]: AuthOrchestrationReadScope,
[ORCHESTRATION_V2_WS_METHODS.subscribeThread]: AuthOrchestrationReadScope,
[ORCHESTRATION_V2_WS_METHODS.subscribeCommandOutput]: AuthOrchestrationReadScope,
Expand Down
33 changes: 8 additions & 25 deletions apps/server/src/mcp/toolkits/project/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ import {
agentLaunchInboxRouting,
CommandId,
type EnvironmentId,
isProviderAvailable,
MessageId,
ThreadId,
OrchestratorMcpFailure,
ProjectId,
type ProviderInteractionMode,
resolveProjectProviderInstanceEnabled,
type RuntimeMode,
} from "@t3tools/contracts";
import type * as Tool from "effect/ai/Tool";
import * as RemoteDelegation from "../../../peer/RemoteDelegation.ts";
import * as HostResources from "../../../resourceTelemetry/HostResources.ts";
import { hereEvidence } from "../../../peer/placementEvidence.ts";
import * as Digest from "effect/crypto/Digest";
import * as Effect from "effect/Effect";
import * as Hex from "effect/encoding/Hex";
Expand All @@ -30,8 +28,8 @@ import { mutateProjectWithClone } from "../../../project/ProjectMutation.ts";
import * as ManagedProjectFolders from "../../../project/ManagedProjectFolders.ts";
import * as Repositories from "../../../sourceControl/SourceControlRepositoryService.ts";
import * as GitVcsDriver from "../../../vcs/GitVcsDriver.ts";
import * as ServerSettings from "../../../serverSettings.ts";
import * as ProviderRegistry from "../../../provider/ProviderRegistry.ts";
import * as ServerSettings from "../../../serverSettings.ts";
import * as DateTime from "effect/DateTime";
import { resolveNewThreadModelSelection } from "@t3tools/shared/usageLimits";
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
Expand Down Expand Up @@ -198,34 +196,19 @@ const launchPlacedThere = Effect.fn("mcp.launchPlacedThere")(function* (
message:
'With placement "auto", pass clientRequestId and modelSelection: a retry must reach the environment chosen, with the exact model.',
});
const providers = yield* (yield* ProviderRegistry.ProviderRegistry).getProviders;
const settings = yield* ServerSettings.ServerSettingsService.pipe(
Effect.flatMap((service) => service.getSettings),
Effect.mapError(unavailable),
);
const provider = providers.find(
(candidate) => candidate.instanceId === modelSelection.instanceId,
);
const hostResources = yield* Effect.serviceOption(HostResources.HostResources);
const here = yield* hereEvidence({
projectId: local.projectId,
modelSelection,
runtimeMode: local.runtimeMode,
}).pipe(Effect.mapError(unavailable));
const environmentId = yield* delegation.value.place(scope, {
operation: "thread-launch",
key,
threadId: local.threadId,
projectId: local.projectId,
modelSelection,
runtimeMode: local.runtimeMode,
here: {
resources: Option.isSome(hostResources) ? yield* hostResources.value.read : null,
modelAvailable:
provider !== undefined &&
provider.enabled &&
isProviderAvailable(provider) &&
resolveProjectProviderInstanceEnabled(settings, local.projectId, provider.instanceId) &&
provider.models.some((model) => model.slug === modelSelection.model) &&
(provider.supportedRuntimeModes === undefined ||
provider.supportedRuntimeModes.length === 0 ||
provider.supportedRuntimeModes.includes(local.runtimeMode)),
},
here,
});
return environmentId === scope.environmentId ? undefined : yield* sendThere(environmentId, key);
});
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/observability/RpcInstrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const RPC_AGGREGATES = {
[ORCHESTRATION_V2_WS_METHODS.getOlderSettledThreads]: "orchestration",
[ORCHESTRATION_V2_WS_METHODS.getThreadProjection]: "orchestrationV2",
[ORCHESTRATION_V2_WS_METHODS.launchThread]: "orchestration",
[ORCHESTRATION_V2_WS_METHODS.admitThreadPlacement]: "orchestration",
[ORCHESTRATION_V2_WS_METHODS.subscribeShell]: "orchestrationV2",
[ORCHESTRATION_V2_WS_METHODS.subscribeThread]: "orchestrationV2",
[ORCHESTRATION_V2_WS_METHODS.subscribeCommandOutput]: "orchestrationV2",
Expand Down
18 changes: 18 additions & 0 deletions apps/server/src/orchestration-v2/ThreadManagementService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,10 @@ export interface ThreadManagementServiceShape {
}) => Effect.Effect<boolean>;
/** Receipt replays start no work; skip enablement checks with the same command id so settings cannot reject accepted retries. */
readonly hasReceipt: (commandId: CommandId) => Effect.Effect<boolean>;
/** The thread a command's launch here created, when its `thread.create` was accepted. */
readonly acceptedLaunchOf: (
commandId: CommandId,
) => Effect.Effect<Option.Option<ThreadId>, CommandReceiptStore.CommandReceiptStoreV2Error>;
/** What a caller's intent was bound to, such as the environment chosen for it, if it was. */
readonly intentOf: (
commandId: CommandId,
Expand Down Expand Up @@ -666,6 +670,19 @@ const make = Effect.gen(function* () {
Effect.map(Option.isSome),
);

const acceptedLaunchOf: ThreadManagementServiceShape["acceptedLaunchOf"] = (commandId) =>
receipts
.getByCommandId(commandId)
.pipe(
Effect.map(
Option.flatMap((receipt) =>
receipt.status === "accepted" && receipt.commandType === "thread.create"
? Option.some(receipt.threadId)
: Option.none(),
),
),
);

// A binding is a receipt with no events, so it lives and is kept like every other
// command's; its type carries the value.
const intentOf: ThreadManagementServiceShape["intentOf"] = (commandId) =>
Expand Down Expand Up @@ -1819,6 +1836,7 @@ const make = Effect.gen(function* () {
assertProviderInstanceEnabled,
isProviderInstanceEnabledForProject,
hasReceipt,
acceptedLaunchOf,
intentOf,
bindIntent,
waitForThread,
Expand Down
118 changes: 104 additions & 14 deletions apps/server/src/peer/PeerForwarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
OrchestratorMcpThreadWaitResult,
type PeerLinkError,
RuntimeMode,
type ThreadId,
} from "@t3tools/contracts";
import * as Context from "effect/Context";
import * as Digest from "effect/crypto/Digest";
Expand All @@ -24,7 +25,7 @@ import type * as Tool from "effect/ai/Tool";
import { runtimeModeRank } from "../mcp/delegatedTaskModes.ts";
import { assertNotLinked } from "../mcp/linkOrigin.ts";
import * as McpInvocationContext from "../mcp/McpInvocationContext.ts";
import { readCaller, unavailable } from "../mcp/threadAccess.ts";
import { type CallerLimits, readCaller, unavailable } from "../mcp/threadAccess.ts";
import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts";
import * as PeerLinks from "./PeerLinks.ts";
import * as PeerMcpClient from "./PeerMcpClient.ts";
Expand All @@ -35,6 +36,29 @@ const MAX_WAIT_TIMEOUT_MS = 45_000;

type Scope = McpInvocationContext.McpInvocationScope;

/**
* A client signed in to this server itself, rather than an agent over MCP: it has no thread
* here, and its requests carry the modes it asked for, already within this server's ceilings.
*/
export interface NativeCaller {
readonly limits: CallerLimits;
}

/** What a native caller may ask a linked environment: evidence for placement, never an effect. */
const NATIVE_EVIDENCE_TOOLS: ReadonlySet<string> = new Set([
"orchestrator_capabilities",
"t3_project_list",
]);

/** Who a forwarded call is made for, as it travels: its limits, retry-key scope and thread. */
interface ForwardedCaller {
readonly requestNamespace: string | undefined;
readonly sender:
| { readonly environmentId: EnvironmentId; readonly threadId: ThreadId; readonly title: string }
| undefined;
readonly limits: CallerLimits;
}

/**
* Runs T3 MCP tools in a linked environment for an agent here. The tool's own
* declaration checks the agent here first; the linked environment then holds
Expand Down Expand Up @@ -74,6 +98,16 @@ export class PeerForwarding extends Context.Service<
environmentId: EnvironmentId,
input: OrchestratorMcpThreadWaitInput,
) => Effect.Effect<OrchestratorMcpThreadWaitResult, OrchestratorMcpFailure>;
/**
* A read-only evidence tool (`orchestrator_capabilities`, `t3_project_list`) there for a
* native caller, under the link's credential narrowed to the caller's modes.
*/
readonly callNative: <T extends Tool.Any>(
caller: NativeCaller,
tool: T,
environmentId: EnvironmentId,
params: Tool.Parameters<T>,
) => Effect.Effect<Tool.Success<T>, OrchestratorMcpFailure>;
/** The environments this one links to, as an agent sees them. */
readonly links: (
scope: Scope,
Expand Down Expand Up @@ -149,17 +183,50 @@ const make = Effect.gen(function* () {
});
});

/** Tool and contract schemas are plain data: they encode and decode without services. */
const forward = Effect.fn("PeerForwarding.forward")(function* <A>(
const mcpCaller = (scope: Scope) =>
forwardingCaller(scope).pipe(
Effect.map(({ caller, limits }): ForwardedCaller => ({
requestNamespace: scope.requestNamespace,
// So a message sent there names the thread that sent it, even to a client that cannot reach here.
sender:
caller === undefined
? undefined
: { environmentId: scope.environmentId, threadId: caller.id, title: caller.title },
limits,
})),
);

const forward = <A>(
scope: Scope,
toolName: string,
parameters: Schema.Top,
success: Schema.Top,
environmentId: EnvironmentId,
params: unknown,
expectedIncarnation?: string,
) =>
Effect.flatMap(mcpCaller(scope), (caller) =>
forwardAs<A>(
caller,
toolName,
parameters,
success,
environmentId,
params,
expectedIncarnation,
),
);

/** Tool and contract schemas are plain data: they encode and decode without services. */
const forwardAs = Effect.fn("PeerForwarding.forward")(function* <A>(
{ requestNamespace, sender, limits }: ForwardedCaller,
toolName: string,
parameters: Schema.Top,
success: Schema.Top,
environmentId: EnvironmentId,
params: unknown,
expectedIncarnation?: string,
) {
const { caller, limits } = yield* forwardingCaller(scope);
const encoded = (yield* Schema.encodeUnknownEffect(parameters as Schema.Encoder<unknown>)(
params,
).pipe(Effect.mapError(unavailable))) as Record<string, unknown>;
Expand All @@ -173,17 +240,14 @@ const make = Effect.gen(function* () {
});
}
if (typeof args.clientRequestId === "string") {
args.clientRequestId = yield* scopedRequestId(scope.requestNamespace, args.clientRequestId);
if (requestNamespace === undefined) {
return yield* new OrchestratorMcpFailure({
code: "invalid_request",
message: "This caller cannot send a request with a retry key to a linked environment.",
});
}
args.clientRequestId = yield* scopedRequestId(requestNamespace, args.clientRequestId);
}
// So a message sent there names the thread that sent it, even to a client that cannot reach here.
const sender =
caller === undefined
? undefined
: {
environmentId: scope.environmentId,
threadId: caller.id,
title: caller.title,
};
return yield* peers.call({
environmentId,
tool: toolName,
Expand Down Expand Up @@ -290,8 +354,34 @@ const make = Effect.gen(function* () {
},
);

const callNative: PeerForwarding["Service"]["callNative"] = (
caller,
tool,
environmentId,
params,
) =>
NATIVE_EVIDENCE_TOOLS.has(tool.name)
? Effect.andThen(
requireEnabled,
forwardAs<Tool.Success<typeof tool>>(
{ requestNamespace: undefined, sender: undefined, limits: caller.limits },
tool.name,
tool.parametersSchema,
tool.successSchema,
environmentId,
params,
),
)
: Effect.fail(
new OrchestratorMcpFailure({
code: "capability_denied",
message: `${tool.name} is not placement evidence, so it is not sent for a native caller.`,
}),
);

return PeerForwarding.of({
call,
callNative,
readThread,
interruptThread,
waitForThread,
Expand Down
20 changes: 20 additions & 0 deletions apps/server/src/peer/PeerLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ export class PeerLinks extends Context.Service<
readonly enabled: Effect.Effect<boolean>;
readonly link: (input: PeerLinkCreateInput) => Effect.Effect<PeerLink, PeerLinkError>;
readonly list: Effect.Effect<ReadonlyArray<PeerLinkSummary>, PeerLinkError>;
/**
* Machines automatic placement never uses or contacts (server settings). Unreadable settings
* fail, so placement contacts nobody rather than everyone.
*/
readonly placementExclusions: Effect.Effect<ReadonlySet<EnvironmentId>, PeerLinkError>;
/** The stored links, read here only: no peer is asked whether it answers. */
readonly stored: Effect.Effect<ReadonlyArray<PeerLink>, PeerLinkError>;
/**
* Names the link's session there without revealing its token. Linking again
* starts a new session there, so this changes exactly when that does.
Expand Down Expand Up @@ -241,6 +248,13 @@ const make = Effect.gen(function* () {
Effect.map((settings) => settings.linkedEnvironmentsEnabled),
Effect.orElseSucceed(() => false),
);
const placementExclusions = serverSettings.getSettings.pipe(
Effect.map(
(settings): ReadonlySet<EnvironmentId> =>
new Set(settings.automaticPlacementExcludedEnvironmentIds),
),
Effect.catch(failStorage),
);

/** A fresh name for one link's token; the row records it. */
const newSecretName = random.randomBytes(16).pipe(
Expand Down Expand Up @@ -619,6 +633,10 @@ const make = Effect.gen(function* () {
});
});

const stored: PeerLinks["Service"]["stored"] = Effect.flatMap(readRows, (rows) =>
Effect.forEach(rows, toLink),
);

const get: PeerLinks["Service"]["get"] = (environmentId) =>
readRow(environmentId).pipe(
Effect.flatMap(
Expand Down Expand Up @@ -730,6 +748,8 @@ const make = Effect.gen(function* () {
enabled,
link,
list,
stored,
placementExclusions,
session,
get,
unlink,
Expand Down
Loading
Loading