Open History to see the last commit
1import * as Schema from "effect/Schema";
2import { GitPreparePullRequestThreadInput } from "./git.ts";
3import {
4 AuthOrchestrationOperateScope,
5 AuthPreviewOperateScope,
6 AuthSettingsWriteScope,
7 AuthSourceControlWriteScope,
8 type AuthEnvironmentScope,
9} from "./auth.ts";
10import { ORCHESTRATION_V2_WS_METHODS } from "./orchestrationV2.ts";
11import { WS_METHODS } from "./rpc.ts";
12
13/** Incremental client enforcement; the server still authorizes every request. */
14export const CLIENT_GUARDED_RPC_SCOPES = {
15 [WS_METHODS.previewClose]: AuthPreviewOperateScope,
16 [WS_METHODS.serverRunStorageCleanup]: AuthSettingsWriteScope,
17 [WS_METHODS.serverCheckOtlpEndpoint]: AuthSettingsWriteScope,
18 [WS_METHODS.pullRequestsRunAction]: AuthSourceControlWriteScope,
19 [WS_METHODS.pullRequestsUpdate]: AuthSourceControlWriteScope,
20 [WS_METHODS.pullRequestsComment]: AuthSourceControlWriteScope,
21 [WS_METHODS.pullRequestsUpdateComment]: AuthSourceControlWriteScope,
22 [WS_METHODS.pullRequestsSubmitReview]: AuthSourceControlWriteScope,
23 [WS_METHODS.pullRequestsReplyToThread]: AuthSourceControlWriteScope,
24 [WS_METHODS.pullRequestsSetThreadResolution]: AuthSourceControlWriteScope,
25 [WS_METHODS.pullRequestsSetReaction]: AuthSourceControlWriteScope,
26 [WS_METHODS.pullRequestsSetFilesViewed]: AuthSourceControlWriteScope,
27 [WS_METHODS.pullRequestsRequestReviewers]: AuthSourceControlWriteScope,
28 [WS_METHODS.pullRequestsSetLabels]: AuthSourceControlWriteScope,
29 // Writing a guide runs a model on this environment's providers, which reading one does not.
30 [WS_METHODS.pullRequestsGenerateReviewGuide]: AuthOrchestrationOperateScope,
31 [WS_METHODS.sourceControlCloneRepository]: AuthSourceControlWriteScope,
32 [WS_METHODS.sourceControlPublishRepository]: AuthSourceControlWriteScope,
33 [WS_METHODS.projectCloneStart]: AuthSourceControlWriteScope,
34 [WS_METHODS.projectCloneCancel]: AuthSourceControlWriteScope,
35 [WS_METHODS.projectCloneRetry]: AuthSourceControlWriteScope,
36 [WS_METHODS.vcsPull]: AuthSourceControlWriteScope,
37 [WS_METHODS.gitRunStackedAction]: AuthSourceControlWriteScope,
38 [WS_METHODS.gitPreparePullRequestThread]: AuthSourceControlWriteScope,
39 [WS_METHODS.vcsCreateWorktree]: AuthSourceControlWriteScope,
40 [WS_METHODS.vcsRemoveWorktree]: AuthSourceControlWriteScope,
41 [WS_METHODS.vcsCreateRef]: AuthSourceControlWriteScope,
42 [WS_METHODS.vcsSwitchRef]: AuthSourceControlWriteScope,
43 [WS_METHODS.vcsInit]: AuthSourceControlWriteScope,
44
45 [WS_METHODS.scheduledTasksUpsert]: AuthOrchestrationOperateScope,
46 [WS_METHODS.scheduledTasksSetEnabled]: AuthOrchestrationOperateScope,
47 [WS_METHODS.scheduledTasksDelete]: AuthOrchestrationOperateScope,
48 [WS_METHODS.scheduledTasksRunNow]: AuthOrchestrationOperateScope,
49 [WS_METHODS.scheduledTasksRotateWebhookToken]: AuthOrchestrationOperateScope,
50 [ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: AuthOrchestrationOperateScope,
51} as const;
52export type ClientGuardedRpcTag = keyof typeof CLIENT_GUARDED_RPC_SCOPES;
53
54const decodePrepareThread = Schema.decodeUnknownSync(GitPreparePullRequestThreadInput);
55
56export function clientRpcRequiredScopes(
57 method: string,
58 input: unknown,
59): readonly AuthEnvironmentScope[] {
60 if (method === WS_METHODS.gitPreparePullRequestThread && input !== undefined) {
61 const payload = decodePrepareThread(input);
62 if (payload.mode === "worktree" && payload.threadId !== undefined)
63 return [AuthSourceControlWriteScope, AuthOrchestrationOperateScope];
64 }
65 return Object.hasOwn(CLIENT_GUARDED_RPC_SCOPES, method)
66 ? [CLIENT_GUARDED_RPC_SCOPES[method as ClientGuardedRpcTag]]
67 : [];
68}