Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { ProviderInstanceId } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6
7import * as ServerConfig from "../config.ts";
8import * as OpenCode2AdapterV2Testkit from "../orchestration-v2/Adapters/OpenCode2AdapterV2.testkit.ts";
9import * as OpenCode2Server from "../provider/opencode2/OpenCode2Server.ts";
10import * as OpenCode2TextGeneration from "./OpenCode2TextGeneration.ts";
11import { OPENCODE2_TITLE_GENERATION } from "./OpenCode2TextGeneration.fixture.ts";
12
13const layer = ServerConfig.ServerConfig.layerTest(process.cwd(), {
14 prefix: "t3code-opencode2-text-generation-test-",
15}).pipe(Layer.provideMerge(NodeServices.layer));
16
17it.layer(layer)("OpenCode2TextGeneration", (it) => {
18 it.effect("generates a title in a temporary session on the free tier and removes it", () =>
19 Effect.gen(function* () {
20 const server = yield* OpenCode2AdapterV2Testkit.replayServer({
21 provider: "opencode",
22 protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
23 version: "2.0.18",
24 scenario: "opencode2_title_generation",
25 entries: [...OPENCODE2_TITLE_GENERATION, { type: "runtime_exit", status: "success" }],
26 });
27 const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
28 Effect.provideService(OpenCode2Server.OpenCode2Server, server),
29 );
30 const title = yield* textGeneration.generateThreadTitle({
31 cwd: process.cwd(),
32 message: "fix the login redirect loop after oauth",
33 modelSelection: {
34 instanceId: ProviderInstanceId.make("opencode"),
35 model: "opencode/big-pickle",
36 },
37 });
38 assert.equal(title.title, "Fix OAuth Login Redirect Loop");
39 }).pipe(Effect.scoped),
40 );
41
42 it.effect("fails at once when the event stream drops before the reply", () =>
43 Effect.gen(function* () {
44 const [subscribe, connected, create, created] = OPENCODE2_TITLE_GENERATION;
45 const sessionId = "ses_f0ec85cf6ffebfalpvV2E9nocu";
46 const server = yield* OpenCode2AdapterV2Testkit.replayServer({
47 provider: "opencode",
48 protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
49 version: "2.0.18",
50 scenario: "opencode2_title_generation_stream_lost",
51 entries: [
52 subscribe!,
53 connected!,
54 create!,
55 created!,
56 {
57 type: "expect_outbound",
58 frame: { type: "session.prompt", input: { sessionID: sessionId, text: "<any>" } },
59 },
60 {
61 type: "emit_inbound",
62 frame: {
63 type: "sdk.response",
64 operation: "session.prompt",
65 data: {
66 data: {
67 id: "msg_1",
68 sessionID: sessionId,
69 time: { created: 1 },
70 type: "user",
71 payload: { text: "t" },
72 delivery: "steer",
73 },
74 },
75 },
76 },
77 // The server goes away before the reply; the temporary session is still removed.
78 { type: "runtime_exit", status: "success" },
79 {
80 type: "expect_outbound",
81 frame: { type: "session.remove", input: { sessionID: sessionId } },
82 },
83 {
84 type: "emit_inbound",
85 frame: { type: "sdk.response", operation: "session.remove", data: null },
86 },
87 ],
88 });
89 const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
90 Effect.provideService(OpenCode2Server.OpenCode2Server, server),
91 );
92 const failure = yield* textGeneration
93 .generateThreadTitle({
94 cwd: process.cwd(),
95 message: "fix the login redirect loop after oauth",
96 modelSelection: {
97 instanceId: ProviderInstanceId.make("opencode"),
98 model: "opencode/big-pickle",
99 },
100 })
101 .pipe(Effect.flip, Effect.timeout("10 seconds"));
102 assert.equal(failure._tag, "TextGenerationError");
103 }).pipe(Effect.scoped),
104 );
105
106 it.effect("keeps a provider's own error message out of the caller-visible detail", () =>
107 Effect.gen(function* () {
108 const [subscribe, connected, create, created] = OPENCODE2_TITLE_GENERATION;
109 const sessionId = "ses_f0ec85cf6ffebfalpvV2E9nocu";
110 // The spike's `text_generation` recording: a deny-all session is refused this way.
111 const refusal = {
112 type: "provider.auth",
113 message:
114 "Error from provider (Console): OpenCode's free tier can only be used from within OpenCode",
115 status: 403,
116 };
117 const server = yield* OpenCode2AdapterV2Testkit.replayServer({
118 provider: "opencode",
119 protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
120 version: "2.0.18",
121 scenario: "opencode2_title_generation_refused",
122 entries: [
123 subscribe!,
124 connected!,
125 create!,
126 created!,
127 {
128 type: "expect_outbound",
129 frame: { type: "session.prompt", input: { sessionID: sessionId, text: "<any>" } },
130 },
131 {
132 type: "emit_inbound",
133 frame: {
134 type: "sdk.response",
135 operation: "session.prompt",
136 data: {
137 data: {
138 id: "msg_1",
139 sessionID: sessionId,
140 time: { created: 1 },
141 type: "user",
142 payload: { text: "t" },
143 delivery: "steer",
144 },
145 },
146 },
147 },
148 {
149 type: "emit_inbound",
150 frame: {
151 type: "sdk.event",
152 event: {
153 id: "evt_0eb7f9ae0001VCNDHVLnqTfoPG",
154 created: 1790657403616,
155 type: "session.execution.failed",
156 data: { sessionID: sessionId, error: refusal },
157 durable: { aggregateID: sessionId, seq: 7, version: 1 },
158 },
159 },
160 },
161 {
162 type: "expect_outbound",
163 frame: { type: "session.remove", input: { sessionID: sessionId } },
164 },
165 {
166 type: "emit_inbound",
167 frame: { type: "sdk.response", operation: "session.remove", data: null },
168 },
169 { type: "runtime_exit", status: "success" },
170 ],
171 });
172 const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
173 Effect.provideService(OpenCode2Server.OpenCode2Server, server),
174 );
175 const failure = yield* textGeneration
176 .generateThreadTitle({
177 cwd: process.cwd(),
178 message: "fix the login redirect loop after oauth",
179 modelSelection: {
180 instanceId: ProviderInstanceId.make("opencode"),
181 model: "opencode/big-pickle",
182 },
183 })
184 .pipe(Effect.flip);
185 assert.equal(failure.detail, "OpenCode could not generate the text.");
186 assert.deepEqual(failure.cause, refusal);
187 }).pipe(Effect.scoped),
188 );
189
190 it.effect("ends at once on an execution end this build cannot decode", () =>
191 Effect.gen(function* () {
192 const [subscribe, connected, create, created] = OPENCODE2_TITLE_GENERATION;
193 const sessionId = "ses_f0ec85cf6ffebfalpvV2E9nocu";
194 const server = yield* OpenCode2AdapterV2Testkit.replayServer({
195 provider: "opencode",
196 protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
197 version: "2.0.18",
198 scenario: "opencode2_title_generation_unreadable_end",
199 entries: [
200 subscribe!,
201 connected!,
202 create!,
203 created!,
204 {
205 type: "expect_outbound",
206 frame: { type: "session.prompt", input: { sessionID: sessionId, text: "<any>" } },
207 },
208 {
209 type: "emit_inbound",
210 frame: {
211 type: "sdk.response",
212 operation: "session.prompt",
213 data: {
214 data: {
215 id: "msg_1",
216 sessionID: sessionId,
217 time: { created: 1 },
218 type: "user",
219 payload: { text: "t" },
220 delivery: "steer",
221 },
222 },
223 },
224 },
225 // A reason added after 2.0.18: the full schema rejects the frame.
226 {
227 type: "emit_inbound",
228 frame: {
229 type: "sdk.event",
230 event: {
231 id: "evt_0eb7f9ae0001VCNDHVLnqTfoPG",
232 created: 1790657403616,
233 type: "session.execution.interrupted",
234 data: { sessionID: sessionId, reason: "budget" },
235 durable: { aggregateID: sessionId, seq: 7, version: 1 },
236 },
237 },
238 },
239 {
240 type: "expect_outbound",
241 frame: { type: "session.remove", input: { sessionID: sessionId } },
242 },
243 {
244 type: "emit_inbound",
245 frame: { type: "sdk.response", operation: "session.remove", data: null },
246 },
247 { type: "runtime_exit", status: "success" },
248 ],
249 });
250 const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
251 Effect.provideService(OpenCode2Server.OpenCode2Server, server),
252 );
253 const failure = yield* textGeneration
254 .generateThreadTitle({
255 cwd: process.cwd(),
256 message: "fix the login redirect loop after oauth",
257 modelSelection: {
258 instanceId: ProviderInstanceId.make("opencode"),
259 model: "opencode/big-pickle",
260 },
261 })
262 .pipe(Effect.flip, Effect.timeout("10 seconds"));
263 assert.equal(failure._tag, "TextGenerationError");
264 }).pipe(Effect.scoped),
265 );
266});