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