Repository navigation
Expand file tree
/
Copy paththreadAccess.ts
More file actions
255 lines (237 loc) · 9.79 KB
/
Copy paththreadAccess.ts
File metadata and controls
255 lines (237 loc) · 9.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import type {
ProjectionRecordField,
ProjectionRecordFilter,
} from "../orchestration-v2/ProjectionStore.ts";
import {
CommandId,
OrchestratorMcpFailure,
type ProjectId,
type ProviderInstanceId,
type ProviderInteractionMode,
type RuntimeMode,
ThreadId,
type OrchestrationV2ThreadShell,
} from "@t3tools/contracts";
import * as SecureRandom from "effect/crypto/SecureRandom";
import * as Effect from "effect/Effect";
import type { OrchestratorV2Error } from "../orchestration-v2/Orchestrator.ts";
import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts";
import { resolveInteractionMode, resolveRuntimeMode } from "./delegatedTaskModes.ts";
import { fencingLinkOrigin } from "./linkOrigin.ts";
import * as McpInvocationContext from "./McpInvocationContext.ts";
const delegatedTaskPrefix = "thread:delegated-task:";
const suggestedThreadId = (threadId: ThreadId) => {
if (!threadId.startsWith(delegatedTaskPrefix)) return ThreadId.make(`mcp:${threadId}`);
const rest = threadId.slice(delegatedTaskPrefix.length);
// encodeURIComponent throws on lone surrogates, which a JSON string can carry.
if (!rest.isWellFormed()) return null;
const encoded = encodeURIComponent(rest);
return encoded === rest ? null : ThreadId.make(`${delegatedTaskPrefix}${encoded}`);
};
/**
* Agents paste bare uuids for `mcp:<uuid>` threads and unencoded delegated-task ids.
* The id is never rewritten, so permission checks keyed on it still hold; the miss
* only names a live thread the caller could read by its full id. Ids from another
* machine miss here too. The link hint is static: listing links probes peers.
*/
export const threadNotFound = Effect.fn("OrchestratorMcpService.threadNotFound")(function* (
threads: Pick<ThreadManagement.ThreadManagementService["Service"], "getThreadShell">,
scope: McpInvocationContext.McpInvocationScope,
threadId: ThreadId,
message: string,
) {
const suggested = suggestedThreadId(threadId);
const shell =
suggested === null
? null
: yield* threads.getThreadShell(suggested).pipe(Effect.orElseSucceed(() => null));
const mcpHint =
shell === null || shell.deletedAt !== null ? "" : ` Did you mean \`${suggested}\`?`;
const caller =
scope.thread === undefined
? null
: yield* threads.getThreadShell(scope.thread.threadId).pipe(Effect.orElseSucceed(() => null));
// A peer link's caller would be pointed at this machine's links, and a fenced one may not use them.
const linksHint =
scope.linkedEnvironments === false ||
scope.client?.linked === true ||
fencingLinkOrigin({ scope, caller: caller ?? undefined }) !== undefined
? ""
: " Thread ids belong to one environment: for a thread on a linked one, find it with `t3_environment_links` and pass its `environmentId` to a tool that takes one.";
return yield* new OrchestratorMcpFailure({
code: "thread_not_found",
message: `${message}${mcpHint}${linksHint}`,
});
});
export const unavailable = () =>
new OrchestratorMcpFailure({
code: "orchestration_error",
message: "The operation could not be completed.",
});
export const dispatchFailure = (
error: OrchestratorV2Error | ThreadManagement.ThreadManagementProviderInstanceDisabledError,
) =>
error._tag === "ThreadManagementProviderInstanceDisabledError" ||
error._tag === "OrchestratorRuntimeModeAboveCeilingError" ||
error._tag === "OrchestratorProviderSelectionBusyError"
? new OrchestratorMcpFailure({ code: "invalid_request", message: error.message })
: (error._tag === "OrchestratorDispatchError" ||
error._tag === "OrchestratorCommandRejectedError") &&
typeof error.cause === "string" &&
error.cause.length > 0
? new OrchestratorMcpFailure({
code: "orchestration_error",
message: Array.from(error.cause).slice(0, 1000).join(""),
})
: unavailable();
export interface CallerLimits {
readonly runtimeMode: RuntimeMode;
readonly interactionMode: ProviderInteractionMode;
}
export interface Caller {
readonly scope: McpInvocationContext.McpInvocationScope;
readonly threads: ThreadManagement.ThreadManagementService["Service"];
readonly caller: OrchestrationV2ThreadShell | undefined;
readonly limits: CallerLimits;
}
export const readCaller = Effect.fn("mcp.readCaller")(function* () {
const scope = yield* McpInvocationContext.McpInvocationContext;
if (!scope.capabilities.has("orchestration")) {
return yield* new OrchestratorMcpFailure({
code: "capability_denied",
message: "This credential cannot control threads.",
});
}
return yield* loadCaller();
});
export const loadCaller = Effect.fn("mcp.loadCaller")(function* () {
const scope = yield* McpInvocationContext.McpInvocationContext;
const threads = yield* ThreadManagement.ThreadManagementService;
if (scope.thread === undefined) {
return {
scope,
threads,
caller: undefined,
limits: McpInvocationContext.clientModeCeiling(scope.client),
} satisfies Caller;
}
const caller = yield* threads
.getThreadShell(scope.thread.threadId)
.pipe(Effect.mapError(unavailable));
if (caller === null || caller.deletedAt !== null) {
return yield* new OrchestratorMcpFailure({
code: "thread_not_found",
message: "The calling thread was not found.",
});
}
return {
scope,
threads,
caller,
limits: { runtimeMode: caller.runtimeMode, interactionMode: caller.interactionMode },
} satisfies Caller;
});
export const assertTargetWithinLimits = (
limits: CallerLimits,
target: Pick<OrchestrationV2ThreadShell, "id" | "runtimeMode" | "interactionMode">,
) =>
resolveRuntimeMode(limits.runtimeMode, target.runtimeMode).pipe(
Effect.andThen(resolveInteractionMode(limits.interactionMode, target.interactionMode)),
Effect.asVoid,
Effect.mapError(
(error) =>
new OrchestratorMcpFailure({
code: error.code,
message: `Thread "${target.id}" runs in ${target.runtimeMode}/${target.interactionMode}, above this caller's ${limits.runtimeMode}/${limits.interactionMode} limit. For linked calls, this limit is also capped by the link's approved access. This call does not create an approval request. Have the user act directly in the target thread through an authorized client.`,
}),
),
);
/** Background subagents retain credentials until provider exit; archived threads and other instances cannot act. */
export const isLiveCallerShell = (
shell: Pick<
OrchestrationV2ThreadShell,
"archivedAt" | "activeRunId" | "providerInstanceId" | "pendingBackgroundTasks"
> | null,
providerInstanceId: ProviderInstanceId | undefined,
) =>
shell !== null &&
shell.archivedAt === null &&
shell.providerInstanceId === providerInstanceId &&
(shell.activeRunId !== null ||
(shell.pendingBackgroundTasks?.some(
(task) => task.kind === "subagent" && task.providerInstanceId === providerInstanceId,
) ??
false));
/** Thread callers need a live run or its background subagent in an unarchived thread. OAuth clients rely on their session and ceiling. */
export function assertLiveCaller({ caller, scope }: Caller) {
if (caller === undefined) return Effect.void;
return isLiveCallerShell(caller, scope.thread?.providerInstanceId)
? Effect.void
: Effect.fail(
new OrchestratorMcpFailure({
code: "parent_not_active",
message: "The calling provider no longer owns an active thread run.",
}),
);
}
export const assertFullAccess = (context: Caller, message: string) =>
context.limits.runtimeMode === "full-access" && context.limits.interactionMode === "default"
? Effect.void
: Effect.fail(new OrchestratorMcpFailure({ code: "capability_denied", message }));
export const resolveProjectId = (context: Caller, projectId: ProjectId | undefined) =>
projectId !== undefined
? Effect.succeed(projectId)
: context.caller !== undefined
? Effect.succeed(context.caller.projectId)
: Effect.fail(
new OrchestratorMcpFailure({
code: "target_required",
message: "Pass projectId: this MCP client is not running inside a T3 thread.",
}),
);
const resolveThreadId = (context: Caller, threadId: ThreadId | undefined) =>
threadId !== undefined
? Effect.succeed(threadId)
: context.caller !== undefined
? Effect.succeed(context.caller.id)
: Effect.fail(
new OrchestratorMcpFailure({
code: "target_required",
message: "Pass threadId: this MCP client is not running inside a T3 thread.",
}),
);
export const readThread = Effect.fn("mcp.readThread")(function* <
K extends ProjectionRecordField = never,
>(threadId?: ThreadId, fields: ReadonlyArray<K> = [], filter?: ProjectionRecordFilter) {
const context = yield* readCaller();
const targetId = yield* resolveThreadId(context, threadId);
const shell = yield* context.threads.getThreadShell(targetId).pipe(Effect.mapError(unavailable));
if (shell === null || shell.deletedAt !== null) {
return yield* threadNotFound(
context.threads,
context.scope,
targetId,
"The thread was not found.",
);
}
const projection = yield* context.threads
.getProjectThreadRecords({ projectId: shell.projectId, threadId: targetId }, fields, {
turnItemTypes: ["user_input_request"],
...filter,
})
.pipe(
Effect.mapError((error) =>
error._tag === "ThreadManagementThreadNotFoundError"
? new OrchestratorMcpFailure({
code: "thread_not_found",
message: "The thread was not found.",
})
: unavailable(),
),
);
return { ...context, projection };
});
export const newCommandId = Effect.fn("mcp.newCommandId")(function* () {
const random = yield* SecureRandom.SecureRandom;
return CommandId.make(`mcp:${yield* random.uuidV4()}`);
});