diff --git a/.github/workflows/closura-arc-ci.yml b/.github/workflows/closura-arc-ci.yml index 8050491b53e79..262594353d21c 100644 --- a/.github/workflows/closura-arc-ci.yml +++ b/.github/workflows/closura-arc-ci.yml @@ -150,7 +150,8 @@ jobs: apps/server/src/orchestration-v2/SubagentProjection.test.ts \ packages/shared/src/threadPlacement.test.ts \ apps/server/src/peer/RemoteDelegation.test.ts \ - apps/server/src/mcp/OrchestratorMcpService.test.ts + apps/server/src/mcp/OrchestratorMcpService.test.ts \ + apps/server/src/cli/thread.test.ts - name: Build native T3 server shell: bash diff --git a/apps/server/src/cli/thread.test.ts b/apps/server/src/cli/thread.test.ts index 5252d81b2a22f..1ec2acf94bf32 100644 --- a/apps/server/src/cli/thread.test.ts +++ b/apps/server/src/cli/thread.test.ts @@ -31,18 +31,28 @@ import { waitForRun, } from "./thread.ts"; +// Like the server, every sign-in is a new session; revocations are kept for assertions. +const auth = vi.hoisted(() => ({ issued: 0, revoked: [] as Array })); + vi.mock("../auth/EnvironmentAuth.ts", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, layerRuntime: Layer.mock(actual.EnvironmentAuth)({ issueMcpClientSession: () => - Effect.map(DateTime.now, (expiresAt) => ({ - sessionId: AuthSessionId.make("test-session"), - token: "synthetic-token", - expiresAt, - })), - revokeSession: () => Effect.succeed(true), + Effect.map(DateTime.now, (expiresAt) => { + auth.issued += 1; + return { + sessionId: AuthSessionId.make(`test-session-${auth.issued}`), + token: `synthetic-token-${auth.issued}`, + expiresAt, + }; + }), + revokeSession: (sessionId) => + Effect.sync(() => { + auth.revoked.push(sessionId); + return true; + }), }), }; }); @@ -138,6 +148,273 @@ it.effect( ), ); +it.effect( + "start --request-id finds the thread an earlier run started when its answer was lost", + () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fs + .makeTempDirectoryScoped({ prefix: "t3-thread-request-id-" }) + .pipe(Effect.flatMap((directory) => fs.realPath(directory))); + const baseDir = path.join(root, "state"); + yield* persistServerRuntimeState({ + path: path.join(baseDir, "userdata", "server-runtime.json"), + state: yield* makePersistedServerRuntimeState({ + config: { host: "127.0.0.1", devUrl: undefined }, + port: 3_773, + }), + }); + // The server names a launch after the caller's session and its key, and replays it. + const launches = new Map(); + let answerLost = true; + using _session = vi.spyOn(McpBridge, "mcpToolSession").mockImplementation((options) => + Effect.succeed({ + call: (tool, args) => { + switch (tool) { + case "t3_project_list": + return Effect.succeed({ + structuredContent: { + projects: [ + { id: ProjectId.make("project-1"), title: "App", workspaceRoot: root }, + ], + nextCursor: null, + }, + }); + case "t3_thread_launch": { + const key = `${options.authorization} ${String(args.clientRequestId)}`; + const launched = launches.get(key) ?? ThreadId.make(`thread-${launches.size + 1}`); + launches.set(key, launched); + return answerLost + ? Effect.fail(new McpBridge.AcpMcpBridgeError({ message: "connection lost" })) + : Effect.succeed({ + structuredContent: { + threadId: launched, + projectId: args.projectId, + runId: null, + }, + }); + } + default: + return Effect.die(`unexpected ${tool} call`); + } + }, + }), + ); + // Each run is a new process: its own sign-in and its own services. + const start = Command.runWith(threadCommand, { version: "0.0.0" })([ + "start", + "project-1", + "Run the suite", + "--request-id", + "suite-1", + "--json", + "--base-dir", + baseDir, + ]); + + // The server takes the launch, but the answer never reaches this run. + const revokedBefore = auth.revoked.length; + const keptSession = `test-session-${auth.issued + 1}`; + const lost = yield* Effect.exit(start); + assert.strictEqual(lost._tag, "Failure"); + assert.strictEqual(launches.size, 1); + const revokedAfterLoss = auth.revoked.slice(revokedBefore); + + // A new run with the same id sends the same launch, which the server answers with that thread. + answerLost = false; + yield* start; + assert.strictEqual(launches.size, 1); + // The lost run kept its session for this one instead of revoking it. + assert.deepStrictEqual(revokedAfterLoss, []); + assert.strictEqual( + (yield* TestConsole.logLines).at(-1), + JSON.stringify({ threadId: "thread-1", projectId: "project-1", runId: null }), + ); + // Now that the thread is known, the kept session is given up. + assert.include(auth.revoked, keptSession); + + // Running it once more prints the same thread without launching anything. + yield* start; + assert.strictEqual(launches.size, 1); + assert.strictEqual( + (yield* TestConsole.logLines).at(-1), + JSON.stringify({ threadId: "thread-1", projectId: "project-1", runId: null }), + ); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + NodeServices.layer, + NodeCrypto.layer, + NetService.layer, + ConfigProvider.layer(ConfigProvider.fromEnv({ env: { T3CODE_PORT: "3773" } })), + ), + ), + ), +); + +it.effect( + "start --auto --wait follows the thread where it was placed, also from its recorded result", + () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fs + .makeTempDirectoryScoped({ prefix: "t3-thread-placed-wait-" }) + .pipe(Effect.flatMap((directory) => fs.realPath(directory))); + const baseDir = path.join(root, "state"); + yield* persistServerRuntimeState({ + path: path.join(baseDir, "userdata", "server-runtime.json"), + state: yield* makePersistedServerRuntimeState({ + config: { host: "127.0.0.1", devUrl: undefined }, + port: 3_773, + }), + }); + const there = "environment-box"; + const at = "2026-11-18T00:00:00.000Z"; + const detail = { + threadId, + projectId: "project-1", + title: "Run the suite", + createdBy: "agent", + creationSource: "mcp", + status: "idle", + latestRunId: runId, + activeRunId: null, + providerInstanceId: "claude-box", + model: "claude-sonnet-4-6", + runtimeMode: "full-access", + interactionMode: "default", + linkedPullRequest: null, + titleRegeneration: null, + branch: null, + worktreePath: null, + parentThreadId: null, + relationshipToParent: null, + runCount: 1, + itemCount: 1, + pendingRequestCount: 0, + archived: false, + settled: false, + settledAt: null, + snoozed: false, + snoozedUntil: null, + createdAt: at, + updatedAt: at, + }; + const reply = { + position: 0, + visibility: "local", + sourceThreadId: threadId, + itemId: "item-reply", + runId, + messageId: null, + createdBy: "agent", + creationSource: "mcp", + type: "assistant_message", + status: "completed", + title: null, + text: "Done on the box.", + textTruncated: false, + updatedAt: at, + }; + let launches = 0; + const elsewhere: Array = []; + // The thread runs only on the box: a follow-up that does not name it finds nothing here. + using _session = vi.spyOn(McpBridge, "mcpToolSession").mockReturnValue( + Effect.succeed({ + call: (tool, args) => { + if (tool === "t3_project_list") + return Effect.succeed({ + structuredContent: { + projects: [ + { id: ProjectId.make("project-1"), title: "App", workspaceRoot: root }, + ], + nextCursor: null, + }, + }); + if (tool === "t3_thread_launch") { + launches += 1; + return Effect.succeed({ + structuredContent: { + threadId, + projectId: "project-1", + runId, + environmentId: there, + }, + }); + } + if (args.environmentId !== there) { + elsewhere.push(tool); + return Effect.succeed({ + isError: true, + content: [{ type: "text", text: `Thread ${threadId} was not found here.` }], + }); + } + if (tool === "t3_thread_wait") + return Effect.succeed({ + structuredContent: { threadId, runId, status: "completed", timedOut: false }, + }); + if (tool === "t3_thread_read") + return Effect.succeed({ + structuredContent: { + thread: detail, + recentRuns: [], + items: [reply], + nextPosition: null, + hasMore: false, + }, + }); + return Effect.die(`unexpected ${tool} call`); + }, + }), + ); + const start = Command.runWith(threadCommand, { version: "0.0.0" })([ + "start", + "project-1", + "Run the suite", + "--auto", + "--provider", + "claude-box", + "--model", + "claude-sonnet-4-6", + "--request-id", + "placed-1", + "--wait", + "--json", + "--base-dir", + baseDir, + ]); + const result = JSON.stringify({ + threadId, + runId, + status: "completed", + reply: "Done on the box.", + error: null, + }); + + yield* start; + assert.deepStrictEqual(elsewhere, []); + assert.strictEqual((yield* TestConsole.logLines).at(-1), result); + // A fresh run recovers the recorded result and follows it to the box again. + yield* start; + assert.deepStrictEqual(elsewhere, []); + assert.strictEqual(launches, 1); + assert.strictEqual((yield* TestConsole.logLines).at(-1), result); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + NodeServices.layer, + NodeCrypto.layer, + NetService.layer, + ConfigProvider.layer(ConfigProvider.fromEnv({ env: { T3CODE_PORT: "3773" } })), + ), + ), + ), +); + it.effect("does not sign in to whatever took over a dead server's port", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/apps/server/src/cli/thread.ts b/apps/server/src/cli/thread.ts index 98d18a6c9d240..610ae825e8826 100644 --- a/apps/server/src/cli/thread.ts +++ b/apps/server/src/cli/thread.ts @@ -1,4 +1,6 @@ import { + AuthSessionId, + EnvironmentId, OrchestratorMcpFailure, OrchestratorMcpThreadListResult, OrchestratorMcpThreadReadResult, @@ -15,6 +17,9 @@ import { } from "@t3tools/contracts"; import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Console from "effect/Console"; +import * as Digest from "effect/crypto/Digest"; +import * as Hex from "effect/encoding/Hex"; +import * as Ref from "effect/Ref"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; @@ -28,6 +33,7 @@ import * as Stream from "effect/Stream"; import { Argument, Command, Flag, GlobalFlag } from "effect/cli"; import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts"; +import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; import * as ServerConfig from "../config.ts"; import { mcpToolSession, type McpToolSession } from "../mcp/AcpMcpStdioBridge.ts"; import { expandHomePath } from "../os-jank.ts"; @@ -172,6 +178,8 @@ const ProjectListResult = Schema.Struct({ const ThreadLaunchResult = Schema.Struct({ threadId: ThreadId, + /** The linked environment the thread was placed in; absent when it runs on this server. */ + environmentId: Schema.optional(EnvironmentId), projectId: ProjectId, runId: Schema.NullOr(RunId), }); @@ -211,13 +219,139 @@ const makeThreadTools = (session: McpToolSession, origin: string) => { }; export type ThreadTools = ReturnType; +/** The same tools, acting on a thread in the linked environment it was placed in. */ +const toolsIn = (tools: ThreadTools, environmentId: EnvironmentId | undefined): ThreadTools => + environmentId === undefined + ? tools + : { + call: >( + tool: string, + args: Readonly>, + schema: S, + ) => tools.call(tool, { ...args, environmentId }, schema), + }; + +/** A `thread start --request-id` whose launch is not yet known: the session it is sent with. */ +const PendingLaunch = Schema.Struct({ sessionId: Schema.String, token: Schema.String }); +const LaunchIntentRecord = Schema.Union([ + PendingLaunch, + Schema.Struct({ launched: ThreadLaunchResult }), +]); +const decodeLaunchIntent = Schema.decodeUnknownOption(Schema.fromJsonString(LaunchIntentRecord)); +const encodeLaunchIntent = Schema.encodeSync(Schema.fromJsonString(LaunchIntentRecord)); + +/** What a durable `thread start` knows before it launches, and how it records the outcome. */ +export interface LaunchIntent { + /** The thread an earlier run with this request id already started. */ + readonly recorded: Option.Option; + readonly record: (launched: typeof ThreadLaunchResult.Type) => Effect.Effect; +} + +const intentFailure = (detail: string) => + new ThreadToolFailedError({ tool: "t3_thread_launch", detail }); + +/** + * The server names a launch after the caller's session and key, and each run signs in + * afresh, so a request id keeps its launch's session in the server's secret store until + * the launch is known. A later run sends the same launch, which the server replays. + */ +const openLaunchIntent = Effect.fn("cli.thread.openLaunchIntent")(function* ( + environmentAuth: EnvironmentAuth.EnvironmentAuth["Service"], + requestId: string, +) { + const secrets = yield* ServerSecretStore.ServerSecretStore; + const digest = yield* Digest.Digest.pipe( + Effect.flatMap((crypto) => crypto.digest(Digest.sha256, new TextEncoder().encode(requestId))), + Effect.orDie, + ); + const name = `cli-thread-start-${Hex.encode(digest)}`; + const unreadable = () => + intentFailure( + "This request id's earlier start could not be read, so nothing was started. Run it again with the same --request-id.", + ); + const read = secrets.get(name).pipe( + Effect.mapError(unreadable), + Effect.flatMap( + Option.match({ + onNone: () => Effect.succeedNone, + onSome: (bytes) => + Option.match(decodeLaunchIntent(new TextDecoder().decode(bytes)), { + onNone: () => Effect.fail(unreadable()), + onSome: (record) => Effect.succeedSome(record), + }), + }), + ), + ); + let stored = yield* read; + if (Option.isNone(stored)) { + const issued = yield* environmentAuth.issueMcpClientSession({ + label: "t3 thread cli", + access: "full-access", + peerLink: false, + client: { deviceType: "bot" }, + }); + const pending = { sessionId: issued.sessionId, token: issued.token }; + // The first run to store its session owns the request id; any other gives its own up. + const created = yield* secrets + .create(name, new TextEncoder().encode(encodeLaunchIntent(pending))) + .pipe( + Effect.as(true), + Effect.catchIf(ServerSecretStore.isSecretAlreadyExistsError, () => Effect.succeed(false)), + Effect.tapError(() => + environmentAuth.revokeSession(issued.sessionId).pipe(Effect.ignore({ log: true })), + ), + Effect.mapError(() => + intentFailure( + "This request id could not be recorded, so nothing was started. Run it again with the same --request-id.", + ), + ), + ); + if (created) stored = Option.some(pending); + else { + yield* environmentAuth.revokeSession(issued.sessionId).pipe(Effect.ignore({ log: true })); + stored = yield* read; + } + } + if (Option.isNone(stored)) return yield* unreadable(); + const record = stored.value; + const resolved = yield* Ref.make(false); + // A launch not yet known keeps its session for the next run; a known one gives it up. + if ("sessionId" in record) { + yield* Effect.addFinalizer(() => + Ref.get(resolved).pipe( + Effect.flatMap((done) => + done + ? environmentAuth + .revokeSession(AuthSessionId.make(record.sessionId)) + .pipe(Effect.ignore({ log: true })) + : Effect.void, + ), + ), + ); + } + return { + pending: "sessionId" in record ? record : undefined, + intent: { + recorded: "launched" in record ? Option.some(record.launched) : Option.none(), + record: (launched) => + secrets.set(name, new TextEncoder().encode(encodeLaunchIntent({ launched }))).pipe( + Effect.andThen(Ref.set(resolved, true)), + // Unrecorded, the next run with this id sends the launch again and is answered the same. + Effect.ignore({ log: true }), + ), + } satisfies LaunchIntent, + }; +}); + /** * Signs in as a short-lived MCP client of the running server, the same way an - * outside agent does, and revokes that session when the command ends. + * outside agent does, and revokes that session when the command ends. With a + * request id, a launch keeps its session until it is known; see `openLaunchIntent`. */ const withThreadTools = Effect.fn("cli.thread.withThreadTools")(function* ( flags: { readonly baseDir: Option.Option; readonly json: boolean }, - run: (tools: ThreadTools) => Effect.Effect, + run: (tools: ThreadTools, intent: LaunchIntent | undefined) => Effect.Effect, + requestId?: string, ) { const logLevel = yield* GlobalFlag.LogLevel; const config = yield* resolveCliAuthConfig(flags, logLevel, { quietLogs: flags.json }); @@ -229,29 +363,42 @@ const withThreadTools = Effect.fn("cli.thread.withThreadTools")(function* - environmentAuth.revokeSession(session.sessionId).pipe(Effect.ignore({ log: true })), - ); + const opened = + requestId === undefined ? undefined : yield* openLaunchIntent(environmentAuth, requestId); + const token = + opened?.pending?.token ?? + (yield* Effect.acquireRelease( + environmentAuth.issueMcpClientSession({ + label: "t3 thread cli", + // The CLI runs as the machine's owner. New threads still start at + // the project's default mode, never at this ceiling by default. + access: "full-access", + peerLink: false, + client: { deviceType: "bot" }, + }), + (session) => + environmentAuth.revokeSession(session.sessionId).pipe(Effect.ignore({ log: true })), + )).token; const session = yield* mcpToolSession({ endpoint: new URL("/mcp", origin).href, - authorization: `Bearer ${issued.token}`, + authorization: `Bearer ${token}`, clientName: "t3-thread-cli", - }).pipe(Effect.mapError((cause) => new ThreadServerUnreachableError({ origin, cause }))); - return yield* run(makeThreadTools(session, origin)); + }).pipe( + Effect.mapError((cause) => + opened?.pending === undefined + ? new ThreadServerUnreachableError({ origin, cause }) + : // Only that session can find its launch, so a new one is never tried in its place. + intentFailure( + `Could not reach the T3 Code server at ${origin} with this request id's earlier session: the server may be stopped, or the session expired or was revoked. t3 thread list shows whether that start happened; anything new is a new request.`, + ), + ), + ); + return yield* run(makeThreadTools(session, origin), opened?.intent); }).pipe( Effect.scoped, Effect.provideService(References.MinimumLogLevel, minimumLogLevel), Effect.provide( - EnvironmentAuth.layerRuntime.pipe( + Layer.merge(EnvironmentAuth.layerRuntime, ServerSecretStore.layer).pipe( Layer.provide(ServerConfig.layer(config)), Layer.provide(Layer.succeed(References.MinimumLogLevel, minimumLogLevel)), ), @@ -803,6 +950,18 @@ const threadStartCommand = Command.make("start", { Flag.withDescription("Branch name for the new worktree."), Flag.optional, ), + requestId: Flag.String("request-id").pipe( + Flag.withDescription( + "Your id for this start. Running it again with the same id after a lost answer finds the thread it started instead of starting another.", + ), + Flag.optional, + ), + auto: Flag.Boolean("auto").pipe( + Flag.withDescription( + "Run it here or on a linked environment with spare capacity that has this repository and the exact --provider and --model. Needs --request-id.", + ), + Flag.withDefault(false), + ), wait: waitFlag, json: jsonFlag, }).pipe( @@ -814,34 +973,47 @@ const threadStartCommand = Command.make("start", { if (Option.isSome(flags.provider) !== Option.isSome(flags.model)) { return yield* new ThreadUsageError({ detail: "--provider and --model go together." }); } + if (flags.auto && (Option.isNone(flags.provider) || Option.isNone(flags.requestId))) { + return yield* new ThreadUsageError({ + detail: "--auto needs --provider, --model and --request-id.", + }); + } const title = yield* decodeTitle( Option.getOrElse(flags.title, () => titleFromMessage(message)), ).pipe(Effect.mapError(() => new ThreadUsageError({ detail: "The title is empty." }))); + const requestId = Option.getOrUndefined(flags.requestId); yield* withThreadTools( flags, - Effect.fn("cli.thread.start")(function* (tools) { + Effect.fn("cli.thread.start")(function* (tools, intent) { const project = yield* resolveProject(tools, flags.project); - const launched = yield* tools.call( - "t3_thread_launch", - { - projectId: project.id, - title, - message, - ...(workspaceStrategy === undefined ? {} : { workspaceStrategy }), - ...(Option.isSome(flags.provider) && Option.isSome(flags.model) - ? { - modelSelection: { - instanceId: flags.provider.value, - model: flags.model.value, - }, - } - : {}), - }, - ThreadLaunchResult, - ); + const launched = + intent !== undefined && Option.isSome(intent.recorded) + ? intent.recorded.value + : yield* tools.call( + "t3_thread_launch", + { + projectId: project.id, + title, + message, + ...(workspaceStrategy === undefined ? {} : { workspaceStrategy }), + ...(Option.isSome(flags.provider) && Option.isSome(flags.model) + ? { + modelSelection: { + instanceId: flags.provider.value, + model: flags.model.value, + }, + } + : {}), + ...(requestId === undefined ? {} : { clientRequestId: requestId }), + ...(flags.auto ? { placement: "auto" } : {}), + }, + ThreadLaunchResult, + ); + if (intent !== undefined) yield* intent.record(launched); if (flags.wait && launched.runId !== null) { if (!flags.json) yield* Console.error(`Thread ${launched.threadId}`); - return yield* finishRun(tools, { + // Placed elsewhere, the thread is only found there. + return yield* finishRun(toolsIn(tools, launched.environmentId), { threadId: launched.threadId, runId: launched.runId, json: flags.json, @@ -850,6 +1022,7 @@ const threadStartCommand = Command.make("start", { } yield* flags.json ? printJson(launched) : Console.log(launched.threadId); }), + requestId, ); }).pipe(reportThreadErrors), ), diff --git a/apps/server/src/mcp/McpToolAccess.ts b/apps/server/src/mcp/McpToolAccess.ts index 20a9274798d0f..228facf950d42 100644 --- a/apps/server/src/mcp/McpToolAccess.ts +++ b/apps/server/src/mcp/McpToolAccess.ts @@ -443,8 +443,17 @@ const routed = Effect.fn("McpToolAccess.routed")(function* Effect.Effect>; - handlers[name] = ({ environmentId, ...params }: { readonly environmentId?: EnvironmentId }) => + handlers[name] = ({ + environmentId, + ...requested + }: { + readonly environmentId?: EnvironmentId; + readonly placement?: unknown; + }) => Effect.gen(function* () { + // A named environment wins over automatic placement, here and there alike. + const { placement: _placement, ...pinned } = requested; + const params = environmentId === undefined ? requested : pinned; const scope = yield* McpInvocationContext.McpInvocationContext; const target = PeerForwarding.remoteTarget(scope, environmentId); if (target === undefined) return yield* here(params); diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index a4405d02c0ed6..3a2c917c83e27 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -2281,12 +2281,21 @@ const make = Effect.gen(function* () { parent.thread.interactionMode, input.interactionMode, ); + const here = yield* service.capabilities(callerScope, { search: model }); const environmentId = yield* remoteDelegation.place(scope, { + operation: "delegate-task", key, + threadId: scope.thread.threadId, projectId: parent.thread.projectId, modelSelection: { instanceId, model }, runtimeMode, - here: yield* service.capabilities(callerScope, { search: model }), + here: { + resources: here.hostResources ?? null, + modelAvailable: RemoteDelegation.runsModel(here, { + modelSelection: { instanceId, model }, + runtimeMode, + }), + }, }); return { environmentId, runtimeMode, interactionMode }; }); diff --git a/apps/server/src/mcp/toolkits/project/handlers.ts b/apps/server/src/mcp/toolkits/project/handlers.ts index 46774dedbc5ab..7361cfd7e9d69 100644 --- a/apps/server/src/mcp/toolkits/project/handlers.ts +++ b/apps/server/src/mcp/toolkits/project/handlers.ts @@ -1,10 +1,18 @@ import { 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 * as Digest from "effect/crypto/Digest"; import * as Effect from "effect/Effect"; import * as Hex from "effect/encoding/Hex"; @@ -102,6 +110,122 @@ const launchCommandId = Effect.fn("mcp.launchCommandId")(function* ( return CommandId.make(`mcp:launch:${Hex.encode(digest)}`); }); +type LaunchInput = Tool.Parameters; + +/** + * Where a launch with a key runs: the environment the key was placed in, whatever + * this request asks, or for `placement: "auto"` work needing nothing of this + * checkout, the one chosen now and bound before anything starts. The launch there, + * or undefined to launch it here. + */ +const launchPlacedThere = Effect.fn("mcp.launchPlacedThere")(function* ( + scope: McpInvocationContext.McpInvocationScope, + input: LaunchInput, + local: { + readonly projectId: ProjectId; + readonly threadId: ThreadId; + /** The workspace with the project's defaults applied, as it would start here. */ + readonly strategy: LaunchInput["workspaceStrategy"]; + /** The mode it would start with here, project defaults included. */ + readonly runtimeMode: RuntimeMode; + readonly interactionMode: ProviderInteractionMode; + }, +) { + const key = input.clientRequestId; + if (key === undefined && input.placement !== "auto") return undefined; + // Work a link sent here was placed on its own side, and never uses this side's links. + if (scope.client?.linked === true) return undefined; + // Without linked environments there is nowhere else it can be or go. + const delegation = yield* Effect.serviceOption(RemoteDelegation.RemoteDelegation); + if (Option.isNone(delegation)) return undefined; + const sendThere = Effect.fn("mcp.launchPlacedThere.send")(function* ( + environmentId: EnvironmentId, + placedKey: string, + ) { + const modelSelection = input.modelSelection; + if (modelSelection === undefined) + return yield* new OrchestratorMcpFailure({ + code: "invalid_request", + message: + "This clientRequestId runs in a linked environment it was placed in; pass the same modelSelection to reach it there.", + }); + const { + environmentId: _here, + projectId: _project, + placement: _placement, + delegatedFrom: _delegatedFrom, + workspaceStrategy: _requested, + ...params + } = input; + const launched = yield* delegation.value.launch(scope, { + environmentId, + key: placedKey, + threadId: local.threadId, + projectId: local.projectId, + // The workspace and modes it was admitted with go explicitly, so nothing there changes them. + params: { + ...params, + modelSelection, + ...(local.strategy === undefined ? {} : { workspaceStrategy: local.strategy }), + runtimeMode: local.runtimeMode, + interactionMode: local.interactionMode, + }, + }); + return { ...launched, environmentId }; + }); + if (key !== undefined) { + const bound = yield* delegation.value.placed(scope, { operation: "thread-launch", key }); + if (Option.isSome(bound)) + return bound.value === scope.environmentId ? undefined : yield* sendThere(bound.value, key); + } + if (input.placement !== "auto") return undefined; + const strategy = local.strategy; + if ( + (input.attachments?.length ?? 0) > 0 || + input.scratch === true || + strategy?.type === "existing_worktree" || + (strategy?.type === "worktree" && strategy.startFromOrigin !== true) + ) + return undefined; + const modelSelection = input.modelSelection; + if (key === undefined || modelSelection === undefined) + return yield* new OrchestratorMcpFailure({ + code: "invalid_request", + 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 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)), + }, + }); + return environmentId === scope.environmentId ? undefined : yield* sendThere(environmentId, key); +}); + const access = Effect.gen(function* () { yield* readCaller(); return yield* Project.ProjectService; @@ -179,15 +303,6 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { }), ), ); - if (project !== undefined && input.workspaceStrategy?.type === "existing_worktree") - yield* assertProjectWorktree(project.workspaceRoot, input.workspaceStrategy.worktreePath); - if (!replaying && input.modelSelection !== undefined) { - const { instanceId, model } = input.modelSelection; - const providers = yield* (yield* ProviderRegistry.ProviderRegistry).getProviders; - const provider = providers.find((candidate) => candidate.instanceId === instanceId); - if (provider !== undefined) - yield* OrchestrationMcp.requireAdvertisedModel(provider, model); - } const projectSettings = project === undefined ? undefined @@ -209,6 +324,37 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { startFromOrigin: projectSettings.newWorktreesStartFromOrigin, } : input.workspaceStrategy; + // A caller outside a thread has nothing to inherit, so it starts with the + // project's effective defaults, as the composer does. Its mode stays + // within the ceiling it was approved with. + const projectDefaults = caller === undefined ? projectSettings : undefined; + const defaultRuntimeMode = + projectDefaults !== undefined && input.runtimeMode === undefined + ? yield* resolveRuntimeMode(runtimeMode, projectDefaults.defaultRuntimeMode).pipe( + Effect.orElseSucceed(() => runtimeMode), + ) + : runtimeMode; + // A launch already made here replays here. Any other is placed, or reaches the + // environment its key was placed in, before anything only this checkout checks. + if (!replaying) { + const placedThere = yield* launchPlacedThere(scope, input, { + projectId, + threadId, + strategy, + runtimeMode: defaultRuntimeMode, + interactionMode, + }); + if (placedThere !== undefined) return placedThere; + } + if (project !== undefined && input.workspaceStrategy?.type === "existing_worktree") + yield* assertProjectWorktree(project.workspaceRoot, input.workspaceStrategy.worktreePath); + if (!replaying && input.modelSelection !== undefined) { + const { instanceId, model } = input.modelSelection; + const providers = yield* (yield* ProviderRegistry.ProviderRegistry).getProviders; + const provider = providers.find((candidate) => candidate.instanceId === instanceId); + if (provider !== undefined) + yield* OrchestrationMcp.requireAdvertisedModel(provider, model); + } // Reject unresolved base refs and blocked branch names before accepting a launch; worktree provisioning happens later. if (!replaying && strategy?.type === "worktree") yield* ThreadLaunch.ThreadLaunchService.pipe( @@ -223,10 +369,6 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { }), ), ); - // A caller outside a thread has nothing to inherit, so it starts with the - // project's effective defaults, as the composer does. Its mode stays - // within the ceiling it was approved with. - const projectDefaults = caller === undefined ? projectSettings : undefined; const modelSelection = input.modelSelection ?? launchedModel ?? @@ -250,12 +392,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { message: "Pass modelSelection: the project has no default model. orchestrator_capabilities lists providers and models.", }); - let startedRuntimeMode = - projectDefaults !== undefined && input.runtimeMode === undefined - ? yield* resolveRuntimeMode(runtimeMode, projectDefaults.defaultRuntimeMode).pipe( - Effect.orElseSucceed(() => runtimeMode), - ) - : runtimeMode; + let startedRuntimeMode = defaultRuntimeMode; if (!replaying) { const providers = yield* (yield* ProviderRegistry.ProviderRegistry).getProviders; startedRuntimeMode = yield* resolveRuntimeMode(startedRuntimeMode, input.runtimeMode, { diff --git a/apps/server/src/mcp/toolkits/project/tools.ts b/apps/server/src/mcp/toolkits/project/tools.ts index 957404aea573c..ac9b2a8ef67b8 100644 --- a/apps/server/src/mcp/toolkits/project/tools.ts +++ b/apps/server/src/mcp/toolkits/project/tools.ts @@ -15,6 +15,7 @@ import { Project, ProjectCreatePayload, ProjectUpdatePayload, + EnvironmentId, ProjectId, OrchestratorMcpClientRequestId, OrchestratorMcpEnvironmentTarget, @@ -158,12 +159,20 @@ const ThreadLaunchTool = Tool.make("t3_thread_launch", { ), attachments: Schema.optional(Schema.Array(McpAttachmentInput).check(Schema.isMaxLength(8))), clientRequestId: Schema.optional(OrchestratorMcpClientRequestId), + placement: Schema.optional( + Schema.Literals(["auto"]).annotate({ + description: + "'auto', without `environmentId`: here or on a linked environment with spare capacity, this repository's project and your exact `modelSelection`; there, in that project's checkout. Needs `modelSelection` and `clientRequestId`; a retry keeps the environment chosen. Attachments, scratch and worktrees not started from origin stay here.", + }), + ), /** Set by a linked environment launching its agent's delegated task here; ignored otherwise. */ delegatedFrom: Schema.optional(OrchestrationV2DelegatedFrom), }), success: Schema.Struct({ threadId: ThreadId, link: Schema.optional(Schema.String), + /** The linked environment placement chose; its tools take it to reach the thread. */ + environmentId: Schema.optional(EnvironmentId), projectId: ProjectId, modelSelection: ModelSelection, runId: Schema.NullOr(RunId), diff --git a/apps/server/src/peer/RemoteDelegation.test.ts b/apps/server/src/peer/RemoteDelegation.test.ts index ac480f51382d0..42f5919c5032d 100644 --- a/apps/server/src/peer/RemoteDelegation.test.ts +++ b/apps/server/src/peer/RemoteDelegation.test.ts @@ -672,6 +672,8 @@ const makeLaptop = ( readonly afterBindIntent?: (commandId: string) => Effect.Effect; /** Runs once delegation has read the link session it binds a launch to. */ readonly afterSession?: (session: Option.Option) => Effect.Effect; + /** Runs whenever something here reaches for a local launch, which this harness cannot make. */ + readonly localLaunch?: Effect.Effect; } = {}, ) => Effect.gen(function* () { @@ -697,7 +699,11 @@ const makeLaptop = ( Layer.provide(CommandReceiptStore.layer.pipe(Layer.provide(layerDatabase))), Layer.provideMerge(Layer.merge(layerOrchestrator, ServerSettings.layerTest())), ); - const layerHere = McpHttpServer.layerOrchestratorToolkit.pipe( + // Its project tools too, so an outside client here can launch top-level threads. + const layerHere = Layer.merge( + McpHttpServer.layerOrchestratorToolkit, + McpHttpServer.layerProjectRegistration, + ).pipe( Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), Layer.provideMerge( RemoteDelegation.layer.pipe( @@ -772,11 +778,34 @@ const makeLaptop = ( }), ), Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})), - Layer.provide(Layer.mock(ProjectService.ProjectService)({})), + Layer.provide( + Layer.mock(ProjectService.ProjectService)({ + getById: (id) => Effect.succeedSome({ ...laptopProject, id }), + }), + ), Layer.provide(Layer.mock(ProjectCloneTracker.ProjectCloneTracker)({})), Layer.provide(Layer.mock(SecretRequests.SecretRequests)({})), - Layer.provide(Layer.mock(ThreadLaunch.ThreadLaunchService)({})), + Layer.provide( + Layer.mock(ThreadLaunch.ThreadLaunchService)({ + launch: () => + (hooks.localLaunch ?? Effect.void).pipe(Effect.andThen(Effect.die("no local launch"))), + checkWorktreeBase: () => + (hooks.localLaunch ?? Effect.void).pipe(Effect.andThen(Effect.die("no local launch"))), + }), + ), Layer.provide(Layer.mock(GitVcsDriver.GitVcsDriver)({})), + Layer.provide( + Layer.merge( + Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ namedProjectsRoot: "/p" }), + Layer.mock(SourceControlRepositoryService.SourceControlRepositoryService)({}), + ), + ), + Layer.provide(DocumentText.layer.pipe(Layer.provideMerge(NodeServices.layer))), + Layer.provide( + ServerConfig.layerTest(process.cwd(), { prefix: "t3-remote-delegation-laptop-" }).pipe( + Layer.provide(NodeServices.layer), + ), + ), Layer.fresh, ); const here = yield* Layer.build(layerHere); @@ -815,6 +844,52 @@ const parentScope: McpInvocationContext.McpInvocationScope = { issuedAt: 1, }; +/** The laptop's project, with this repository, as its project tools read it. */ +const laptopProject: Project = { + id: ProjectId.make("project:laptop"), + title: "app", + workspaceRoot: "/home/me/app", + repositoryIdentity: null, + defaultModelSelection: null, + scripts: [], + createdAt: "2026-10-01T00:00:00.000Z", + updatedAt: "2026-10-01T00:00:00.000Z", + deletedAt: null, +}; + +/** An outside client of the laptop, such as the t3 CLI or another agent, in no thread. */ +const outsideScope: McpInvocationContext.McpInvocationScope = { + environmentId: laptop.environmentId, + requestNamespace: "client:outside-session", + thread: undefined, + client: { + sessionId: "outside-session", + label: "t3 thread cli", + access: "full-access", + linked: false, + }, + capabilities: new Set(["orchestration", "worktree", "pull-requests"]), + issuedAt: 1, +}; + +/** A top-level thread placed by capacity, for the box's Claude model. */ +const launchPlaced = ( + laptopEnv: Effect.Success>, + args: Record, +) => + laptopEnv.call( + "t3_thread_launch", + { + projectId: "project:laptop", + title: "Suite", + message: "Run the suite wherever there is room.", + modelSelection: { instanceId: boxClaude.instanceId, model: "claude-sonnet-4-6" }, + placement: "auto", + ...args, + }, + outsideScope, + ); + /** The laptop's parent thread, mid-turn when its agent delegates. */ const seedParent = ( laptopEnv: Effect.Success>, @@ -1461,6 +1536,146 @@ it.effect("an auto-placed task keeps its environment after a lost answer and a r ).pipe(Effect.provide(NodeHttpServer.layerTest)), ); +it.effect( + "an auto-placed top-level launch keeps its environment after a lost answer and a restart", + () => + Effect.scoped( + Effect.gen(function* () { + const thread = yield* makeBoxThread; + const b = yield* serveBox(thread); + const offers = yield* SubscriptionRef.make< + ReadonlyArray + >([]); + const database = yield* SqlitePersistence.layerMemory.pipe(Layer.build); + // Its link outlives the restart, so the box knows it as the same link session. + const linking = yield* layerLinkingEnvironment(laptop).pipe(Layer.build); + // The box starts the thread with room to spare, but its answer never arrives. + yield* Ref.set(thread.afterLaunch, Effect.die("connection lost")); + const lost = yield* Effect.scoped( + Effect.gen(function* () { + const before = yield* makeLaptop(database, offers, linking); + yield* linkTo(before.links, b, "full-access"); + return yield* launchPlaced(before, { clientRequestId: "top-level" }); + }), + ); + expect(lost.isError).toBe(true); + const lostThere = yield* Ref.get(thread.launched); + expect(lostThere).not.toBeNull(); + + // The box fills up while this side restarts: choosing afresh finds nowhere. + yield* Ref.set(thread.resources, loadOf(0.99)); + yield* Ref.set(thread.afterLaunch, Effect.void); + const after = yield* makeLaptop(database, offers, linking); + const fresh = yield* launchPlaced(after, { clientRequestId: "another" }); + expect(fresh.isError).toBe(true); + expect(JSON.stringify(fresh.content)).toContain("no-eligible-environment"); + // Without a key there is nothing a retry could find again, so nothing is placed. + const keyless = yield* launchPlaced(after, {}); + expect(keyless.isError).toBe(true); + expect(JSON.stringify(keyless.content)).toContain("clientRequestId"); + expect(yield* Ref.get(thread.launched)).toBe(lostThere); + + // The retry keeps the binding: the same thread there, with the exact model and modes. + const retried = yield* launchPlaced(after, { clientRequestId: "top-level" }); + expect(retried.isError, JSON.stringify(retried.content)).toBe(false); + expect((retried.structuredContent as { threadId: ThreadId }).threadId).toBe(lostThere); + expect(yield* Ref.get(thread.launchedWith)).toEqual({ + modelSelection: { instanceId: boxClaude.instanceId, model: "claude-sonnet-4-6" }, + runtimeMode: "full-access", + interactionMode: "default", + }); + + // A named environment wins over placement: it launches there although the box is full, + // in the project named with that environment's ids. + const pinned = yield* launchPlaced(after, { + environmentId: box.environmentId, + projectId: boxProject, + clientRequestId: "pinned", + }); + expect(pinned.isError, JSON.stringify(pinned.content)).toBe(false); + expect((pinned.structuredContent as { threadId: ThreadId }).threadId).not.toBe(lostThere); + }), + ).pipe(Effect.provide(NodeHttpServer.layerTest)), +); + +it.effect("an outside client's auto-placed launch starts at the project's default mode", () => + Effect.scoped( + Effect.gen(function* () { + const thread = yield* makeBoxThread; + const b = yield* serveBox(thread); + const offers = yield* SubscriptionRef.make< + ReadonlyArray + >([]); + const database = yield* SqlitePersistence.layerMemory.pipe(Layer.build); + const a = yield* makeLaptop(database, offers); + // The client may run anything; this project starts its threads approval-required. + yield* a.settings.updateSettings({ + projectSettingsOverrides: { + [laptopProject.id]: { defaultRuntimeMode: "approval-required" }, + }, + }); + yield* linkTo(a.links, b, "full-access"); + const launched = yield* launchPlaced(a, { clientRequestId: "defaults" }); + expect(launched.isError, JSON.stringify(launched.content)).toBe(false); + expect((yield* Ref.get(thread.launchedWith))?.runtimeMode).toBe("approval-required"); + }), + ).pipe(Effect.provide(NodeHttpServer.layerTest)), +); + +it.effect( + "a retry that drops placement or asks for this checkout reaches the thread placed there", + () => + Effect.scoped( + Effect.gen(function* () { + const thread = yield* makeBoxThread; + const b = yield* serveBox(thread); + const offers = yield* SubscriptionRef.make< + ReadonlyArray + >([]); + const database = yield* SqlitePersistence.layerMemory.pipe(Layer.build); + const localLaunches = yield* Ref.make(0); + const a = yield* makeLaptop(database, offers, undefined, Effect.void, { + localLaunch: Ref.update(localLaunches, (count) => count + 1), + }); + yield* linkTo(a.links, b, "full-access"); + // The box takes the launch, but its answer is lost. + yield* Ref.set(thread.afterLaunch, Effect.die("connection lost")); + expect((yield* launchPlaced(a, { clientRequestId: "kept" })).isError).toBe(true); + const placedThere = yield* Ref.get(thread.launched); + expect(placedThere).not.toBeNull(); + yield* Ref.set(thread.afterLaunch, Effect.void); + + // The same request without placement is the same intent: it reaches that thread. + const unplaced = yield* a.call( + "t3_thread_launch", + { + projectId: "project:laptop", + title: "Suite", + message: "Run the suite wherever there is room.", + modelSelection: { instanceId: boxClaude.instanceId, model: "claude-sonnet-4-6" }, + clientRequestId: "kept", + }, + outsideScope, + ); + expect(unplaced.isError, JSON.stringify(unplaced.content)).toBe(false); + expect(unplaced.structuredContent).toMatchObject({ + threadId: placedThere, + environmentId: box.environmentId, + }); + // So does one asking for a worktree only this checkout has. + const local = yield* launchPlaced(a, { + clientRequestId: "kept", + workspaceStrategy: { type: "worktree", baseRef: "main", startFromOrigin: false }, + }); + expect(local.isError, JSON.stringify(local.content)).toBe(false); + expect((local.structuredContent as { threadId: ThreadId }).threadId).toBe(placedThere); + // Nothing here ever reached for a launch of its own. + expect(yield* Ref.get(localLaunches)).toBe(0); + expect(yield* Ref.get(thread.launched)).toBe(placedThere); + }), + ).pipe(Effect.provide(NodeHttpServer.layerTest)), +); + it.effect("a launch whose answer was lost is never started again under a new link", () => Effect.scoped( Effect.gen(function* () { diff --git a/apps/server/src/peer/RemoteDelegation.ts b/apps/server/src/peer/RemoteDelegation.ts index 5ef7b4a690414..e30b65c590d25 100644 --- a/apps/server/src/peer/RemoteDelegation.ts +++ b/apps/server/src/peer/RemoteDelegation.ts @@ -19,6 +19,7 @@ import { type ProjectId, type ProviderInstanceId, ProviderDriverKind, + type HostResourcesSnapshot, type RunId, type RuntimeMode, type ThreadId, @@ -111,17 +112,52 @@ export class RemoteDelegation extends Context.Service< * answers, bound before anything starts. A retry never chooses again. */ readonly place: ( - scope: ThreadScope, + scope: McpInvocationContext.McpInvocationScope, input: { + /** What is placed: one caller's key binds one environment per operation. */ + readonly operation: "delegate-task" | "thread-launch"; readonly key: string; + /** The thread the binding is recorded against. */ + readonly threadId: ThreadId; readonly projectId: ProjectId; readonly modelSelection: Pick; - /** The parent's effective mode or a narrower one asked for, sent explicitly. */ + /** The effective mode, sent explicitly wherever the work runs. */ readonly runtimeMode: RuntimeMode; - /** This environment's own `orchestrator_capabilities`, searched for the model. */ - readonly here: OrchestratorMcpCapabilitiesResult; + /** This environment's own load, and whether it runs the exact model in that mode. */ + readonly here: { + readonly resources: HostResourcesSnapshot | null; + readonly modelAvailable: boolean; + }; }, ) => Effect.Effect; + /** The environment a caller's key was placed in, if it was. */ + readonly placed: ( + scope: McpInvocationContext.McpInvocationScope, + input: { readonly operation: "delegate-task" | "thread-launch"; readonly key: string }, + ) => Effect.Effect, OrchestratorMcpFailure>; + /** + * A top-level `t3_thread_launch` there, sent only under the link session its + * key was first bound to: a retry after linking again never starts a second + * thread there, and refuses instead of guessing which one is its. + */ + readonly launch: ( + scope: McpInvocationContext.McpInvocationScope, + input: { + readonly environmentId: EnvironmentId; + readonly key: string; + /** The thread the bindings are recorded against. */ + readonly threadId: ThreadId; + /** The project here; it runs in the project there with the same repository. */ + readonly projectId: ProjectId; + readonly params: Omit< + Tool.Parameters, + "projectId" + >; + }, + ) => Effect.Effect< + Tool.Success, + OrchestratorMcpFailure + >; /** * A `t3_thread_send` there that started or queued a run on the thread of * one of this thread's tasks that has its result: like a follow-up to a @@ -164,7 +200,7 @@ export class RemoteDelegation extends Context.Service< >()("t3/peer/RemoteDelegation") {} /** The exact instance and model there can run a child task, in the mode asked for. */ -const runsModel = ( +export const runsModel = ( capabilities: OrchestratorMcpCapabilitiesResult, requested: { readonly modelSelection: Pick; @@ -221,7 +257,7 @@ const make = Effect.gen(function* () { /** The peer's project with this thread's repository, or the one the caller named. */ const resolveRemoteProject = Effect.fn("RemoteDelegation.resolveRemoteProject")(function* ( - scope: ThreadScope, + scope: McpInvocationContext.McpInvocationScope, environmentId: EnvironmentId, parentProjectId: ProjectId, requested: ProjectId | undefined, @@ -308,7 +344,7 @@ const make = Effect.gen(function* () { /** A linked environment as a placement candidate, from what it answered just now. */ const peerCandidate = Effect.fn("RemoteDelegation.peerCandidate")( function* ( - scope: ThreadScope, + scope: McpInvocationContext.McpInvocationScope, link: PeerLinkSummary, input: Parameters[1], ) { @@ -353,21 +389,32 @@ const make = Effect.gen(function* () { ), ); + const placementBinding = ( + scope: McpInvocationContext.McpInvocationScope, + input: { readonly operation: string; readonly key: string }, + ) => + CommandId.make( + `command:mcp:${encodeURIComponent(scope.requestNamespace)}:placement:${input.operation}:${encodeURIComponent(input.key)}`, + ); + const bindingFailure = () => + new OrchestratorMcpFailure({ + code: "orchestration_error", + message: + "Unable to record where this task runs, so nothing was started. Retry with the same clientRequestId.", + }); + + const placed: RemoteDelegation["Service"]["placed"] = (scope, input) => + threads + .intentOf(placementBinding(scope, input)) + .pipe(Effect.map(Option.map(EnvironmentId.make)), Effect.mapError(bindingFailure)); + const place: RemoteDelegation["Service"]["place"] = Effect.fn("RemoteDelegation.place")( function* (scope, input) { - const bindingId = CommandId.make( - `command:mcp:${encodeURIComponent(scope.requestNamespace)}:placement:delegate-task:${encodeURIComponent(input.key)}`, - ); - const bindingFailure = () => - new OrchestratorMcpFailure({ - code: "orchestration_error", - message: - "Unable to record where this task runs, so nothing was started. Retry with the same clientRequestId.", - }); + const bindingId = placementBinding(scope, input); // A bound intent keeps its environment, even one that has since filled up or // stopped answering: its first attempt may have started the task there. - const bound = yield* threads.intentOf(bindingId).pipe(Effect.mapError(bindingFailure)); - if (Option.isSome(bound)) return EnvironmentId.make(bound.value); + const bound = yield* placed(scope, input); + if (Option.isSome(bound)) return bound.value; const linked = (yield* links.enabled) ? yield* links.list.pipe(Effect.orElseSucceed(() => [])) @@ -389,12 +436,12 @@ const make = Effect.gen(function* () { [ { environmentId: hereId, - resources: input.here.hostResources ?? null, + resources: input.here.resources, receivedAt: now, weight: 1, operable: true, projectAvailable: true, - modelAvailable: runsModel(input.here, input), + modelAvailable: input.here.modelAvailable, }, ...peers.flatMap((peer) => (Option.isSome(peer) ? [peer.value] : [])), ], @@ -407,19 +454,125 @@ const make = Effect.gen(function* () { if (!decision.accepted) { return yield* new OrchestratorMcpFailure({ code: "target_required", - message: `No environment can take this task now (${decision.reason}): none answered with spare capacity, this repository's project and ${input.modelSelection.instanceId}/${input.modelSelection.model}. Nothing was started. Pass target.environmentId, or omit placement to run it here.`, + message: `No environment can take this task now (${decision.reason}): none answered with spare capacity, this repository's project and ${input.modelSelection.instanceId}/${input.modelSelection.model}. Nothing was started. Name an environment, or omit placement to run it here.`, }); } return yield* threads .bindIntent({ commandId: bindingId, - threadId: scope.thread.threadId, + threadId: input.threadId, value: decision.environmentId, }) .pipe(Effect.map(EnvironmentId.make), Effect.mapError(bindingFailure)); }, ); + /** + * The other side names the thread from its link session and the key. The first launch's + * session and answered thread are bound here, and it is sent only under that session; a + * key bound under another one is never sent again, so a re-link never starts a second. + */ + const launchThere = Effect.fn("RemoteDelegation.launchThere")(function* ( + scope: McpInvocationContext.McpInvocationScope, + input: { + readonly environmentId: EnvironmentId; + readonly label: string; + readonly launchId: CommandId; + readonly childId: CommandId; + readonly threadId: ThreadId; + readonly params: Tool.Parameters; + }, + ) { + const { label } = input; + const linkSession = yield* links.session(input.environmentId).pipe( + Effect.map(Option.getOrUndefined), + Effect.mapError( + () => + new OrchestratorMcpFailure({ + code: "orchestration_error", + message: `Unable to read the link to ${label}, so nothing was started. Retry with the same clientRequestId.`, + }), + ), + ); + // A launch is only sent bound to the link session it goes out under; with no link + // there is none, and one appearing before the send must not carry it unbound. + if (linkSession === undefined) { + return yield* new OrchestratorMcpFailure({ + code: "invalid_request", + message: `This environment is not linked to ${label}, so nothing was started. t3_environment_links lists the linked ones.`, + }); + } + const session = `${input.environmentId}@${linkSession}`; + const launchedUnder = yield* threads + .bindIntent({ commandId: input.launchId, threadId: input.threadId, value: session }) + .pipe( + Effect.mapError( + () => + new OrchestratorMcpFailure({ + code: "orchestration_error", + message: + "Unable to record the launch there, so nothing was started. Retry with the same clientRequestId.", + }), + ), + ); + if (launchedUnder !== session) { + const childThreadId = yield* threads.intentOf(input.childId).pipe( + Effect.map(Option.getOrUndefined), + Effect.orElseSucceed(() => undefined), + ); + return { _tag: "bound-elsewhere" as const, childThreadId }; + } + const launched = yield* forwarding.call( + scope, + ProjectToolkit.tools.t3_thread_launch, + input.environmentId, + input.params, + // Sent only under the session bound above, never one linked since. + { expectedIncarnation: linkSession }, + ); + // Losing this only makes a retry after linking again refuse instead of finding the thread. + yield* threads + .bindIntent({ commandId: input.childId, threadId: input.threadId, value: launched.threadId }) + .pipe(Effect.ignoreCause({ log: true })); + return { _tag: "launched" as const, launched }; + }); + + const launch: RemoteDelegation["Service"]["launch"] = Effect.fn("RemoteDelegation.launch")( + function* (scope, input) { + const keyed = (operation: string) => + CommandId.make( + `command:mcp:${encodeURIComponent(scope.requestNamespace)}:${operation}:${encodeURIComponent(input.key)}`, + ); + const label = yield* links.get(input.environmentId).pipe( + Effect.map((link) => (Option.isSome(link) ? link.value.label : input.environmentId)), + Effect.orElseSucceed(() => input.environmentId), + ); + const projectId = yield* resolveRemoteProject( + scope, + input.environmentId, + input.projectId, + undefined, + ); + const sent = yield* launchThere(scope, { + environmentId: input.environmentId, + label, + launchId: keyed("thread-launch-there"), + childId: keyed("thread-launch-child"), + threadId: input.threadId, + params: { ...input.params, projectId }, + }); + if (sent._tag === "launched") return sent.launched; + return yield* new OrchestratorMcpFailure({ + code: "orchestration_error", + message: `${label} was linked again after an earlier attempt with this clientRequestId launched the thread there, and the new link cannot find it. Nothing new was started. ${ + sent.childThreadId === undefined + ? "t3_thread_list with this environmentId finds it." + : `Its thread there is ${sent.childThreadId}.` + } This clientRequestId stays bound to the earlier link. Check that thread there before deciding the work still needs doing.`, + }); + }, + ); + const delegate: RemoteDelegation["Service"]["delegate"] = Effect.fn("RemoteDelegation.delegate")( function* (scope, input) { const parent = yield* threads.getThreadRecords(scope.thread.threadId, ["runs"]).pipe( @@ -510,50 +663,32 @@ const make = Effect.gen(function* () { Effect.orElseSucceed(() => undefined), ); const label = link?.label ?? target.environmentId; - // The other side names the thread from its link session and the key, so a - // retry after linking again would start a second one. The session that took - // the first launch and the thread it answered with are kept here. - const childId = keyed("remote-child"); - const linkSession = yield* links.session(target.environmentId).pipe( - Effect.map(Option.getOrUndefined), - Effect.mapError( - () => - new OrchestratorMcpFailure({ - code: "orchestration_error", - message: `Unable to read the link to ${label}, so nothing was started. Retry with the same clientRequestId.`, - }), - ), - ); - // A launch is only sent bound to the link session it goes out under; with no link - // there is none, and one appearing before the send must not carry it unbound. - if (linkSession === undefined) { - return yield* new OrchestratorMcpFailure({ - code: "invalid_request", - message: `This environment is not linked to ${label}, so nothing was started. t3_environment_links lists the linked ones.`, - }); - } - const session = `${target.environmentId}@${linkSession}`; - const launchedUnder = yield* threads - .bindIntent({ - commandId: keyed("remote-launch"), - threadId: parent.thread.id, - value: session, - }) - .pipe( - Effect.mapError( - () => - new OrchestratorMcpFailure({ - code: "orchestration_error", - message: - "Unable to record the launch there, so nothing was started. Retry with the same clientRequestId.", - }), - ), - ); - if (launchedUnder !== session) { - const childThere = yield* threads.intentOf(childId).pipe( - Effect.map(Option.getOrUndefined), - Effect.orElseSucceed(() => undefined), - ); + const task = taskPrompt(input); + const title = input.title ?? parent.thread.title; + const sent = yield* launchThere(scope, { + environmentId: target.environmentId, + label, + launchId: keyed("remote-launch"), + childId: keyed("remote-child"), + threadId: parent.thread.id, + params: { + projectId, + title, + modelSelection: { instanceId, model, ...(options === undefined ? {} : { options }) }, + ...(runtimeMode === undefined ? {} : { runtimeMode }), + ...(interactionMode === undefined ? {} : { interactionMode }), + message: task, + clientRequestId: `delegate:${scope.thread.threadId}:${key}`, + // The thread there shows it is this thread's subagent, with a way back. + delegatedFrom: { + environmentId: hereId, + threadId: parent.thread.id, + title: parent.thread.title, + }, + }, + }); + if (sent._tag === "bound-elsewhere") { + const childThere = sent.childThreadId; const recordedTask = childThere === undefined ? undefined @@ -577,34 +712,7 @@ const make = Effect.gen(function* () { } This clientRequestId stays bound to the earlier link. Check that thread there before deciding the work still needs doing.`, }); } - const task = taskPrompt(input); - const title = input.title ?? parent.thread.title; - const launched = yield* forwarding.call( - scope, - ProjectToolkit.tools.t3_thread_launch, - target.environmentId, - { - projectId, - title, - modelSelection: { instanceId, model, ...(options === undefined ? {} : { options }) }, - ...(runtimeMode === undefined ? {} : { runtimeMode }), - ...(interactionMode === undefined ? {} : { interactionMode }), - message: task, - clientRequestId: `delegate:${scope.thread.threadId}:${key}`, - // The thread there shows it is this thread's subagent, with a way back. - delegatedFrom: { - environmentId: hereId, - threadId: parent.thread.id, - title: parent.thread.title, - }, - }, - // Sent only under the session bound above, never one linked since. - { expectedIncarnation: linkSession }, - ); - // Losing this only makes a retry after linking again refuse instead of finding the task. - yield* threads - .bindIntent({ commandId: childId, threadId: parent.thread.id, value: launched.threadId }) - .pipe(Effect.ignoreCause({ log: true })); + const launched = sent.launched; const remoteChild: OrchestrationV2RemoteTaskChild = { environmentId: target.environmentId, threadId: launched.threadId, @@ -1268,7 +1376,16 @@ const make = Effect.gen(function* () { }).pipe(Effect.ignoreCause({ log: true })), ); - return RemoteDelegation.of({ delegate, place, followUp, cancel, runStatus, start }); + return RemoteDelegation.of({ + delegate, + place, + placed, + launch, + followUp, + cancel, + runStatus, + start, + }); }); /** A defect, logged, as a failure, so a retry covers it. */