The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 05:45 UTC
fix(opencode): decode compaction history from OpenCode 2.1 (#22661)
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Julius Marminge committed
Nov 2, 2026 at 19:01 UTC
53e168fef5efa6141c89c1775e0630d6dc488f49
3 files changed
+146
-1
apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
+1
@@ -3630,6 +3630,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
3630
status: "completed",
3631
reason: "auto",
3632
summary: "First turn summary",
3633
+ recent: 42,
3634
},
3635
{ id: "msg_dropped", time: { created: 3 }, text: "second", type: "user" },
3636
],
packages/provider-opencode/src/server/v2/OpenCode2Client.test.ts
+88
@@ -1,7 +1,9 @@
1
import { assert, it } from "@effect/vitest";
2
+import { Session } from "@opencode/client/effect";
3
import * as Effect from "effect/Effect";
4
import * as Layer from "effect/Layer";
5
import * as Redacted from "effect/Redacted";
6
+import * as Schema from "effect/Schema";
7
import * as HttpClient from "effect/http/HttpClient";
8
import * as HttpClientResponse from "effect/http/HttpClientResponse";
9
import { describe } from "vite-plus/test";
@@ -45,3 +47,89 @@ describe("OpenCode2Client authorization", () => {
47
}),
48
);
49
});
50
+
51
+const previous = {
52
+ id: "msg_repro088",
53
+ type: "synthetic",
54
+ text: "Fixture message 88",
55
+ time: { created: 1088 },
56
+};
57
+
58
+const compaction = (fields: Record<string, unknown>) => ({
59
+ type: "compaction",
60
+ id: "msg_repro089",
61
+ time: { created: 1089 },
62
+ reason: "manual",
63
+ summary: "Fixture compaction",
64
+ ...fields,
65
+});
66
+
67
+const listHistory = (messages: ReadonlyArray<unknown>) =>
68
+ Effect.gen(function* () {
69
+ const transport = Layer.succeed(
70
+ HttpClient.HttpClient,
71
+ HttpClient.make((request) =>
72
+ Effect.sync(() => {
73
+ assert.include(request.url, "/api/session/ses_repro/message");
74
+ return HttpClientResponse.fromWeb(
75
+ request,
76
+ new Response(JSON.stringify({ data: messages, cursor: {} }), {
77
+ headers: { "content-type": "application/json" },
78
+ }),
79
+ );
80
+ }),
81
+ ),
82
+ );
83
+ const opencode = yield* OpenCode2Client.OpenCode2Client.pipe(
84
+ Effect.provide(OpenCode2Client.layer.pipe(Layer.provide(transport))),
85
+ );
86
+ const { client } = yield* opencode.connect({
87
+ baseUrl: "http://127.0.0.1:4096",
88
+ password: "test-password",
89
+ });
90
+ return yield* client.message.list({ sessionID: Session.ID.make("ses_repro"), order: "asc" });
91
+ });
92
+
93
+describe("OpenCode2Client message history", () => {
94
+ it.effect.each([
95
+ { status: "running", shape: "native keepID", fields: { metadata: { keepID: previous.id } } },
96
+ { status: "completed", shape: "native keepID", fields: { metadata: { keepID: previous.id } } },
97
+ { status: "running", shape: "native without metadata", fields: {} },
98
+ { status: "completed", shape: "native without metadata", fields: {} },
99
+ {
100
+ status: "completed",
101
+ shape: "legacy metadata",
102
+ fields: { metadata: { recent: previous.id } },
103
+ },
104
+ { status: "running", shape: "top-level", fields: { recent: previous.id } },
105
+ ] as const)("decodes a $status compaction in the $shape format", ({ status, fields }) =>
106
+ Effect.gen(function* () {
107
+ const history = yield* listHistory([previous, compaction({ status, ...fields })]);
108
+
109
+ assert.deepStrictEqual(
110
+ history.data.map((message) => message.id),
111
+ [previous.id, "msg_repro089"],
112
+ );
113
+ assert.deepInclude(history.data[1], {
114
+ type: "compaction",
115
+ status,
116
+ summary: "Fixture compaction",
117
+ recent:
118
+ "metadata" in fields && "recent" in fields.metadata ? previous.id : (fields.recent ?? ""),
119
+ });
120
+ }),
121
+ );
122
+
123
+ it.effect.each([
124
+ { field: "recent", fields: { status: "completed", recent: 42 } },
125
+ { field: "reason", fields: { status: "completed", reason: "scheduled", recent: previous.id } },
126
+ ] as const)("keeps a compaction with an invalid $field an error", ({ field, fields }) =>
127
+ Effect.gen(function* () {
128
+ const error = yield* listHistory([compaction(fields)]).pipe(Effect.flip);
129
+
130
+ assert.strictEqual(error._tag, "ClientError");
131
+ assert.isTrue(Schema.isSchemaError(error.cause));
132
+ assert.include(String(error.cause), field);
133
+ }),
134
+ );
135
+});
packages/provider-opencode/src/server/v2/OpenCode2Client.ts
+57
-1
@@ -142,6 +142,62 @@ const basicAuthorization = (password: string | Redacted.Redacted) => {
142
return `Basic ${Buffer.from(`${OPENCODE_USERNAME}:${plain}`, "utf8").toString("base64")}`;
143
};
144
145
+const MESSAGE_LIST_URL = /\/api\/session\/[^/?#]+\/message(?:[?#]|$)/;
146
+
147
+/** Reads only the page's shape; each message stays opaque for the real schema to judge. */
148
+const MessagePage = Schema.fromJsonString(
149
+ Schema.StructWithRest(Schema.Struct({ data: Schema.Array(Schema.Unknown) }), [
150
+ Schema.Record(Schema.String, Schema.Unknown),
151
+ ]),
152
+);
153
+const decodePage = Schema.decodeUnknownOption(MessagePage);
154
+const encodePage = Schema.encodeSync(MessagePage);
155
+
156
+const CompactionWithRecent = Schema.Struct({
157
+ type: Schema.Literal("compaction"),
158
+ status: Schema.Literals(["running", "completed"]),
159
+ metadata: Schema.optionalKey(Schema.Unknown),
160
+});
161
+const isCompactionWithRecent = Schema.is(CompactionWithRecent);
162
+const isLegacyMetadata = Schema.is(Schema.Struct({ recent: Schema.String }));
163
+
164
+const fillCompactionRecent = (message: unknown) =>
165
+ isCompactionWithRecent(message) && !("recent" in message)
166
+ ? { ...message, recent: isLegacyMetadata(message.metadata) ? message.metadata.recent : "" }
167
+ : message;
168
+
169
+/**
170
+ * OpenCode 2.1 dropped a compaction's `recent`, which the bundled 2.0.28 schema requires.
171
+ * Migrated pre-2.1 compactions keep it under `metadata.recent`; native 2.1 ones have none.
172
+ * T3 never reads it, so fill the gap; every other field still validates downstream.
173
+ */
174
+const fillCompactionRecents = HttpClient.transformResponse(
175
+ Effect.fnUntraced(function* (
176
+ pending: Effect.Effect<HttpClientResponse.HttpClientResponse, HttpClientError.HttpClientError>,
177
+ ) {
178
+ const response = yield* pending;
179
+ const { request } = response;
180
+ if (
181
+ request.method !== "GET" ||
182
+ response.status !== 200 ||
183
+ !MESSAGE_LIST_URL.test(request.url)
184
+ ) {
185
+ return response;
186
+ }
187
+ const text = yield* response.text;
188
+ const page = decodePage(text);
189
+ // The response caches its body, so the untouched one still decodes downstream.
190
+ if (Option.isNone(page)) return response;
191
+ const filled = encodePage({ ...page.value, data: page.value.data.map(fillCompactionRecent) });
192
+ // The filled body is longer than the one the server measured.
193
+ const { "content-length": _, ...headers } = response.headers;
194
+ return HttpClientResponse.fromWeb(
195
+ request,
196
+ new Response(filled, { status: response.status, headers }),
197
+ );
198
+ }),
199
+);
200
+
201
/** @public */
202
export const make = Effect.gen(function* () {
203
const httpClient = yield* HttpClient.HttpClient;
@@ -152,7 +208,7 @@ export const make = Effect.gen(function* () {
208
HttpClientRequest.setHeader("Authorization", basicAuthorization(password)),
209
);
210
return OpenCode.make({ baseUrl }).pipe(
155
- Effect.provideService(HttpClient.HttpClient, authenticated),
211
+ Effect.provideService(HttpClient.HttpClient, fillCompactionRecents(authenticated)),
212
Effect.map((client) => ({
213
client,
214
events: readEvents.pipe(