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