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 * as ProjectionProjects from "../persistence/Services/ProjectionProjects.ts";
18import type { ProviderInstance } from "../provider/ProviderDriver.ts";
19import { ProviderInstanceRegistry } from "../provider/Services/ProviderInstanceRegistry.ts";
20import { layerFromProjectRepository, RuntimePolicyV2 } 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
64// Grok's instance offers no Auto-accept edits; the Codex instance advertises no
65// restriction.
66const grokInstanceId = ProviderInstanceId.make("grok");
67const supportedRuntimeModesByInstance = new Map<ProviderInstanceId, ReadonlyArray<RuntimeMode>>([
68 [grokInstanceId, ["approval-required", "auto", "full-access"]],
69]);
70const providerInstanceFor = (instanceId: ProviderInstanceId) =>
71 ({
72 snapshot: {
73 getSnapshot: Effect.succeed({
74 supportedRuntimeModes: supportedRuntimeModesByInstance.get(instanceId),
75 } as ServerProvider),
76 },
77 }) as ProviderInstance;
78
79const TestLayer = layerFromProjectRepository.pipe(
80 Layer.provide(
81 Layer.succeed(ProviderInstanceRegistry, {
82 getInstance: (instanceId) => Effect.succeed(providerInstanceFor(instanceId)),
83 listInstances: Effect.succeed([]),
84 listUnavailable: Effect.succeed([]),
85 streamChanges: Stream.empty,
86 subscribeChanges: Effect.never,
87 }),
88 ),
89 Layer.provide(
90 Layer.mock(ProjectionProjects.ProjectionProjectRepository)({
91 getById: () =>
92 Effect.succeed(
93 Option.some({
94 projectId,
95 title: "Project",
96 workspaceRoot: "/project-root",
97 defaultModelSelection: null,
98 defaultThreadEnvMode: null,
99 autoPull: false,
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(TestLayer)("RuntimePolicyV2", (it) => {
111 it.effect("uses the project root for local-checkout threads", () =>
112 Effect.gen(function* () {
113 const policy = yield* 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* 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* 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 // A provider that advertises no restriction runs every mode as stored.
151 assert.equal(yield* modeFor(providerInstanceId, "auto-accept-edits"), "auto-accept-edits");
152 }),
153 );
154});