Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DispatchModeLimit.test.ts

138 lines5.3 KB

1import { assert, it } from "@effect/vitest";
2import {
3 CommandId,
4 ProjectId,
5 ProviderDriverKind,
6 ProviderInstanceId,
7 ThreadId,
8} from "@t3tools/contracts";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11import * as SqlitePersistence from "../persistence/Sqlite.ts";
12import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
13import { DispatchModeLimit } from "./DispatchModeLimit.ts";
14import * as Orchestrator from "./Orchestrator.ts";
15import * as ProjectionStore from "./ProjectionStore.ts";
16import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
17import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts";
18import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
19​
20const instanceId = ProviderInstanceId.make("codex");
21const adapter = {
22 instanceId,
23 driver: ProviderDriverKind.make("codex"),
24 getCapabilities: () => Effect.succeed(CodexProviderCapabilitiesV2),
25 planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" as const }),
26 openSession: () => Effect.die("No provider process needed for metadata commands"),
27} as ProviderAdapter.ProviderAdapterV2["Service"];
28const layerDatabase = SqlitePersistence.layerMemory;
29const layerTest = Layer.mergeAll(
30 layerDatabase,
31 ProjectionStore.layer.pipe(Layer.provide(layerDatabase)),
32 ProviderReplayHarness.layerWithRegistry(
33 { name: "dispatch-mode-limit" },
34 ProviderAdapterRegistry.layerFromAdapters([adapter]),
35 { databaseLayer: layerDatabase, runEffectWorker: false },
36 ),
37);
38​
39const supervised = { runtimeMode: "approval-required", interactionMode: "default" } as const;
40​
41const createThread = (threadId: ThreadId) =>
42 Effect.gen(function* () {
43 const orchestrator = yield* Orchestrator.OrchestratorV2;
44 yield* orchestrator.dispatch({
45 type: "thread.create",
46 commandId: CommandId.make(`create:${threadId}`),
47 threadId,
48 projectId: ProjectId.make("project:dispatch-mode-limit"),
49 title: "Before",
50 modelSelection: { instanceId, model: "gpt-5" },
51 ...supervised,
52 branch: null,
53 worktreePath: null,
54 createdBy: "user",
55 creationSource: "web",
56 });
57 });
58​
59it.layer(layerTest)("DispatchModeLimit", (it) => {
60 it.effect("refuses a limited command on a thread its user raised, and records nothing", () =>
61 Effect.gen(function* () {
62 const orchestrator = yield* Orchestrator.OrchestratorV2;
63 const projections = yield* ProjectionStore.ProjectionStoreV2;
64 const threadId = ThreadId.make("thread:limit-raised");
65 yield* createThread(threadId);
66 yield* orchestrator.dispatch({
67 type: "thread.runtime-mode.set",
68 commandId: CommandId.make("raise"),
69 threadId,
70 runtimeMode: "full-access",
71 });
72 const rename = {
73 type: "thread.metadata.update",
74 commandId: CommandId.make("rename"),
75 threadId,
76 title: "After",
77 } as const;
78 const refused = yield* orchestrator
79 .dispatch(rename)
80 .pipe(Effect.provideService(DispatchModeLimit, supervised), Effect.flip);
81 assert.equal(refused._tag, "OrchestratorThreadAboveModeLimitError");
82 assert.equal((yield* projections.getThreadShell(threadId))?.title, "Before");
83​
84 yield* orchestrator.dispatch({
85 type: "thread.runtime-mode.set",
86 commandId: CommandId.make("lower"),
87 threadId,
88 runtimeMode: "approval-required",
89 });
90 yield* orchestrator
91 .dispatch(rename)
92 .pipe(Effect.provideService(DispatchModeLimit, supervised));
93 assert.equal((yield* projections.getThreadShell(threadId))?.title, "After");
94 }),
95 );
96​
97 it.effect("refuses to fork a source thread its user raised", () =>
98 Effect.gen(function* () {
99 const orchestrator = yield* Orchestrator.OrchestratorV2;
100 const projections = yield* ProjectionStore.ProjectionStoreV2;
101 const sourceThreadId = ThreadId.make("thread:limit-fork-source");
102 const targetThreadId = ThreadId.make("thread:limit-fork-target");
103 yield* createThread(sourceThreadId);
104 yield* orchestrator.dispatch({
105 type: "thread.runtime-mode.set",
106 commandId: CommandId.make("raise-source"),
107 threadId: sourceThreadId,
108 runtimeMode: "full-access",
109 });
110 const fork = {
111 type: "thread.fork",
112 commandId: CommandId.make("fork"),
113 sourceThreadId,
114 targetThreadId,
115 sourcePoint: { type: "latest_stable" },
116 createdBy: "agent",
117 creationSource: "mcp",
118 } as const;
119 const refused = yield* orchestrator
120 .dispatch(fork)
121 .pipe(Effect.provideService(DispatchModeLimit, supervised), Effect.flip);
122 assert.ok(refused._tag === "OrchestratorThreadAboveModeLimitError");
123 assert.equal(refused.threadId, sourceThreadId);
124 assert.isNull(yield* projections.getThreadShell(targetThreadId));
125​
126 yield* orchestrator.dispatch({
127 type: "thread.runtime-mode.set",
128 commandId: CommandId.make("lower-source"),
129 threadId: sourceThreadId,
130 runtimeMode: "approval-required",
131 });
132 const retried = yield* orchestrator
133 .dispatch(fork)
134 .pipe(Effect.provideService(DispatchModeLimit, supervised), Effect.flip);
135 assert.equal(retried._tag, "OrchestratorDispatchError");
136 }),
137 );
138});