Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import {
3 type ManagedPolicySummary,
4 type ModelSelection,
5 type OrchestrationV2AppThread,
6 ProjectId,
7 ProviderInstanceId,
8 type RuntimeMode,
9 type ServerProvider,
10 ThreadId,
11} from "@t3tools/contracts";
12import * as DateTime from "effect/DateTime";
13import * as Effect from "effect/Effect";
14import * as Layer from "effect/Layer";
15import * as Stream from "effect/Stream";
16
17import type { ProviderInstance } from "@t3tools/provider-core/server/driver";
18import * as ServerSettings from "../serverSettings.ts";
19import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts";
20import * as ProjectStore from "./ProjectStore.ts";
21import * as RuntimePolicy from "./RuntimePolicy.ts";
22
23const projectId = ProjectId.make("project:runtime-policy");
24const providerInstanceId = ProviderInstanceId.make("codex");
25const modelSelection = {
26 instanceId: providerInstanceId,
27 model: "gpt-5.5",
28} satisfies ModelSelection;
29
30function makeThread(input: {
31 readonly now: DateTime.Utc;
32 readonly worktreePath: string | null;
33 readonly runtimeMode?: RuntimeMode;
34}): OrchestrationV2AppThread {
35 const threadId = ThreadId.make("thread:runtime-policy");
36 return {
37 createdBy: "user",
38 creationSource: "web",
39 id: threadId,
40 projectId,
41 title: "Runtime policy",
42 providerInstanceId,
43 modelSelection,
44 runtimeMode: input.runtimeMode ?? "full-access",
45 interactionMode: "default",
46 branch: null,
47 worktreePath: input.worktreePath,
48 activeProviderThreadId: null,
49 lineage: {
50 parentThreadId: null,
51 relationshipToParent: null,
52 rootThreadId: threadId,
53 },
54 forkedFrom: null,
55 createdAt: input.now,
56 updatedAt: input.now,
57 archivedAt: null,
58 settledOverride: null,
59 settledAt: null,
60 lastVisitedAt: null,
61 deletedAt: null,
62 };
63}
64
65const grokInstanceId = ProviderInstanceId.make("grok");
66const supportedRuntimeModesByInstance = new Map<ProviderInstanceId, ReadonlyArray<RuntimeMode>>([
67 [grokInstanceId, ["approval-required", "auto", "full-access"]],
68]);
69const providerInstanceFor = (instanceId: ProviderInstanceId) =>
70 ({
71 snapshot: {
72 getSnapshot: Effect.succeed({
73 supportedRuntimeModes: supportedRuntimeModesByInstance.get(instanceId),
74 } as ServerProvider),
75 },
76 }) as ProviderInstance;
77
78const layerWithPolicy = (policy?: ManagedPolicySummary) =>
79 RuntimePolicy.layerFromProjectStore.pipe(
80 Layer.provide(ServerSettings.layerTest({}, policy)),
81 Layer.provide(
82 Layer.succeed(ProviderInstanceRegistry.ProviderInstanceRegistry, {
83 getInstance: (instanceId) => Effect.succeed(providerInstanceFor(instanceId)),
84 listInstances: Effect.succeed([]),
85 listUnavailable: Effect.succeed([]),
86 streamChanges: Stream.empty,
87 subscribeChanges: Effect.never,
88 }),
89 ),
90 Layer.provide(
91 Layer.mock(ProjectStore.ProjectStoreV2)({
92 get: () =>
93 Effect.succeedSome({
94 projectId,
95 title: "Project",
96 workspaceRoot: "/project-root",
97 defaultModelSelection: null,
98 defaultThreadEnvMode: null,
99 autoPull: false,
100 faviconPath: null,
101 projectIcon: null,
102 scripts: [],
103 pinnedAt: null,
104 archivedAt: null,
105 createdAt: "2026-06-21T00:00:00.000Z",
106 updatedAt: "2026-06-21T00:00:00.000Z",
107 deletedAt: null,
108 }),
109 }),
110 ),
111 );
112
113const layerTest = layerWithPolicy();
114
115it.layer(layerTest)("RuntimePolicyV2", (it) => {
116 it.effect("runs a mode the provider does not offer in Supervised", () =>
117 Effect.gen(function* () {
118 const policy = yield* RuntimePolicy.RuntimePolicyV2;
119 const now = yield* DateTime.now;
120 const modeFor = (instanceId: ProviderInstanceId, runtimeMode: RuntimeMode) =>
121 policy
122 .resolve({
123 thread: makeThread({ now, worktreePath: null, runtimeMode }),
124 modelSelection: { instanceId, model: "test-model" },
125 })
126 .pipe(Effect.map((resolved) => resolved.runtimeMode));
127
128 assert.equal(yield* modeFor(grokInstanceId, "auto-accept-edits"), "approval-required");
129 assert.equal(yield* modeFor(grokInstanceId, "auto"), "auto");
130 assert.equal(yield* modeFor(grokInstanceId, "full-access"), "full-access");
131 assert.equal(yield* modeFor(providerInstanceId, "auto-accept-edits"), "auto-accept-edits");
132 }),
133 );
134});
135
136it.layer(
137 layerWithPolicy({
138 organization: "Example Corp",
139 lockedSettings: [],
140 limits: { maxRuntimeMode: "auto-accept-edits" },
141 }),
142)("RuntimePolicyV2 under a runtime mode ceiling", (it) => {
143 it.effect("refuses to start a turn above the ceiling, such as one resumed after a restart", () =>
144 Effect.gen(function* () {
145 const policy = yield* RuntimePolicy.RuntimePolicyV2;
146 const now = yield* DateTime.now;
147 const resolve = (runtimeMode: RuntimeMode) =>
148 policy.resolve({
149 thread: makeThread({ now, worktreePath: "/work", runtimeMode }),
150 modelSelection,
151 });
152
153 const refused = yield* resolve("full-access").pipe(Effect.flip);
154 assert.equal(refused._tag, "RuntimePolicyAboveCeilingError");
155 assert.equal(
156 refused.message,
157 "Example Corp limits threads to Auto-accept edits. Switch this thread's mode to continue.",
158 );
159 const allowed = yield* resolve("auto-accept-edits");
160 assert.equal(allowed.runtimeMode, "auto-accept-edits");
161 assert.equal(allowed.maxRuntimeMode, "auto-accept-edits");
162 }),
163 );
164});