Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

clientRpcPermissions.ts

143 lines8.0 KB

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.sourceControlCloneRepository]: AuthSourceControlWriteScope,
60 [WS_METHODS.sourceControlPublishRepository]: AuthSourceControlWriteScope,
61 [WS_METHODS.sourceControlStar]: AuthSourceControlWriteScope,
62 // Signing in replaces the server's GitHub credential, as saving a token in Settings does.
63 [WS_METHODS.sourceControlSignInStart]: AuthSettingsWriteScope,
64 [WS_METHODS.sourceControlSignInWait]: AuthSettingsWriteScope,
65 [WS_METHODS.sourceControlSignInCancel]: AuthSettingsWriteScope,
66 [WS_METHODS.projectCloneStart]: AuthSourceControlWriteScope,
67 [WS_METHODS.projectCloneCancel]: AuthSourceControlWriteScope,
68 [WS_METHODS.projectCloneRetry]: AuthSourceControlWriteScope,
69 [WS_METHODS.vcsPull]: AuthSourceControlWriteScope,
70 [WS_METHODS.gitRunStackedAction]: AuthSourceControlWriteScope,
71 [WS_METHODS.gitPreparePullRequestThread]: AuthSourceControlWriteScope,
72 [WS_METHODS.vcsCreateWorktree]: AuthSourceControlWriteScope,
73 [WS_METHODS.vcsRemoveWorktree]: AuthSourceControlWriteScope,
74 [WS_METHODS.workspaceAddTaskFolderRepository]: AuthSourceControlWriteScope,
75 // The server runs an approved layout command: approving one is running code there, as a terminal does.
76 [WS_METHODS.workspaceApproveLayoutCommands]: AuthTerminalOperateScope,
77 [WS_METHODS.vcsCreateRef]: AuthSourceControlWriteScope,
78 [WS_METHODS.vcsSwitchRef]: AuthSourceControlWriteScope,
79 [WS_METHODS.vcsInit]: AuthSourceControlWriteScope,
80​
81 // Starts an agent session on this environment that can read its logs and database.
82 [WS_METHODS.serverPrepareTriage]: AuthOrchestrationOperateScope,
83 [WS_METHODS.scheduledTasksUpsert]: AuthOrchestrationOperateScope,
84 [WS_METHODS.scheduledTasksSetEnabled]: AuthOrchestrationOperateScope,
85 [WS_METHODS.scheduledTasksDelete]: AuthOrchestrationOperateScope,
86 [WS_METHODS.scheduledTasksRunNow]: AuthOrchestrationOperateScope,
87 [WS_METHODS.scheduledTasksRotateWebhookToken]: AuthOrchestrationOperateScope,
88 [ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: AuthOrchestrationOperateScope,
89 // An answer, a resume and a cancel each decide what a run's agents do next.
90 [WS_METHODS.workflowsAnswer]: AuthOrchestrationOperateScope,
91 [WS_METHODS.workflowsResume]: AuthOrchestrationOperateScope,
92 [WS_METHODS.workflowsCancel]: AuthOrchestrationOperateScope,
93 // The server runs this file: saving it is running code there, as a terminal does.
94 [WS_METHODS.workflowsSaveDefinition]: AuthTerminalOperateScope,
95 [WS_METHODS.workflowsDeleteDefinition]: AuthTerminalOperateScope,
96 // Force stop kills agent processes, as Stop on a terminal does.
97 [ORCHESTRATION_V2_WS_METHODS.forceStopThread]: AuthOrchestrationOperateScope,
98 // A guest link hands thread access to someone outside, like a pairing link.
99 [WS_METHODS.guestShareCreate]: AuthAccessWriteScope,
100 [WS_METHODS.peerLinksLink]: AuthAccessWriteScope,
101 [WS_METHODS.peerLinksUnlink]: AuthAccessWriteScope,
102 // A link lets every agent here reach the other environment: Settings' bar.
103 [WS_METHODS.peerLinksAnswerRequest]: AuthAccessWriteScope,
104 [WS_METHODS.peerLinksPairClient]: AuthAccessWriteScope,
105 [WS_METHODS.peerLinksPairLink]: AuthAccessWriteScope,
106 // Send stores and sends a report from a thread's turn: the bar of running that thread.
107 [WS_METHODS.agentFeedbackAnswerRequest]: AuthOrchestrationOperateScope,
108} as const;
109export type ClientGuardedRpcTag = keyof typeof CLIENT_GUARDED_RPC_SCOPES;
110​
111const decodePrepareThread = Schema.decodeUnknownSync(GitPreparePullRequestThreadInput);
112const decodeStackedAction = Schema.decodeUnknownSync(GitRunStackedActionInput);
113const decodeLinkPairing = Schema.decodeUnknownSync(PeerLinkLinkPairingInput);
114​
115export function clientRpcRequiredScopes(
116 method: string,
117 input: unknown,
118): readonly AuthEnvironmentScope[] {
119 if (method === WS_METHODS.gitPreparePullRequestThread && input !== undefined) {
120 const payload = decodePrepareThread(input);
121 if (payload.mode === "worktree" && payload.threadId !== undefined)
122 return [AuthSourceControlWriteScope, AuthOrchestrationOperateScope];
123 }
124 if (method === WS_METHODS.gitRunStackedAction && input !== undefined) {
125 const payload = decodeStackedAction(input);
126 if (
127 payload.threadId !== undefined &&
128 (payload.action === "create_pr" || payload.action === "commit_push_pr")
129 )
130 return [AuthSourceControlWriteScope, AuthOrchestrationReadScope];
131 }
132 // Handing out access takes holding it: the caller gets back a credential it can spend itself.
133 if (method === WS_METHODS.peerLinksPairLink && input !== undefined) {
134 const held = mcpClientScopes(decodeLinkPairing(input).access);
135 return [AuthAccessWriteScope, ...held];
136 }
137 if (method === WS_METHODS.guestShareCreate) {
138 return [AuthAccessWriteScope, AuthOrchestrationReadScope];
139 }
140 return Object.hasOwn(CLIENT_GUARDED_RPC_SCOPES, method)
141 ? [CLIENT_GUARDED_RPC_SCOPES[method as ClientGuardedRpcTag]]
142 : [];