Skip to content

Commit 704ffa0

Browse files
feat(server): a full-access link is not fenced
A link's agents run as the receiving environment's user, so at full access the fence only ever stopped T3's own tools, never what the agent could do. It also kept a host from driving machines it fully trusts: answering a pending question or steering a thread the user started there, updating a project, or running the project's setup script in a worktree it launched. The stamp now records the access the link was paired with, and only a link below full access is fenced. Its work still carries linkOrigin, so clients keep showing where it came from. Stamps from before access was recorded stay fenced. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 043d589 commit 704ffa0

9 files changed

Lines changed: 164 additions & 101 deletions

File tree

‎apps/server/src/mcp/McpToolAccess.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ const supervisedClient = clientCaller("approval-required");
233233
const fullAccessClient = clientCaller("full-access");
234234
const readOnlyClient = clientCaller("read-only");
235235
const linkedClient: McpInvocationContext.McpInvocationScope = {
236+
...supervisedClient,
237+
client: { ...supervisedClient.client!, linked: true },
238+
};
239+
// A full-access link's agents can do anything this environment's user can.
240+
const fullAccessLinkedClient: McpInvocationContext.McpInvocationScope = {
236241
...fullAccessClient,
237242
client: { ...fullAccessClient.client!, linked: true },
238243
};
@@ -272,6 +277,8 @@ it.effect.each([
272277
["writes_with_capability", ended, {}, "parent_not_active"],
273278
["writes", linkedClient, {}, "capability_denied"],
274279
["writes_with_capability", linkedClient, {}, "capability_denied"],
280+
["writes", fullAccessLinkedClient, {}, "ran"],
281+
["writes_with_capability", fullAccessLinkedClient, {}, "ran"],
275282

276283
["writes_threads", supervised, {}, "ran"],
277284
["writes_threads", supervised, { threadId: planThreadId }, "ran"],

‎apps/server/src/mcp/linkOrigin.test.ts‎

Lines changed: 95 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as DocumentText from "../provider/DocumentText.ts";
12
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
23
import * as NodeServices from "@effect/platform-node/NodeServices";
34
import { assert, it } from "@effect/vitest";
@@ -14,7 +15,6 @@ import {
1415
ThreadId,
1516
} from "@t3tools/contracts";
1617
import * as Effect from "effect/Effect";
17-
import * as Option from "effect/Option";
1818
import * as Layer from "effect/Layer";
1919
import { McpSchema, McpServer } from "effect/ai";
2020

@@ -113,6 +113,7 @@ const layerTools = Layer.mergeAll(
113113
McpHttpServer.layerThreadToolkit,
114114
McpHttpServer.layerProjectRegistration,
115115
).pipe(
116+
Layer.provide(DocumentText.layer.pipe(Layer.provideMerge(NodeServices.layer))),
116117
Layer.provideMerge(McpServer.McpServer.layer),
117118
Layer.provide(layerLaunches),
118119
Layer.provide(NodeCrypto.layer),
@@ -127,7 +128,7 @@ const layerTools = Layer.mergeAll(
127128
Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})),
128129
Layer.provide(
129130
Layer.mock(ProjectService.ProjectService)({
130-
getById: () => Effect.succeed(Option.none()),
131+
getById: () => Effect.succeedNone,
131132
create: (input) =>
132133
Effect.succeed({
133134
id: input.projectId,
@@ -139,7 +140,17 @@ const layerTools = Layer.mergeAll(
139140
updatedAt: "2026-10-18T00:00:00.000Z",
140141
deletedAt: null,
141142
}),
142-
update: () => Effect.die("A linked caller must never reach a project update."),
143+
update: (input) =>
144+
Effect.succeed({
145+
id: input.projectId,
146+
title: input.title ?? "Untitled",
147+
workspaceRoot: "/p/t3code",
148+
defaultModelSelection: null,
149+
scripts: [],
150+
createdAt: "2026-10-18T00:00:00.000Z",
151+
updatedAt: "2026-10-18T00:00:00.000Z",
152+
deletedAt: null,
153+
}),
143154
}),
144155
),
145156
Layer.provide(Layer.mock(SecretRequests.SecretRequests)({})),
@@ -183,17 +194,19 @@ const mcpClient = McpSchema.McpServerClient.of({
183194
const clientScope = (
184195
sessionId: string,
185196
linked: boolean,
197+
access: McpInvocationContext.McpClientCaller["access"],
186198
): McpInvocationContext.McpInvocationScope => ({
187199
environmentId: EnvironmentId.make("environment:box"),
188200
requestNamespace: `client:${sessionId}`,
189201
thread: undefined,
190-
client: { sessionId, label: `T3 Code · ${sessionId}`, access: "full-access", linked },
202+
client: { sessionId, label: `T3 Code · ${sessionId}`, access, linked },
191203
capabilities: new Set(["orchestration", "worktree", "pull-requests"]),
192204
issuedAt: 0,
193205
});
194-
const laptop = clientScope("laptop", true);
195-
const desk = clientScope("desk", true);
196-
const claudeCode = clientScope("claude-code", false);
206+
const laptop = clientScope("laptop", true, "auto");
207+
const desk = clientScope("desk", true, "auto");
208+
const trusted = clientScope("trusted", true, "full-access");
209+
const claudeCode = clientScope("claude-code", false, "full-access");
197210

198211
const call = (
199212
scope: McpInvocationContext.McpInvocationScope,
@@ -235,7 +248,8 @@ const usersOwnThread = Effect.gen(function* () {
235248
projectId,
236249
title: "The user's own",
237250
modelSelection: { instanceId, model: "gpt-5" },
238-
runtimeMode: "full-access",
251+
// Within an `auto` link's modes, so only the fence decides.
252+
runtimeMode: "auto",
239253
interactionMode: "default",
240254
branch: null,
241255
worktreePath: null,
@@ -251,7 +265,11 @@ it.layer(Layer.provideMerge(layerTools, layerOrchestration))("work a link starts
251265
const projections = yield* ProjectionStore.ProjectionStoreV2;
252266
const launched = yield* launch(laptop, "Started from the laptop");
253267
const shell = yield* projections.getThreadShell(launched);
254-
assert.deepEqual(shell?.linkOrigin, { sessionId: "laptop", label: "T3 Code · laptop" });
268+
assert.deepEqual(shell?.linkOrigin, {
269+
sessionId: "laptop",
270+
label: "T3 Code · laptop",
271+
access: "auto",
272+
});
255273

256274
// A subagent of it and a fork of it are built from its thread, origin included.
257275
const thread = (yield* ThreadManagement.ThreadManagementService.pipe(
@@ -294,64 +312,76 @@ it.layer(Layer.provideMerge(layerTools, layerOrchestration))("work a link starts
294312
}),
295313
);
296314

297-
it.effect(
298-
"changes only that link's threads, and adds to the environment without changing it",
299-
() =>
300-
Effect.gen(function* () {
301-
const projections = yield* ProjectionStore.ProjectionStoreV2;
302-
const users = yield* usersOwnThread;
303-
const ownLaunch = yield* launch(laptop, "The laptop's");
304-
const rename = (scope: McpInvocationContext.McpInvocationScope, threadId: ThreadId) =>
305-
call(scope, "t3_thread_update", {
306-
threadId,
307-
action: "rename",
308-
title: `Renamed by ${scope.client!.sessionId}`,
309-
});
315+
it.effect("changes only that link's threads below full access", () =>
316+
Effect.gen(function* () {
317+
const projections = yield* ProjectionStore.ProjectionStoreV2;
318+
const users = yield* usersOwnThread;
319+
const ownLaunch = yield* launch(laptop, "The laptop's");
320+
const rename = (scope: McpInvocationContext.McpInvocationScope, threadId: ThreadId) =>
321+
call(scope, "t3_thread_update", {
322+
threadId,
323+
action: "rename",
324+
title: `Renamed by ${scope.client!.sessionId}`,
325+
});
310326

311-
// The link may change what it started.
312-
assert.equal((yield* rename(laptop, ownLaunch)).isError, false);
313-
assert.equal((yield* projections.getThreadShell(ownLaunch))?.title, "Renamed by laptop");
314-
// Not the user's own thread, nor another link's.
315-
assert.equal(failureCode(yield* rename(laptop, users)), "capability_denied");
316-
assert.equal(failureCode(yield* rename(desk, ownLaunch)), "capability_denied");
317-
assert.equal((yield* projections.getThreadShell(users))?.title, "The user's own");
318-
// An ordinary outside agent is not fenced.
319-
assert.equal((yield* rename(claudeCode, users)).isError, false);
320-
// Naming this environment's own id is still this environment's thread.
321-
for (const [name, args] of [
322-
["t3_thread_send", { message: "From the laptop." }],
323-
["t3_thread_interrupt", {}],
324-
] as const) {
325-
const result = yield* call(laptop, name, {
326-
...args,
327-
environmentId: laptop.environmentId,
328-
threadId: users,
329-
});
330-
assert.equal(failureCode(result), "capability_denied", name);
331-
}
327+
// The link may change what it started.
328+
assert.equal((yield* rename(laptop, ownLaunch)).isError, false);
329+
assert.equal((yield* projections.getThreadShell(ownLaunch))?.title, "Renamed by laptop");
330+
// Not the user's own thread, nor another link's.
331+
assert.equal(failureCode(yield* rename(laptop, users)), "capability_denied");
332+
assert.equal(failureCode(yield* rename(desk, ownLaunch)), "capability_denied");
333+
assert.equal((yield* projections.getThreadShell(users))?.title, "The user's own");
334+
// An ordinary outside agent is not fenced.
335+
assert.equal((yield* rename(claudeCode, users)).isError, false);
336+
// Naming this environment's own id is still this environment's thread.
337+
for (const [name, args] of [
338+
["t3_thread_send", { message: "From the laptop." }],
339+
["t3_thread_interrupt", {}],
340+
] as const) {
341+
const result = yield* call(laptop, name, {
342+
...args,
343+
environmentId: laptop.environmentId,
344+
threadId: users,
345+
});
346+
assert.equal(failureCode(result), "capability_denied", name);
347+
}
332348

333-
// Nor what the environment already has: its projects and settings (one
334-
// declaration covers both), and scheduled tasks.
335-
assert.equal(
336-
failureCode(yield* call(laptop, "t3_project_update", { projectId, title: "Mine now" })),
337-
"capability_denied",
338-
);
339-
// It may bring a repository of its own to work in.
340-
for (const [name, args] of [
341-
["t3_project_clone", { repository: "pingdotgg/t3code", destinationPath: "/p/t3code" }],
342-
["t3_project_create", { title: "t3code", workspaceRoot: "/p/t3code" }],
343-
] as const) {
344-
const added = yield* call(laptop, name, args);
345-
assert.equal(added.isError, false, `${name}: ${JSON.stringify(added.content)}`);
346-
}
347-
assert.equal(
348-
failureCode(yield* call(laptop, "delete_scheduled_task", { scheduledTaskId: "any" })),
349-
"capability_denied",
350-
);
349+
// Nor scheduled tasks, which belong to no thread.
350+
assert.equal(
351+
failureCode(yield* call(laptop, "delete_scheduled_task", { scheduledTaskId: "any" })),
352+
"capability_denied",
353+
);
354+
355+
// Reads are not fenced.
356+
const read = yield* call(laptop, "t3_thread_read", { threadId: users });
357+
assert.equal(read.isError, false);
358+
}),
359+
);
360+
361+
it.effect("is not fenced when the link has full access, but still says where it came from", () =>
362+
Effect.gen(function* () {
363+
const projections = yield* ProjectionStore.ProjectionStoreV2;
364+
const users = yield* usersOwnThread;
365+
const launched = yield* launch(trusted, "Started from the desk");
366+
assert.deepEqual((yield* projections.getThreadShell(launched))?.linkOrigin, {
367+
sessionId: "trusted",
368+
label: "T3 Code · trusted",
369+
access: "full-access",
370+
});
351371

352-
// Reads are not fenced.
353-
const read = yield* call(laptop, "t3_thread_read", { threadId: users });
354-
assert.equal(read.isError, false);
355-
}),
372+
const renamed = yield* call(trusted, "t3_thread_update", {
373+
threadId: users,
374+
action: "rename",
375+
title: "Renamed from the desk",
376+
});
377+
assert.equal(renamed.isError, false, JSON.stringify(renamed.content));
378+
for (const [name, args] of [
379+
["t3_project_update", { projectId, title: "The desk's now" }],
380+
["t3_project_create", { title: "t3code", workspaceRoot: "/p/t3code" }],
381+
] as const) {
382+
const changed = yield* call(trusted, name, args);
383+
assert.equal(changed.isError, false, `${name}: ${JSON.stringify(changed.content)}`);
384+
}
385+
}),
356386
);
357387
});

‎apps/server/src/mcp/linkOrigin.ts‎

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
linkOriginIsFenced,
23
OrchestratorMcpFailure,
34
type OrchestrationV2LinkOrigin,
45
type OrchestrationV2ThreadShell,
@@ -8,15 +9,10 @@ import * as Effect from "effect/Effect";
89
import type { Caller } from "./threadAccess.ts";
910

1011
/**
11-
* Work a linked environment starts here stays within that link. An agent
12-
* reaches this environment through a link as one MCP client session, and
13-
* everything that session starts, and everything that work starts in turn,
14-
* carries the session in `linkOrigin`. Such a caller may add projects, but
15-
* change only work from the same link: not the user's own threads, existing
16-
* projects or settings, nor work from another link. Reads are not fenced.
17-
*
18-
* This is a routing rule for T3's own tools. An agent the link starts still
19-
* runs as this environment's user.
12+
* Below full access, work a link starts here (all of it carries that session in `linkOrigin`)
13+
* may change only work from the same link: not the user's threads, projects or settings, nor
14+
* this environment's links. Reads are not fenced. It is a routing rule for T3's tools; agents
15+
* still run as this environment's user, so a full-access link is not fenced at all.
2016
*/
2117

2218
/** The link a caller acts for: its own session, or the link that started its thread. */
@@ -25,29 +21,39 @@ export const callerLinkOrigin = (
2521
): OrchestrationV2LinkOrigin | undefined =>
2622
caller.caller?.linkOrigin ??
2723
(caller.scope.client?.linked === true
28-
? { sessionId: caller.scope.client.sessionId, label: caller.scope.client.label }
24+
? {
25+
sessionId: caller.scope.client.sessionId,
26+
label: caller.scope.client.label,
27+
access: caller.scope.client.access,
28+
}
2929
: undefined);
3030

31+
/** The link that fences a caller, if any: one paired below full access. */
32+
const fencingLinkOrigin = (caller: Pick<Caller, "scope" | "caller">) => {
33+
const origin = callerLinkOrigin(caller);
34+
return origin !== undefined && linkOriginIsFenced(origin) ? origin : undefined;
35+
};
36+
3137
const outsideLink = (origin: OrchestrationV2LinkOrigin, message: string) =>
3238
new OrchestratorMcpFailure({
3339
code: "capability_denied",
34-
message: `This work was started from ${origin.label} through a linked environment, so ${message}`,
40+
message: `This work was started from ${origin.label} through a linked environment, so ${message} A link paired with full access is not limited this way.`,
3541
});
3642

37-
/** A thread a linked caller may change: one its own link started. */
43+
/** A thread a fenced caller may change: one its own link started. */
3844
export const assertSameLink = (
3945
caller: Pick<Caller, "scope" | "caller">,
4046
target: Pick<OrchestrationV2ThreadShell, "id" | "linkOrigin">,
4147
) => {
42-
const origin = callerLinkOrigin(caller);
48+
const origin = fencingLinkOrigin(caller);
4349
return origin === undefined || target.linkOrigin?.sessionId === origin.sessionId
4450
? Effect.void
4551
: Effect.fail(outsideLink(origin, `it may change only threads that link started.`));
4652
};
4753

48-
/** Linked work never changes what the environment already has: projects, settings, scheduled runs. */
54+
/** Fenced work never changes what the environment already has: projects, settings, scheduled runs. */
4955
export const assertNotLinked = (caller: Pick<Caller, "scope" | "caller">, what: string) => {
50-
const origin = callerLinkOrigin(caller);
56+
const origin = fencingLinkOrigin(caller);
5157
return origin === undefined
5258
? Effect.void
5359
: Effect.fail(outsideLink(origin, `it cannot ${what}.`));

‎apps/server/src/peer/PeerForwarding.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
55
import {
66
EnvironmentId,
77
type OrchestrationV2Run,
8+
type OrchestrationV2ThreadShell,
89
type SourceControlCloneRepositoryInput,
910
ProjectId,
1011
ProviderInstanceId,
@@ -79,7 +80,7 @@ const boxThreads = (seen: Ref.Ref<Seen>, linkSession: Ref.Ref<string>) => {
7980
const users = { ...liveThreadShell(usersThread, { runtimeMode: "auto" }), projectId: boxProject };
8081
const run = (status: OrchestrationV2Run["status"]) =>
8182
({ id: boxRun, status }) as unknown as OrchestrationV2Run;
82-
const shellOf = (threadId: ThreadId) =>
83+
const shellOf = (threadId: ThreadId): Effect.Effect<OrchestrationV2ThreadShell | null> =>
8384
threadId === boxThread ? linked : Effect.succeed(threadId === usersThread ? users : null);
8485
const projectionOf = (threadId: ThreadId) =>
8586
shellOf(threadId).pipe(

‎apps/server/src/peer/PeerForwarding.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,10 @@ const make = Effect.gen(function* () {
7575
const threads = yield* ThreadManagement.ThreadManagementService;
7676

7777
/**
78-
* The caller's own limits here, which travel with every call it forwards.
79-
* The peer sees only the link's credential, so a credential that may not
80-
* control threads here is refused here. Work a link started here never uses
81-
* this environment's own links: the peer would see this environment's
82-
* session, not the link the work came from, so its fence could not hold.
78+
* The caller's own limits, which travel with every forwarded call: the peer sees only the
79+
* link's credential, so a caller that may not control threads here is refused here. Work a
80+
* link started never uses this environment's links, since the peer would see this
81+
* environment's session instead of that link, and its fence could not hold.
8382
*/
8483
const callerLimits = (scope: Scope) =>
8584
readCaller().pipe(

‎apps/server/src/project/ProjectSetupScriptRunner.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { TestClock } from "effect/testing";
1111

1212
import * as ServerSettings from "../serverSettings.ts";
1313
import * as TerminalManager from "../terminal/Manager.ts";
14+
import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts";
1415
import * as ProjectService from "./ProjectService.ts";
1516
import * as ProjectSetupScriptRunner from "./ProjectSetupScriptRunner.ts";
1617

@@ -58,6 +59,9 @@ const makeHarness = Effect.fn(function* (options: {
5859
Layer.mergeAll(
5960
terminals,
6061
Layer.mock(ProjectService.ProjectService)({}),
62+
Layer.mock(ProjectionStore.ProjectionStoreV2)({
63+
getThreadShell: () => Effect.succeed(null),
64+
}),
6165
ServerSettings.layerTest(),
6266
NodeCrypto.layer,
6367
),

‎apps/server/src/project/ProjectSetupScriptRunner.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ProjectId, type ProjectScript, ThreadId } from "@t3tools/contracts";
1+
import { linkOriginIsFenced, ProjectId, type ProjectScript, ThreadId } from "@t3tools/contracts";
22
import * as HostProcess from "@t3tools/shared/HostProcess";
33
import {
44
projectScriptRuntimeEnv,
@@ -48,8 +48,8 @@ export interface ProjectSetupScriptOutputLine {
4848
}
4949

5050
/**
51-
* A thread a linked environment started does not run this project's setup
52-
* script: the script is the user's own code, and the link only grants T3's
51+
* A thread a link below full access started does not run this project's setup
52+
* script: the script is the user's own code, and such a link only grants T3's
5353
* tools (see `mcp/linkOrigin.ts`).
5454
*/
5555
export interface ProjectSetupScriptRunnerResultSkippedForLink {
@@ -368,7 +368,8 @@ export const make = Effect.gen(function* () {
368368
}),
369369
),
370370
);
371-
if (thread?.linkOrigin !== undefined) return { status: "skipped-for-link" } as const;
371+
if (thread?.linkOrigin !== undefined && linkOriginIsFenced(thread.linkOrigin))
372+
return { status: "skipped-for-link" } as const;
372373
const suppliedProject = input.project;
373374
const projectById =
374375
suppliedProject ??

0 commit comments

Comments
 (0)