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
8 changes: 6 additions & 2 deletions .github/workflows/closura-arc-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,12 @@ jobs:
run: |
set -euo pipefail
vp run --filter @t3tools/contracts typecheck
vp run --filter @t3tools/shared typecheck
vp run --filter @t3tools/client-runtime typecheck
vp run --filter @t3tools/provider-core typecheck
vp run --filter t3 typecheck

- name: Test AssemblyAI dictation and native inbox behavior
- name: Test AssemblyAI dictation, native inbox and thread placement behavior
shell: bash
run: |
set -euo pipefail
Expand All @@ -146,7 +147,10 @@ jobs:
packages/contracts/src/threadMetadataMcp.test.ts \
apps/web/src/components/threadActionMenu.logic.test.ts \
apps/server/src/orchestration-v2/ThreadLaunchService.test.ts \
apps/server/src/orchestration-v2/SubagentProjection.test.ts
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

- name: Build native T3 server
shell: bash
Expand Down
16 changes: 16 additions & 0 deletions apps/server/src/mcp/OrchestratorMcpService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2906,6 +2906,22 @@ describe("OrchestratorMcpService provider resolution", () => {
assert.equal(child.providerInstanceId, selection.instanceId);
assert.equal(child.model, selection.model);
assert.deepEqual(yield* service.createThreads(scope, request), first);
// Its threads share this checkout, so another environment is refused, not ignored.
const elsewhere = yield* service
.createThreads(scope, {
threads: [
{
target: {
...request.threads[0]!.target,
environmentId: EnvironmentId.make("environment-elsewhere"),
},
},
],
clientRequestId: "elsewhere",
})
.pipe(Effect.flip);
assert.equal(elsewhere.code, "invalid_request");
assert.include(elsewhere.message, "t3_thread_launch");

// The creation receipt exists, but this prompt has not been accepted yet.
const withPrompt = {
Expand Down
121 changes: 114 additions & 7 deletions apps/server/src/mcp/OrchestratorMcpService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
CommandId,
EnvironmentId,
type RunId,
isProviderAvailable,
MessageId,
Expand Down Expand Up @@ -71,6 +72,10 @@ import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2Pe
import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
import { formatThreadLink } from "@t3tools/shared/threadLinks";
import {
chooseThreadPlacement,
type ThreadPlacementDecision,
} from "@t3tools/shared/threadPlacement";
import { threadPullRequestUpdatedAtMs } from "@t3tools/shared/threadPullRequests";
import {
normalizeThreadPullRequestKey,
Expand Down Expand Up @@ -2224,7 +2229,69 @@ const make = Effect.gen(function* () {
return task;
});

return OrchestratorMcpService.of({
/** Where a placed task may run, or why it may not. */
const admitPlacement = (decision: ThreadPlacementDecision, conflict: string) =>
decision.accepted
? Effect.succeed(EnvironmentId.make(decision.environmentId))
: Effect.fail(
failure(
"invalid_request",
decision.reason === "conflicting-locality"
? conflict
: `No environment can take this (${decision.reason}).`,
),
);

/** The environment bound to an auto-placed task, for the parent's model or the exact one named. */
const placeTask = Effect.fn("OrchestratorMcpService.placeTask")(function* (
callerScope: McpInvocationScope,
input: OrchestratorMcpDelegateTaskInput,
key: string,
) {
yield* requireCapability(callerScope);
const { scope, parent } = yield* loadThreadCaller(callerScope, "delegate_task");
const target = input.target;
// Placement qualifies the project with this repository wherever it runs, and no other.
if (target?.projectId !== undefined) {
return yield* failure(
"invalid_request",
'With placement "auto", omit target.projectId: the task runs in the project with this repository wherever it is placed. To use another project, pass target.environmentId with it.',
);
}
if (target?.driverKind !== undefined && target.providerInstanceId === undefined) {
return yield* failure(
"invalid_request",
'With placement "auto", name target.providerInstanceId instead of driverKind; every environment matches it exactly.',
);
}
const inherited = parent.thread.modelSelection;
const instanceId = target?.providerInstanceId ?? inherited.instanceId;
const model =
target?.model ?? (instanceId === inherited.instanceId ? inherited.model : undefined);
if (model === undefined) {
return yield* failure(
"target_required",
'With placement "auto" and another providerInstanceId, pass target.model.',
);
}
// The parent's modes, or narrower ones asked for, are chosen with and sent explicitly,
// so no environment qualifies or runs the task at less than they allow.
const runtimeMode = yield* resolveRuntimeMode(parent.thread.runtimeMode, input.runtimeMode);
const interactionMode = yield* resolveInteractionMode(
parent.thread.interactionMode,
input.interactionMode,
);
const environmentId = yield* remoteDelegation.place(scope, {
key,
projectId: parent.thread.projectId,
modelSelection: { instanceId, model },
runtimeMode,
here: yield* service.capabilities(callerScope, { search: model }),
});
return { environmentId, runtimeMode, interactionMode };
});

const service: OrchestratorMcpServiceShape = OrchestratorMcpService.of({
scheduleTask: Effect.fn("OrchestratorMcpService.scheduleTask")(function* (scope, input) {
const { parent, limits } = yield* loadCaller(scope, ["runs"]);
const projectId = yield* resolveProjectTarget(parent, input.projectId);
Expand Down Expand Up @@ -2642,16 +2709,40 @@ const make = Effect.gen(function* () {
};
}),
delegateTask: Effect.fn("OrchestratorMcpService.delegateTask")(function* (callerScope, input) {
const environmentId =
input.target === undefined
? undefined
: PeerForwarding.remoteTarget(callerScope, input.target.environmentId);
if (input.target !== undefined && environmentId !== undefined) {
// One key for the placement, the launch and the record, so a retry finds all three.
const clientRequestId = yield* requestKey(input.clientRequestId);
const named = input.target?.environmentId;
// A named target or the parent's checkout decides; only work needing neither is placed.
const automatic =
input.placement === "auto" && named === undefined && input.isolation === undefined
? yield* placeTask(callerScope, input, clientRequestId)
: undefined;
const placed =
automatic !== undefined
? automatic.environmentId
: yield* admitPlacement(
chooseThreadPlacement(
{
originEnvironmentId: callerScope.environmentId,
pinnedEnvironmentId: named ?? callerScope.environmentId,
requiresOrigin: input.isolation === "worktree",
},
[],
0,
),
'A task in a linked environment runs in that project\'s checkout; isolation "worktree" is not supported there. Omit isolation, or use t3_thread_launch with environmentId and a worktree workspaceStrategy.',
);
const environmentId = PeerForwarding.remoteTarget(callerScope, placed);
if (environmentId !== undefined) {
yield* requireCapability(callerScope);
const scope = yield* requireThreadScope(callerScope, "delegate_task");
// The task's child runs as an ordinary thread there; this thread keeps the task.
const { taskId } = yield* remoteDelegation.delegate(scope, {
...input,
...(automatic === undefined
? {}
: { runtimeMode: automatic.runtimeMode, interactionMode: automatic.interactionMode }),
clientRequestId,
target: { ...input.target, environmentId },
});
return yield* input.mode === "wait"
Expand Down Expand Up @@ -2682,7 +2773,7 @@ const make = Effect.gen(function* () {
);
}
const providers = yield* loadProviders;
const key = yield* requestKey(input.clientRequestId);
const key = clientRequestId;
const commandId = stableCommandId({
scope,
requestKey: key,
Expand Down Expand Up @@ -3021,6 +3112,21 @@ const make = Effect.gen(function* () {
createThreads: Effect.fn("OrchestratorMcpService.createThreads")(
function* (callerScope, input) {
const { scope, parent } = yield* loadThreadCaller(callerScope, "create_threads");
// Each thread shares this thread's checkout, so another environment cannot take it.
for (const request of input.threads) {
yield* admitPlacement(
chooseThreadPlacement(
{
originEnvironmentId: callerScope.environmentId,
pinnedEnvironmentId: request.target?.environmentId,
requiresOrigin: true,
},
[],
0,
),
"create_threads starts threads in this checkout. To start one in a linked environment, use t3_thread_launch with that environmentId.",
);
}
const parentRun = ThreadManagementService.latestActiveRun(parent);
if (
parentRun === undefined ||
Expand Down Expand Up @@ -3604,6 +3710,7 @@ const make = Effect.gen(function* () {
} satisfies OrchestratorMcpThreadInterruptResult;
}),
});
return service;
});

export const layer: Layer.Layer<
Expand Down
53 changes: 53 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,19 @@ 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>;
/** What a caller's intent was bound to, such as the environment chosen for it, if it was. */
readonly intentOf: (
commandId: CommandId,
) => Effect.Effect<Option.Option<string>, CommandReceiptStore.CommandReceiptStoreV2Error>;
/**
* Binds a caller's intent to a value unless it is bound already, and returns
* the binding that holds: the first writer's, read back from storage.
*/
readonly bindIntent: (input: {
readonly commandId: CommandId;
readonly threadId: ThreadId;
readonly value: string;
}) => Effect.Effect<string, CommandReceiptStore.CommandReceiptStoreV2Error>;
readonly waitForThread: (
input: ThreadManagementWaitInput,
) => Effect.Effect<ThreadManagementWaitResult, ThreadManagementError>;
Expand Down Expand Up @@ -604,6 +617,8 @@ const FORCE_STOP_REASON = "Force stopped by user";
const FORCE_STOP_RELEASE_WAIT = "10 seconds";
const isForceStopThreadError = Schema.is(OrchestrationV2ForceStopThreadError);

const INTENT_RECEIPT_TYPE = "intent.bind:";

const make = Effect.gen(function* () {
const threadDispatch = yield* ThreadCommandExecutor.ThreadCommandExecutor;
const orchestrator = yield* Orchestrator.OrchestratorV2;
Expand Down Expand Up @@ -651,6 +666,42 @@ const make = Effect.gen(function* () {
Effect.map(Option.isSome),
);

// 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) =>
receipts
.getByCommandId(commandId)
.pipe(
Effect.map(
Option.flatMap((receipt) =>
receipt.commandType.startsWith(INTENT_RECEIPT_TYPE)
? Option.some(receipt.commandType.slice(INTENT_RECEIPT_TYPE.length))
: Option.none(),
),
),
);

const bindIntent: ThreadManagementServiceShape["bindIntent"] = Effect.fn(
"orchestrationV2.threadManagement.bindIntent",
)(function* (input) {
yield* receipts.insertIfAbsent({
commandId: input.commandId,
threadId: input.threadId,
commandType: `${INTENT_RECEIPT_TYPE}${input.value}`,
acceptedAt: yield* DateTime.now,
resultSequence: 0,
status: "accepted",
error: null,
});
const bound = yield* intentOf(input.commandId);
if (Option.isNone(bound)) {
return yield* new CommandReceiptStore.CommandReceiptStoreReadError({
commandId: input.commandId,
});
}
return bound.value;
});

const ensureLegacyTranscript = Effect.fn(
"orchestrationV2.threadManagement.ensureLegacyTranscript",
)(function* (threadId: ThreadId) {
Expand Down Expand Up @@ -1768,6 +1819,8 @@ const make = Effect.gen(function* () {
assertProviderInstanceEnabled,
isProviderInstanceEnabledForProject,
hasReceipt,
intentOf,
bindIntent,
waitForThread,
settleAfterRun,
settleThread,
Expand Down
16 changes: 14 additions & 2 deletions apps/server/src/peer/PeerForwarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ export class PeerForwarding extends Context.Service<
/**
* Runs `tool` in the linked environment `environmentId` with `params`. A
* `t3_thread_wait` goes in pieces short enough to survive any route.
* `expectedIncarnation` sends it only under that link session.
*/
readonly call: <T extends Tool.Any>(
scope: Scope,
tool: T,
environmentId: EnvironmentId,
params: Tool.Parameters<T>,
options?: { readonly expectedIncarnation?: string },
) => Effect.Effect<Tool.Success<T>, OrchestratorMcpFailure>;
/** `t3_thread_read` there. */
readonly readThread: (
Expand Down Expand Up @@ -155,6 +157,7 @@ const make = Effect.gen(function* () {
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>)(
Expand Down Expand Up @@ -187,6 +190,7 @@ const make = Effect.gen(function* () {
arguments: args,
limits,
...(sender === undefined ? {} : { sender }),
...(expectedIncarnation === undefined ? {} : { expectedIncarnation }),
success: success as Schema.Decoder<A>,
});
});
Expand Down Expand Up @@ -237,7 +241,7 @@ const make = Effect.gen(function* () {
Effect.flatMap((enabled) => (enabled ? Effect.void : Effect.fail(linkedEnvironmentsOff))),
);

const call: PeerForwarding["Service"]["call"] = (scope, tool, environmentId, params) => {
const call: PeerForwarding["Service"]["call"] = (scope, tool, environmentId, params, options) => {
const forwarded: Effect.Effect<
Tool.Success<typeof tool>,
OrchestratorMcpFailure
Expand All @@ -247,7 +251,15 @@ const make = Effect.gen(function* () {
environmentId,
params as OrchestratorMcpThreadWaitInput,
) as Effect.Effect<Tool.Success<typeof tool>, OrchestratorMcpFailure>)
: forward(scope, tool.name, tool.parametersSchema, tool.successSchema, environmentId, params);
: forward(
scope,
tool.name,
tool.parametersSchema,
tool.successSchema,
environmentId,
params,
options?.expectedIncarnation,
);
return Effect.andThen(requireEnabled, forwarded);
};

Expand Down
Loading
Loading