Open History to see the last commit
1import * as Schema from "effect/Schema";
2import { GitPreparePullRequestThreadInput, GitRunStackedActionInput } from "./git.ts";
3import {
4 AuthAccessWriteScope,
5 AuthEnvironmentMaintainScope,
6 AuthOrchestrationOperateScope,
7 AuthOrchestrationReadScope,
8 AuthPreviewOperateScope,
9 AuthProvidersManageScope,
10 AuthSettingsWriteScope,
11 AuthSourceControlWriteScope,
12 AuthTerminalOperateScope,
13 type AuthEnvironmentScope,
14 mcpClientScopes,
15} from "./auth.ts";
16import { PeerLinkLinkPairingInput } from "./peerLink.ts";
17import { ORCHESTRATION_V2_WS_METHODS } from "./orchestrationV2.ts";
18import { WS_METHODS } from "./rpc.ts";
19
20/** Incremental client enforcement; the server still authorizes every request. */
21export const CLIENT_GUARDED_RPC_SCOPES = {
22 [WS_METHODS.providerAuthRemoveMethod]: AuthProvidersManageScope,
23 // An MCP sign-in stores a credential for the agent, as an account sign-in does.
24 [WS_METHODS.providerMcpSignInStart]: AuthProvidersManageScope,
25 [WS_METHODS.providerMcpSignInComplete]: AuthProvidersManageScope,
26 [WS_METHODS.previewClose]: AuthPreviewOperateScope,
27 // Stopping a server signals its processes, as closing a terminal does.
28 [WS_METHODS.previewStopLocalServer]: AuthTerminalOperateScope,
29 [WS_METHODS.serverRunStorageCleanup]: AuthSettingsWriteScope,
30 // Restarts the server, as an update does.
31 [WS_METHODS.serverReclaimDatabaseSpace]: AuthEnvironmentMaintainScope,
32 [WS_METHODS.serverCheckOtlpEndpoint]: AuthSettingsWriteScope,
33 [WS_METHODS.serverCheckSecretSource]: AuthSettingsWriteScope,
34 // Listing models spends the stored API key, so only those who can choose a model may.
35 [WS_METHODS.serverListTextGeneratorModels]: AuthSettingsWriteScope,
36 [WS_METHODS.serverRefreshProviders]: AuthOrchestrationReadScope,
37 [WS_METHODS.pullRequestsRunAction]: AuthSourceControlWriteScope,
38 [WS_METHODS.pullRequestsUpdate]: AuthSourceControlWriteScope,
39 [WS_METHODS.pullRequestsComment]: AuthSourceControlWriteScope,
40 [WS_METHODS.pullRequestsUpdateComment]: AuthSourceControlWriteScope,
41 [WS_METHODS.pullRequestsSubmitReview]: AuthSourceControlWriteScope,
42 [WS_METHODS.pullRequestsReplyToThread]: AuthSourceControlWriteScope,
43 [WS_METHODS.pullRequestsSetThreadResolution]: AuthSourceControlWriteScope,
44 [WS_METHODS.pullRequestsSetReaction]: AuthSourceControlWriteScope,
45 [WS_METHODS.pullRequestsSetFilesViewed]: AuthSourceControlWriteScope,
46 [WS_METHODS.pullRequestsRequestReviewers]: AuthSourceControlWriteScope,
47 [WS_METHODS.pullRequestsSetLabels]: AuthSourceControlWriteScope,
48 [WS_METHODS.issuesCreate]: AuthSourceControlWriteScope,
49 [WS_METHODS.issuesComment]: AuthSourceControlWriteScope,
50 [WS_METHODS.issuesUpdate]: AuthSourceControlWriteScope,
51 [WS_METHODS.issuesSetState]: AuthSourceControlWriteScope,
52 [WS_METHODS.issuesReact]: AuthSourceControlWriteScope,
53 // A tracker credential and a project's binding are settings, saved under the same grant.
54 [WS_METHODS.issueTrackersConnect]: AuthSettingsWriteScope,
55 [WS_METHODS.issueTrackersBindProject]: AuthSettingsWriteScope,
56 // Writing a guide runs a model on this environment's providers, which reading one does not.
57 [WS_METHODS.pullRequestsGenerateReviewGuide]: AuthOrchestrationOperateScope,
58 [WS_METHODS.projectsCreateFromThread]: AuthOrchestrationOperateScope,
59 [WS_METHODS.threadsMoveWorkspace]: AuthOrchestrationOperateScope,
60 [WS_METHODS.sourceControlCloneRepository]: AuthSourceControlWriteScope,
61 [WS_METHODS.sourceControlPublishRepository]: AuthSourceControlWriteScope,
62 [WS_METHODS.sourceControlStar]: AuthSourceControlWriteScope,
63 // Signing in replaces the server's GitHub credential, as saving a token in Settings does.
64 [WS_METHODS.sourceControlSignInStart]: AuthSettingsWriteScope,
65 [WS_METHODS.sourceControlSignInWait]: AuthSettingsWriteScope,
66 [WS_METHODS.sourceControlSignInCancel]: AuthSettingsWriteScope,
67 [WS_METHODS.projectCloneStart]: AuthSourceControlWriteScope,
68 [WS_METHODS.projectCloneCancel]: AuthSourceControlWriteScope,
69 [WS_METHODS.projectCloneRetry]: AuthSourceControlWriteScope,
70 [WS_METHODS.vcsPull]: AuthSourceControlWriteScope,
71 [WS_METHODS.gitRunStackedAction]: AuthSourceControlWriteScope,
72 [WS_METHODS.gitPreparePullRequestThread]: AuthSourceControlWriteScope,
73 [WS_METHODS.vcsCreateWorktree]: AuthSourceControlWriteScope,
74 [WS_METHODS.vcsRemoveWorktree]: AuthSourceControlWriteScope,
75 [WS_METHODS.workspaceAddTaskFolderRepository]: AuthSourceControlWriteScope,
76 // The server runs an approved layout command: approving one is running code there, as a terminal does.
77 [WS_METHODS.workspaceApproveLayoutCommands]: AuthTerminalOperateScope,
78 [WS_METHODS.vcsCreateRef]: AuthSourceControlWriteScope,
79 [WS_METHODS.vcsSwitchRef]: AuthSourceControlWriteScope,
80 [WS_METHODS.vcsInit]: AuthSourceControlWriteScope,
81
82 // Starts an agent session on this environment that can read its logs and database.
83 [WS_METHODS.serverPrepareTriage]: AuthOrchestrationOperateScope,
84 [WS_METHODS.scheduledTasksUpsert]: AuthOrchestrationOperateScope,
85 [WS_METHODS.scheduledTasksSetEnabled]: AuthOrchestrationOperateScope,
86 [WS_METHODS.scheduledTasksDelete]: AuthOrchestrationOperateScope,
87 [WS_METHODS.scheduledTasksRunNow]: AuthOrchestrationOperateScope,
88 [WS_METHODS.scheduledTasksRotateWebhookToken]: AuthOrchestrationOperateScope,
89 [ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: AuthOrchestrationOperateScope,
90 // An answer, a resume and a cancel each decide what a run's agents do next.
91 [WS_METHODS.workflowsAnswer]: AuthOrchestrationOperateScope,
92 [WS_METHODS.workflowsResume]: AuthOrchestrationOperateScope,
93 [WS_METHODS.workflowsCancel]: AuthOrchestrationOperateScope,
94 // The server runs this file: saving it is running code there, as a terminal does.
95 [WS_METHODS.workflowsSaveDefinition]: AuthTerminalOperateScope,
96 [WS_METHODS.workflowsDeleteDefinition]: AuthTerminalOperateScope,
97 // Force stop kills agent processes, as Stop on a terminal does.
98 [ORCHESTRATION_V2_WS_METHODS.forceStopThread]: AuthOrchestrationOperateScope,
99 // A guest link hands thread access to someone outside, like a pairing link.
100 [WS_METHODS.guestShareCreate]: AuthAccessWriteScope,
101 [WS_METHODS.peerLinksLink]: AuthAccessWriteScope,
102 [WS_METHODS.peerLinksUnlink]: AuthAccessWriteScope,
103 // A link lets every agent here reach the other environment: Settings' bar.
104 [WS_METHODS.peerLinksAnswerRequest]: AuthAccessWriteScope,
105 [WS_METHODS.peerLinksPairClient]: AuthAccessWriteScope,
106 [WS_METHODS.peerLinksPairLink]: AuthAccessWriteScope,
107 // Moving a thread ends its work here and starts it on the linked environment.
108 [WS_METHODS.threadHandoffStart]: AuthOrchestrationOperateScope,
109 [WS_METHODS.threadHandoffCancel]: AuthOrchestrationOperateScope,
110 [WS_METHODS.threadHandoffAdopt]: AuthOrchestrationOperateScope,
111 // Send stores and sends a report from a thread's turn: the bar of running that thread.
112 [WS_METHODS.agentFeedbackAnswerRequest]: AuthOrchestrationOperateScope,
113} as const;
114export type ClientGuardedRpcTag = keyof typeof CLIENT_GUARDED_RPC_SCOPES;
115
116const decodePrepareThread = Schema.decodeUnknownSync(GitPreparePullRequestThreadInput);
117const decodeStackedAction = Schema.decodeUnknownSync(GitRunStackedActionInput);
118const decodeLinkPairing = Schema.decodeUnknownSync(PeerLinkLinkPairingInput);
119
120export function clientRpcRequiredScopes(
121 method: string,
122 input: unknown,
123): readonly AuthEnvironmentScope[] {
124 if (method === WS_METHODS.gitPreparePullRequestThread && input !== undefined) {
125 const payload = decodePrepareThread(input);
126 if (payload.mode === "worktree" && payload.threadId !== undefined)
127 return [AuthSourceControlWriteScope, AuthOrchestrationOperateScope];
128 }
129 if (method === WS_METHODS.gitRunStackedAction && input !== undefined) {
130 const payload = decodeStackedAction(input);
131 if (
132 payload.threadId !== undefined &&
133 (payload.action === "create_pr" || payload.action === "commit_push_pr")
134 )
135 return [AuthSourceControlWriteScope, AuthOrchestrationReadScope];
136 }
137 // Handing out access takes holding it: the caller gets back a credential it can spend itself.
138 if (method === WS_METHODS.peerLinksPairLink && input !== undefined) {
139 const held = mcpClientScopes(decodeLinkPairing(input).access);
140 return [AuthAccessWriteScope, ...held];
141 }
142 if (method === WS_METHODS.guestShareCreate) {
143 return [AuthAccessWriteScope, AuthOrchestrationReadScope];
144 }
145 // Checking a secret source resolves a caller-named reference and applies it to providers.
146 if (method === WS_METHODS.serverCheckSecretSource) {
147 return [AuthSettingsWriteScope, AuthProvidersManageScope];
148 }
149 return Object.hasOwn(CLIENT_GUARDED_RPC_SCOPES, method)
150 ? [CLIENT_GUARDED_RPC_SCOPES[method as ClientGuardedRpcTag]]
151 : [];
152}