Skip to content

Commit c27d43a

Browse files
t3dotggclaude
andcommitted
fix(server): peer work can only change PR links on its own grant's threads
PR tools gained explicit thread targets on main, so a peer-origin caller could link, unlink, or watch pull requests on trusted threads. A watch also delivers comments into the target thread. Apply the same-grant rule there too. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent ee70595 commit c27d43a

4 files changed

Lines changed: 85 additions & 21 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ interface WithPeerOrigin {
1111
* Grants differ in modes and setup-script permission, so one grant's work must
1212
* not steer another's. Other callers may act on anything. Reads are not gated.
1313
*/
14-
export function assertPeerWorkAccess(caller: WithPeerOrigin | undefined, target: WithPeerOrigin) {
14+
export function assertPeerWorkAccess(
15+
caller: WithPeerOrigin | undefined,
16+
target: WithPeerOrigin,
17+
): Effect.Effect<void, OrchestratorMcpFailure> {
1518
const origin = caller?.peerOrigin;
1619
return origin != null && target.peerOrigin?.grantId !== origin.grantId
1720
? Effect.fail(
@@ -27,7 +30,10 @@ export function assertPeerWorkAccess(caller: WithPeerOrigin | undefined, target:
2730
* Refuses an action that peer-origin work may never take, such as writing
2831
* persistent project or environment configuration or reaching another environment.
2932
*/
30-
export function rejectPeerOriginCaller(caller: WithPeerOrigin | undefined, action: string) {
33+
export function rejectPeerOriginCaller(
34+
caller: WithPeerOrigin | undefined,
35+
action: string,
36+
): Effect.Effect<void, OrchestratorMcpFailure> {
3137
return caller?.peerOrigin != null
3238
? Effect.fail(
3339
new OrchestratorMcpFailure({

‎apps/server/src/mcp/toolkits/pullRequests/handlers.test.ts‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import {
22
EnvironmentId,
3+
PeerGrantId,
34
ProjectId,
45
ProviderInstanceId,
6+
RunId,
57
ThreadId,
68
type OrchestrationV2ServerCommand as OrchestrationCommand,
79
type OrchestrationProjectShell,
10+
type OrchestrationV2ThreadShell,
811
type ThreadPullRequestLink,
912
} from "@t3tools/contracts";
1013
import { describe, expect, it } from "@effect/vitest";
@@ -132,6 +135,8 @@ interface HarnessOptions {
132135
readonly project?: OrchestrationProjectShell | null;
133136
/** A rejection the orchestrator reports as the dispatch error's cause. */
134137
readonly reject?: (command: OrchestrationCommand) => string | null;
138+
/** Every thread the orchestrator knows, replacing the default single thread. */
139+
readonly shells?: ReadonlyArray<OrchestrationV2ThreadShell>;
135140
}
136141

137142
const makeHarness = Effect.fn("makePullRequestsToolkitHarness")(function* (
@@ -158,7 +163,13 @@ const makeHarness = Effect.fn("makePullRequestsToolkitHarness")(function* (
158163
}),
159164
Layer.mock(Orchestrator.OrchestratorV2)({
160165
getThreadShell: (id) =>
161-
Effect.succeed(id === THREAD_ID && thread ? v2PullRequestThread(thread) : null),
166+
Effect.succeed(
167+
options.shells !== undefined
168+
? (options.shells.find((shell) => shell.id === id) ?? null)
169+
: id === THREAD_ID && thread
170+
? v2PullRequestThread(thread)
171+
: null,
172+
),
162173
dispatch,
163174
}),
164175
Layer.succeed(Crypto.Crypto, testCrypto),
@@ -425,6 +436,46 @@ describe("pull request toolkit handlers", () => {
425436
expect(result.pullRequests[0]?.host).toBe("forge.example:3000");
426437
});
427438

439+
it.effect("lets work a peer started change only its own grant's threads", () =>
440+
Effect.gen(function* () {
441+
const peerOrigin = {
442+
grantId: PeerGrantId.make("grant:mainbook"),
443+
label: "Mainbook",
444+
claimedEnvironmentId: null,
445+
};
446+
const shell = (id: string, origin: typeof peerOrigin | null) => ({
447+
...v2PullRequestThread({ ...makeThread([]), id: ThreadId.make(id) }),
448+
peerOrigin: origin,
449+
});
450+
const harness = yield* makeHarness({
451+
shells: [
452+
// The calling thread is peer work with a live run.
453+
{ ...shell(THREAD_ID, peerOrigin), activeRunId: RunId.make("run-1") },
454+
shell("thread-trusted", null),
455+
shell("thread-other-grant", { ...peerOrigin, grantId: PeerGrantId.make("grant:x") }),
456+
shell("thread-same-grant", peerOrigin),
457+
],
458+
});
459+
const watch = (threadId: string) =>
460+
harness.call("watch_pull_request", {
461+
url: "https://github.com/t3tools/t3code/pull/9",
462+
threadId: ThreadId.make(threadId),
463+
});
464+
for (const target of ["thread-trusted", "thread-other-grant"]) {
465+
const error = yield* watch(target).pipe(Effect.flip);
466+
expect(error).toMatchObject({
467+
_tag: "PullRequestThreadAboveLimitsError",
468+
threadId: target,
469+
});
470+
}
471+
expect(yield* Ref.get(harness.commands)).toEqual([]);
472+
yield* watch("thread-same-grant");
473+
expect(yield* Ref.get(harness.commands)).toMatchObject([
474+
{ type: "thread.pull-request.watch", threadId: "thread-same-grant" },
475+
]);
476+
}),
477+
);
478+
428479
it.effect("fails cleanly when the token's thread no longer exists", () =>
429480
Effect.gen(function* () {
430481
const harness = yield* makeHarness({ thread: null });

‎apps/server/src/mcp/toolkits/pullRequests/handlers.ts‎

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import * as Option from "effect/Option";
2626
import * as Orchestrator from "../../../orchestration-v2/Orchestrator.ts";
2727
import * as ProjectService from "../../../project/ProjectService.ts";
2828
import * as McpInvocationContext from "../../McpInvocationContext.ts";
29+
import { assertPeerWorkAccess } from "../../peerOrigin.ts";
2930
import { assertTargetWithinLimits } from "../../threadAccess.ts";
3031
import {
3132
type ListThreadPullRequestsResult,
@@ -190,7 +191,8 @@ const make = Effect.gen(function* () {
190191

191192
/**
192193
* A thread whose pull requests the caller may change: its own, or one that
193-
* runs within the caller's modes.
194+
* runs within the caller's modes and, for work a peer started, belongs to
195+
* the same grant. A watch sends comments into the thread, so it is a write.
194196
*/
195197
const requireWritableThread = Effect.fn("PullRequestsToolkit.requireWritableThread")(function* (
196198
Failure:
@@ -202,27 +204,32 @@ const make = Effect.gen(function* () {
202204
const thread = yield* requireThread(Failure, requested);
203205
const scope = yield* McpInvocationContext.McpInvocationContext;
204206
if (thread.id === scope.thread?.threadId) return thread;
205-
const limits =
207+
const caller =
206208
scope.thread === undefined
209+
? undefined
210+
: yield* engine
211+
.getThreadShell(scope.thread.threadId)
212+
.pipe(Effect.mapError((cause) => new Failure({ cause })));
213+
// A thread caller changes other threads only while its own run is live.
214+
if (
215+
scope.thread !== undefined &&
216+
(caller == null ||
217+
caller.archivedAt !== null ||
218+
caller.activeRunId === null ||
219+
caller.providerInstanceId !== scope.thread.providerInstanceId)
220+
) {
221+
return yield* new PullRequestThreadAboveLimitsError({ threadId: thread.id });
222+
}
223+
const limits =
224+
caller == null
207225
? {
208226
runtimeMode: scope.client?.runtimeModeCeiling ?? ("approval-required" as const),
209227
interactionMode: "default" as const,
210228
}
211-
: yield* engine.getThreadShell(scope.thread.threadId).pipe(
212-
Effect.mapError((cause) => new Failure({ cause })),
213-
Effect.map((caller) =>
214-
// A thread caller changes other threads only while its own run is live.
215-
caller === null ||
216-
caller.archivedAt !== null ||
217-
caller.activeRunId === null ||
218-
caller.providerInstanceId !== scope.thread?.providerInstanceId
219-
? undefined
220-
: { runtimeMode: caller.runtimeMode, interactionMode: caller.interactionMode },
221-
),
222-
);
223-
if (limits === undefined) {
224-
return yield* new PullRequestThreadAboveLimitsError({ threadId: thread.id });
225-
}
229+
: { runtimeMode: caller.runtimeMode, interactionMode: caller.interactionMode };
230+
yield* assertPeerWorkAccess(caller ?? undefined, thread).pipe(
231+
Effect.mapError(() => new PullRequestThreadAboveLimitsError({ threadId: thread.id })),
232+
);
226233
yield* assertTargetWithinLimits(limits, thread).pipe(
227234
Effect.mapError(() => new PullRequestThreadAboveLimitsError({ threadId: thread.id })),
228235
);

‎apps/server/src/mcp/toolkits/pullRequests/tools.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class PullRequestThreadAboveLimitsError extends Schema.TaggedError<PullRe
101101
{ threadId: Schema.String },
102102
) {
103103
override get message(): string {
104-
return `Thread ${this.threadId} cannot be changed from here: it runs with broader permissions than this caller, or the calling thread has no active run.`;
104+
return `Thread ${this.threadId} cannot be changed from here: it runs with broader permissions than this caller, it is work this caller's peer grant does not cover, or the calling thread has no active run.`;
105105
}
106106
}
107107

0 commit comments

Comments
 (0)