Repository navigation
Expand file tree
/
Copy pathRuntimePolicy.ts
More file actions
227 lines (213 loc) · 7.99 KB
/
Copy pathRuntimePolicy.ts
File metadata and controls
227 lines (213 loc) · 7.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import {
isRuntimeModeAbove,
managedRuntimeModeCeilingMessage,
ModelSelection,
OrchestrationV2AppThread,
ProjectId,
ProviderInstanceId,
RuntimeMode,
ThreadId,
} from "@t3tools/contracts";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts";
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
import * as ServerSettings from "../serverSettings.ts";
import * as ProjectStore from "./ProjectStore.ts";
export class RuntimePolicyResolveError extends Schema.TaggedError<RuntimePolicyResolveError>()(
"RuntimePolicyResolveError",
{
projectId: ProjectId,
providerInstanceId: ProviderInstanceId,
cause: Schema.optional(Schema.Defect()),
},
) {
override get message(): string {
return `Failed to resolve runtime policy for provider instance ${this.providerInstanceId} in project ${this.projectId}.`;
}
}
/**
* A turn would start in a mode above the policy's ceiling. The orchestrator refuses those when
* they are dispatched; this catches the ones it never saw, such as a run resumed after a restart
* on a thread whose mode predates the policy.
*/
export class RuntimePolicyAboveCeilingError extends Schema.TaggedError<RuntimePolicyAboveCeilingError>()(
"RuntimePolicyAboveCeilingError",
{
threadId: ThreadId,
runtimeMode: RuntimeMode,
maxRuntimeMode: RuntimeMode,
organization: Schema.String,
},
) {
override get message(): string {
return `${managedRuntimeModeCeilingMessage(this.organization, this.maxRuntimeMode)} Switch this thread's mode to continue.`;
}
}
export const isRuntimePolicyAboveCeilingError = Schema.is(RuntimePolicyAboveCeilingError);
export const RuntimePolicyV2Error = Schema.Union([
RuntimePolicyResolveError,
RuntimePolicyAboveCeilingError,
]);
export type RuntimePolicyV2Error = typeof RuntimePolicyV2Error.Type;
export const RuntimePolicyV2Override = Schema.Struct({
cwd: Schema.optional(Schema.String),
approvalPolicy: Schema.optional(Schema.Unknown),
sandboxPolicy: Schema.optional(Schema.Unknown),
reasoningEffort: Schema.optional(Schema.String),
});
export type RuntimePolicyV2Override = typeof RuntimePolicyV2Override.Type;
export interface RuntimePolicyV2Shape {
readonly resolve: (input: {
readonly thread: OrchestrationV2AppThread;
readonly modelSelection: ModelSelection;
}) => Effect.Effect<ProviderAdapter.ProviderAdapterV2RuntimePolicy, RuntimePolicyV2Error>;
}
export class RuntimePolicyV2 extends Context.Service<RuntimePolicyV2, RuntimePolicyV2Shape>()(
"t3/orchestration-v2/RuntimePolicy/RuntimePolicyV2",
) {}
export const layer: Layer.Layer<RuntimePolicyV2> = Layer.succeed(RuntimePolicyV2, {
resolve: Effect.fn("RuntimePolicyV2.resolve")(function (input) {
return Effect.succeed({
runtimeMode: input.thread.runtimeMode,
interactionMode: input.thread.interactionMode,
cwd: input.thread.worktreePath,
});
}),
});
export function providerRuntimeMode(
runtimeMode: RuntimeMode,
supportedRuntimeModes: ReadonlyArray<RuntimeMode> | undefined,
): RuntimeMode {
return supportedRuntimeModes === undefined ||
supportedRuntimeModes.length === 0 ||
supportedRuntimeModes.includes(runtimeMode)
? runtimeMode
: "approval-required";
}
const refuseAboveCeiling = (
managedPolicy: ServerSettings.ServerSettingsService["Service"]["managedPolicy"],
thread: OrchestrationV2AppThread,
) => {
const maxRuntimeMode = managedPolicy?.limits?.maxRuntimeMode;
return managedPolicy !== undefined &&
maxRuntimeMode !== undefined &&
isRuntimeModeAbove(thread.runtimeMode, maxRuntimeMode)
? Effect.fail(
new RuntimePolicyAboveCeilingError({
threadId: thread.id,
runtimeMode: thread.runtimeMode,
maxRuntimeMode,
organization: managedPolicy.organization,
}),
)
: Effect.void;
};
/** Lays the policy's ceiling over another runtime policy, as `layerFromProjectStore` does. */
export const layerEnforcingCeiling: Layer.Layer<
RuntimePolicyV2,
never,
RuntimePolicyV2 | ServerSettings.ServerSettingsService
> = Layer.effect(
RuntimePolicyV2,
Effect.gen(function* () {
const base = yield* RuntimePolicyV2;
const { managedPolicy } = yield* ServerSettings.ServerSettingsService;
return RuntimePolicyV2.of({
resolve: Effect.fn("RuntimePolicyV2.resolveWithinCeiling")(function* (input) {
yield* refuseAboveCeiling(managedPolicy, input.thread);
return yield* base.resolve(input);
}),
});
}),
);
export const layerFromProjectStore: Layer.Layer<
RuntimePolicyV2,
never,
| ProjectStore.ProjectStoreV2
| ProviderInstanceRegistry.ProviderInstanceRegistry
| ServerSettings.ServerSettingsService
> = Layer.effect(
RuntimePolicyV2,
Effect.gen(function* () {
const projects = yield* ProjectStore.ProjectStoreV2;
const providerInstances = yield* ProviderInstanceRegistry.ProviderInstanceRegistry;
// The same ceiling the orchestrator enforces, fixed for the server's life.
const { managedPolicy } = yield* ServerSettings.ServerSettingsService;
const maxRuntimeMode = managedPolicy?.limits?.maxRuntimeMode;
return RuntimePolicyV2.of({
resolve: Effect.fn("RuntimePolicyV2.resolve")(function* (input) {
yield* refuseAboveCeiling(managedPolicy, input.thread);
const instance = yield* providerInstances.getInstance(input.modelSelection.instanceId);
const supportedRuntimeModes =
instance === undefined
? undefined
: (yield* instance.snapshot.getSnapshot).supportedRuntimeModes;
const cwd =
input.thread.worktreePath ??
(yield* projects.get(input.thread.projectId).pipe(
Effect.mapError(
(cause) =>
new RuntimePolicyResolveError({
projectId: input.thread.projectId,
providerInstanceId: input.modelSelection.instanceId,
cause,
}),
),
Effect.flatMap(
Option.match({
onNone: () =>
Effect.fail(
new RuntimePolicyResolveError({
projectId: input.thread.projectId,
providerInstanceId: input.modelSelection.instanceId,
cause: "Project not found.",
}),
),
onSome: (project) => Effect.succeed(project.workspaceRoot),
}),
),
));
return ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
runtimeMode: providerRuntimeMode(input.thread.runtimeMode, supportedRuntimeModes),
interactionMode: input.thread.interactionMode,
cwd,
...(maxRuntimeMode === undefined ? {} : { maxRuntimeMode }),
});
}),
});
}),
);
export function layerWithOverride(
override: RuntimePolicyV2Override,
): Layer.Layer<RuntimePolicyV2, never, RuntimePolicyV2> {
return Layer.effect(
RuntimePolicyV2,
Effect.gen(function* () {
const base = yield* RuntimePolicyV2;
return {
resolve: (input) =>
base.resolve(input).pipe(
Effect.map((policy) =>
ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
...policy,
...(override.cwd === undefined ? {} : { cwd: override.cwd }),
...(override.approvalPolicy === undefined
? {}
: { approvalPolicy: override.approvalPolicy }),
...(override.sandboxPolicy === undefined
? {}
: { sandboxPolicy: override.sandboxPolicy }),
...(override.reasoningEffort === undefined
? {}
: { reasoningEffort: override.reasoningEffort }),
}),
),
),
} satisfies RuntimePolicyV2Shape;
}),
);
}