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