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