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

clientRpcPermissions.ts

106 lines5.7 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} from "./auth.ts";
15import { ORCHESTRATION_V2_WS_METHODS } from "./orchestrationV2.ts";
16import { WS_METHODS } from "./rpc.ts";
17​
18/** Incremental client enforcement; the server still authorizes every request. */
19export const CLIENT_GUARDED_RPC_SCOPES = {
20 [WS_METHODS.providerAuthRemoveMethod]: AuthProvidersManageScope,
21 [WS_METHODS.previewClose]: AuthPreviewOperateScope,
22 // Stopping a server signals its processes, as closing a terminal does.
23 [WS_METHODS.previewStopLocalServer]: AuthTerminalOperateScope,
24 [WS_METHODS.serverRunStorageCleanup]: AuthSettingsWriteScope,
25 // Restarts the server, as an update does.
26 [WS_METHODS.serverReclaimDatabaseSpace]: AuthEnvironmentMaintainScope,
27 [WS_METHODS.serverCheckOtlpEndpoint]: AuthSettingsWriteScope,
28 // Listing models spends the stored API key, so only those who can choose a model may.
29 [WS_METHODS.serverListTextGeneratorModels]: AuthSettingsWriteScope,
30 [WS_METHODS.serverRefreshProviders]: AuthOrchestrationReadScope,
31 [WS_METHODS.pullRequestsRunAction]: AuthSourceControlWriteScope,
32 [WS_METHODS.pullRequestsUpdate]: AuthSourceControlWriteScope,
33 [WS_METHODS.pullRequestsComment]: AuthSourceControlWriteScope,
34 [WS_METHODS.pullRequestsUpdateComment]: AuthSourceControlWriteScope,
35 [WS_METHODS.pullRequestsSubmitReview]: AuthSourceControlWriteScope,
36 [WS_METHODS.pullRequestsReplyToThread]: AuthSourceControlWriteScope,
37 [WS_METHODS.pullRequestsSetThreadResolution]: AuthSourceControlWriteScope,
38 [WS_METHODS.pullRequestsSetReaction]: AuthSourceControlWriteScope,
39 [WS_METHODS.pullRequestsSetFilesViewed]: AuthSourceControlWriteScope,
40 [WS_METHODS.pullRequestsRequestReviewers]: AuthSourceControlWriteScope,
41 [WS_METHODS.pullRequestsSetLabels]: AuthSourceControlWriteScope,
42 // Writing a guide runs a model on this environment's providers, which reading one does not.
43 [WS_METHODS.pullRequestsGenerateReviewGuide]: AuthOrchestrationOperateScope,
44 [WS_METHODS.projectsCreateFromThread]: AuthOrchestrationOperateScope,
45 [WS_METHODS.sourceControlCloneRepository]: AuthSourceControlWriteScope,
46 [WS_METHODS.sourceControlPublishRepository]: AuthSourceControlWriteScope,
47 [WS_METHODS.sourceControlStar]: AuthSourceControlWriteScope,
48 // Signing in replaces the server's GitHub credential, as saving a token in Settings does.
49 [WS_METHODS.sourceControlSignInStart]: AuthSettingsWriteScope,
50 [WS_METHODS.sourceControlSignInWait]: AuthSettingsWriteScope,
51 [WS_METHODS.sourceControlSignInCancel]: AuthSettingsWriteScope,
52 [WS_METHODS.projectCloneStart]: AuthSourceControlWriteScope,
53 [WS_METHODS.projectCloneCancel]: AuthSourceControlWriteScope,
54 [WS_METHODS.projectCloneRetry]: AuthSourceControlWriteScope,
55 [WS_METHODS.vcsPull]: AuthSourceControlWriteScope,
56 [WS_METHODS.gitRunStackedAction]: AuthSourceControlWriteScope,
57 [WS_METHODS.gitPreparePullRequestThread]: AuthSourceControlWriteScope,
58 [WS_METHODS.vcsCreateWorktree]: AuthSourceControlWriteScope,
59 [WS_METHODS.vcsRemoveWorktree]: AuthSourceControlWriteScope,
60 [WS_METHODS.vcsCreateRef]: AuthSourceControlWriteScope,
61 [WS_METHODS.vcsSwitchRef]: AuthSourceControlWriteScope,
62 [WS_METHODS.vcsInit]: AuthSourceControlWriteScope,
63​
64 // Starts an agent session on this environment that can read its logs and database.
65 [WS_METHODS.serverPrepareTriage]: AuthOrchestrationOperateScope,
66 [WS_METHODS.scheduledTasksUpsert]: AuthOrchestrationOperateScope,
67 [WS_METHODS.scheduledTasksSetEnabled]: AuthOrchestrationOperateScope,
68 [WS_METHODS.scheduledTasksDelete]: AuthOrchestrationOperateScope,
69 [WS_METHODS.scheduledTasksRunNow]: AuthOrchestrationOperateScope,
70 [WS_METHODS.scheduledTasksRotateWebhookToken]: AuthOrchestrationOperateScope,
71 [ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: AuthOrchestrationOperateScope,
72 // Force stop kills agent processes, as Stop on a terminal does.
73 [ORCHESTRATION_V2_WS_METHODS.forceStopThread]: AuthOrchestrationOperateScope,
74 // A guest link hands thread access to someone outside, like a pairing link.
75 [WS_METHODS.guestShareCreate]: AuthAccessWriteScope,
76 [WS_METHODS.peerLinksLink]: AuthAccessWriteScope,
77 [WS_METHODS.peerLinksUnlink]: AuthAccessWriteScope,
78 // A link lets every agent here reach the other environment: Settings' bar.
79 [WS_METHODS.peerLinksAnswerRequest]: AuthAccessWriteScope,
80} as const;
81export type ClientGuardedRpcTag = keyof typeof CLIENT_GUARDED_RPC_SCOPES;
82​
83const decodePrepareThread = Schema.decodeUnknownSync(GitPreparePullRequestThreadInput);
84const decodeStackedAction = Schema.decodeUnknownSync(GitRunStackedActionInput);
85​
86export function clientRpcRequiredScopes(
87 method: string,
88 input: unknown,
89): readonly AuthEnvironmentScope[] {
90 if (method === WS_METHODS.gitPreparePullRequestThread && input !== undefined) {
91 const payload = decodePrepareThread(input);
92 if (payload.mode === "worktree" && payload.threadId !== undefined)
93 return [AuthSourceControlWriteScope, AuthOrchestrationOperateScope];
94 }
95 if (method === WS_METHODS.gitRunStackedAction && input !== undefined) {
96 const payload = decodeStackedAction(input);
97 if (
98 payload.threadId !== undefined &&
99 (payload.action === "create_pr" || payload.action === "commit_push_pr")
100 )
101 return [AuthSourceControlWriteScope, AuthOrchestrationReadScope];
102 }
103 return Object.hasOwn(CLIENT_GUARDED_RPC_SCOPES, method)
104 ? [CLIENT_GUARDED_RPC_SCOPES[method as ClientGuardedRpcTag]]
105 : [];