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.pullRequestsRunAction]: AuthSourceControlWriteScope,
24 [WS_METHODS.pullRequestsUpdate]: AuthSourceControlWriteScope,
25 [WS_METHODS.pullRequestsComment]: AuthSourceControlWriteScope,
26 [WS_METHODS.pullRequestsUpdateComment]: AuthSourceControlWriteScope,
27 [WS_METHODS.pullRequestsSubmitReview]: AuthSourceControlWriteScope,
28 [WS_METHODS.pullRequestsReplyToThread]: AuthSourceControlWriteScope,
29 [WS_METHODS.pullRequestsSetThreadResolution]: AuthSourceControlWriteScope,
30 [WS_METHODS.pullRequestsSetReaction]: AuthSourceControlWriteScope,
31 [WS_METHODS.pullRequestsSetFilesViewed]: AuthSourceControlWriteScope,
32 [WS_METHODS.pullRequestsRequestReviewers]: AuthSourceControlWriteScope,
33 [WS_METHODS.pullRequestsSetLabels]: AuthSourceControlWriteScope,
34 // Writing a guide runs a model on this environment's providers, which reading one does not.
35 [WS_METHODS.pullRequestsGenerateReviewGuide]: AuthOrchestrationOperateScope,
36 [WS_METHODS.projectsCreateFromThread]: AuthOrchestrationOperateScope,
37 [WS_METHODS.sourceControlCloneRepository]: AuthSourceControlWriteScope,
38 [WS_METHODS.sourceControlPublishRepository]: AuthSourceControlWriteScope,
39 // Signing in replaces the server's GitHub credential, as saving a token in Settings does.
40 [WS_METHODS.sourceControlSignInStart]: AuthSettingsWriteScope,
41 [WS_METHODS.sourceControlSignInWait]: AuthSettingsWriteScope,
42 [WS_METHODS.sourceControlSignInCancel]: AuthSettingsWriteScope,
43 [WS_METHODS.projectCloneStart]: AuthSourceControlWriteScope,
44 [WS_METHODS.projectCloneCancel]: AuthSourceControlWriteScope,
45 [WS_METHODS.projectCloneRetry]: AuthSourceControlWriteScope,
46 [WS_METHODS.vcsPull]: AuthSourceControlWriteScope,
47 [WS_METHODS.gitRunStackedAction]: AuthSourceControlWriteScope,
48 [WS_METHODS.gitPreparePullRequestThread]: AuthSourceControlWriteScope,
49 [WS_METHODS.vcsCreateWorktree]: AuthSourceControlWriteScope,
50 [WS_METHODS.vcsRemoveWorktree]: AuthSourceControlWriteScope,
51 [WS_METHODS.vcsCreateRef]: AuthSourceControlWriteScope,
52 [WS_METHODS.vcsSwitchRef]: AuthSourceControlWriteScope,
53 [WS_METHODS.vcsInit]: AuthSourceControlWriteScope,
54
55 // Starts an agent session on this environment that can read its logs and database.
56 [WS_METHODS.serverPrepareTriage]: AuthOrchestrationOperateScope,
57 [WS_METHODS.scheduledTasksUpsert]: AuthOrchestrationOperateScope,
58 [WS_METHODS.scheduledTasksSetEnabled]: AuthOrchestrationOperateScope,
59 [WS_METHODS.scheduledTasksDelete]: AuthOrchestrationOperateScope,
60 [WS_METHODS.scheduledTasksRunNow]: AuthOrchestrationOperateScope,
61 [WS_METHODS.scheduledTasksRotateWebhookToken]: AuthOrchestrationOperateScope,
62 [ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: AuthOrchestrationOperateScope,
63 // Force stop kills agent processes, as Stop on a terminal does.
64 [ORCHESTRATION_V2_WS_METHODS.forceStopThread]: AuthOrchestrationOperateScope,
65 // A guest link hands thread access to someone outside, like a pairing link.
66 [WS_METHODS.guestShareCreate]: AuthAccessWriteScope,
67 [WS_METHODS.peerLinksLink]: AuthAccessWriteScope,
68 [WS_METHODS.peerLinksUnlink]: AuthAccessWriteScope,
69 // A link lets every agent here reach the other environment: Settings' bar.
70 [WS_METHODS.peerLinksAnswerRequest]: AuthAccessWriteScope,
71} as const;
72export type ClientGuardedRpcTag = keyof typeof CLIENT_GUARDED_RPC_SCOPES;
73
74const decodePrepareThread = Schema.decodeUnknownSync(GitPreparePullRequestThreadInput);
75const decodeStackedAction = Schema.decodeUnknownSync(GitRunStackedActionInput);
76
77export function clientRpcRequiredScopes(
78 method: string,
79 input: unknown,
80): readonly AuthEnvironmentScope[] {
81 if (method === WS_METHODS.gitPreparePullRequestThread && input !== undefined) {
82 const payload = decodePrepareThread(input);
83 if (payload.mode === "worktree" && payload.threadId !== undefined)
84 return [AuthSourceControlWriteScope, AuthOrchestrationOperateScope];
85 }
86 if (method === WS_METHODS.gitRunStackedAction && input !== undefined) {
87 const payload = decodeStackedAction(input);
88 if (
89 payload.threadId !== undefined &&
90 (payload.action === "create_pr" || payload.action === "commit_push_pr")
91 )
92 return [AuthSourceControlWriteScope, AuthOrchestrationReadScope];
93 }
94 return Object.hasOwn(CLIENT_GUARDED_RPC_SCOPES, method)
95 ? [CLIENT_GUARDED_RPC_SCOPES[method as ClientGuardedRpcTag]]
96 : [];
97}