Skip to content

Commit ee41108

Browse files
fix(opencode): a duplicate auth.json key keeps the Console workspace
When the selected Console credential and auth.json held the same token, deduplication kept the auth.json entry and dropped the workspace header and workspace fingerprint. The first source of a token now wins. AI-assisted; verified with the tests and tools listed above.
1 parent 075bfea commit ee41108

2 files changed

Lines changed: 158 additions & 29 deletions

File tree

‎packages/provider-opencode/src/server/status.test.ts‎

Lines changed: 150 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import * as Layer from "effect/Layer";
1212
import * as Logger from "effect/Logger";
1313
import * as Schema from "effect/Schema";
1414
import * as Tracer from "effect/Tracer";
15+
import * as TestClock from "effect/testing/TestClock";
1516
import * as HttpClient from "effect/http/HttpClient";
1617
import * as HttpClientResponse from "effect/http/HttpClientResponse";
1718
import { beforeEach } from "vite-plus/test";
19+
import { buildProviderOptionSelectionsFromDescriptors } from "@t3tools/shared/model";
1820
import { settingsSecretLiteral } from "@t3tools/contracts";
1921

2022
import { OpenCodeSettings } from "../settings.ts";
@@ -23,6 +25,7 @@ import * as OpenCodeServerOwner from "./OpenCodeServerOwner.ts";
2325
import {
2426
checkOpenCodeProviderStatus,
2527
loadOpenCode2Workspace,
28+
makeOpenCode2ModelLoader,
2629
type OpenCode2Model,
2730
type OpenCode2Workspace,
2831
} from "./status.ts";
@@ -122,10 +125,10 @@ it.effect(
122125
key: "console-key",
123126
metadata: { orgID: "workspace-id" },
124127
};
128+
// Console credentials share one workspace fingerprint across token refreshes.
129+
const workspaceFingerprint =
130+
"b57cd91082439e0caec0a8016b42c874b7c0cb734814d5fedfca398f62a3614f";
125131
const fingerprints = new Map([
126-
// Console credentials share one workspace fingerprint across token refreshes.
127-
["console-key", "b57cd91082439e0caec0a8016b42c874b7c0cb734814d5fedfca398f62a3614f"],
128-
["oauth-access", "b57cd91082439e0caec0a8016b42c874b7c0cb734814d5fedfca398f62a3614f"],
129132
["go-key", "d0e590e93fc9bd9353f2b993c0c287391cae505da2a78cb698b75418d2f5367e"],
130133
["inline-key", "85649de18e63305548b6407e185f708483727ee6189cc4bc9512ddb17e23e776"],
131134
]);
@@ -136,6 +139,9 @@ it.effect(
136139
inline = false,
137140
databaseFile,
138141
requested,
142+
workspaces = requested.map((key) =>
143+
key === "console-key" || key === "oauth-access" ? "workspace-id" : undefined,
144+
),
139145
} of [
140146
{
141147
consoleRow: {
@@ -159,6 +165,15 @@ it.effect(
159165
},
160166
requested: ["oauth-access"],
161167
},
168+
{
169+
consoleRow: {
170+
type: "oauth",
171+
access: "go-key",
172+
metadata: { server: "https://opencode.ai/console", orgID: "workspace-id" },
173+
},
174+
requested: ["go-key"],
175+
workspaces: ["workspace-id"],
176+
},
162177
{
163178
consoleRow: {
164179
...consoleKey,
@@ -229,12 +244,7 @@ it.effect(
229244
),
230245
);
231246
NodeAssert.deepEqual(authorizations, requested);
232-
NodeAssert.deepEqual(
233-
workspaceIDs,
234-
requested.map((key) =>
235-
key === "console-key" || key === "oauth-access" ? "workspace-id" : undefined,
236-
),
237-
);
247+
NodeAssert.deepEqual(workspaceIDs, workspaces);
238248
NodeAssert.deepEqual(yield* fs.readFile(`${dataHome}/opencode/opencode.db`), before);
239249
NodeAssert.equal(
240250
limits.unavailable?.reason,
@@ -243,7 +253,11 @@ it.effect(
243253
NodeAssert.equal(limits.windows.length, consoleStatus === 500 ? 0 : 3);
244254
NodeAssert.equal(
245255
limits.credentialFingerprint,
246-
consoleStatus === 500 ? undefined : fingerprints.get(requested.at(-1)!),
256+
consoleStatus === 500
257+
? undefined
258+
: workspaces.at(-1)
259+
? workspaceFingerprint
260+
: fingerprints.get(requested.at(-1)!),
247261
);
248262
}
249263
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
@@ -705,6 +719,52 @@ it.layer(layerTest)("checkOpenCodeProviderStatus", (it) => {
705719
}),
706720
);
707721

722+
it.effect("keeps Agent without inventing Reasoning for models with no variants", () =>
723+
Effect.gen(function* () {
724+
runtimeMock.state.inventory = {
725+
providerList: {
726+
connected: ["opencode"],
727+
all: [
728+
{
729+
id: "opencode",
730+
name: "OpenCode Zen",
731+
models: {
732+
"big-pickle": { id: "big-pickle", name: "Big Pickle", variants: {} },
733+
"glm-5": { id: "glm-5", name: "GLM-5" },
734+
},
735+
},
736+
],
737+
default: {},
738+
},
739+
agents: [
740+
{ name: "build", hidden: false, mode: "primary" },
741+
{ name: "plan", hidden: false, mode: "primary" },
742+
],
743+
};
744+
const snapshot = yield* checkProvider(makeOpenCodeSettings());
745+
for (const slug of ["opencode/big-pickle", "opencode/glm-5"]) {
746+
const model = snapshot.models.find((entry) => entry.slug === slug);
747+
NodeAssert.ok(model);
748+
const descriptors = model.capabilities?.optionDescriptors;
749+
NodeAssert.deepEqual(descriptors, [
750+
{
751+
id: "agent",
752+
label: "Agent",
753+
type: "select",
754+
options: [
755+
{ id: "build", label: "Build", isDefault: true },
756+
{ id: "plan", label: "Plan" },
757+
],
758+
currentValue: "build",
759+
},
760+
]);
761+
NodeAssert.deepEqual(buildProviderOptionSelectionsFromDescriptors(descriptors), [
762+
{ id: "agent", value: "build" },
763+
]);
764+
}
765+
}),
766+
);
767+
708768
it.effect("includes OpenCode skills in the provider snapshot", () =>
709769
Effect.gen(function* () {
710770
runtimeMock.state.inventory = {
@@ -822,6 +882,12 @@ it.layer(layerTest)("checkOpenCodeProviderStatus", (it) => {
822882
name: "Space Bunny Free",
823883
variants: [{ id: "low" }, { id: "medium" }, { id: "high" }],
824884
},
885+
{
886+
providerID: "opencode-go",
887+
id: "deepseek-v4.1-flash",
888+
name: "DeepSeek V4.1 Flash",
889+
variants: [{ id: "low" }, { id: "high" }, { id: "max" }],
890+
},
825891
]),
826892
);
827893

@@ -841,14 +907,26 @@ it.layer(layerTest)("checkOpenCodeProviderStatus", (it) => {
841907
);
842908
NodeAssert.deepEqual(
843909
snapshot.models.map((model) => model.slug),
844-
["opencode/big-pickle", "opencode/space-bunny-free"],
910+
["opencode/big-pickle", "opencode-go/deepseek-v4.1-flash", "opencode/space-bunny-free"],
845911
);
846912
const variant = snapshot.models.find((model) => model.slug === "opencode/space-bunny-free")
847913
?.capabilities?.optionDescriptors?.[0];
848914
NodeAssert.deepEqual(
849915
variant?.type === "select" ? variant.options.map((option) => option.id) : [],
850916
["low", "medium", "high"],
851917
);
918+
const goVariant = snapshot.models.find(
919+
(model) => model.slug === "opencode-go/deepseek-v4.1-flash",
920+
)?.capabilities?.optionDescriptors?.[0];
921+
const goVariantIds =
922+
goVariant?.type === "select" ? goVariant.options.map((option) => option.id) : [];
923+
NodeAssert.deepEqual(goVariantIds, ["low", "high", "max"]);
924+
NodeAssert.ok(
925+
goVariant?.type === "select" &&
926+
goVariant.currentValue !== undefined &&
927+
goVariantIds.includes(goVariant.currentValue),
928+
"an OpenCode Go model gets a default reasoning variant it lists",
929+
);
852930
NodeAssert.equal(runtimeMock.state.sdkClientInputs.length, 0);
853931
}),
854932
);
@@ -1087,17 +1165,67 @@ const scanned: OpenCode2Workspace = {
10871165
],
10881166
};
10891167

1090-
it.effect("reads a fresh OpenCode 2 directory again once its scan has ended", () =>
1168+
it.effect(
1169+
"reads a loaded OpenCode 2 directory listing no commands again once its scan has ended",
1170+
() =>
1171+
Effect.gen(function* () {
1172+
const reads: Array<OpenCode2Workspace> = [{ commands: [], skills: [] }, scanned];
1173+
const scanEnded = yield* Deferred.make<void>();
1174+
const load = yield* loadOpenCode2Workspace(
1175+
Effect.sync(() => reads.shift()!),
1176+
Deferred.await(scanEnded),
1177+
Effect.succeed(true),
1178+
).pipe(Effect.forkChild);
1179+
yield* Effect.yieldNow;
1180+
NodeAssert.equal(reads.length, 1);
1181+
yield* Deferred.succeed(scanEnded, undefined);
1182+
NodeAssert.deepEqual(yield* Fiber.join(load), scanned);
1183+
}),
1184+
);
1185+
1186+
it.effect("waits for the scan of a cold OpenCode 2 directory that first lists only built-ins", () =>
1187+
Effect.gen(function* () {
1188+
const calls: Array<string> = [];
1189+
const reads: Array<OpenCode2Workspace> = [
1190+
{ commands: builtinCommands, skills: builtinSkills },
1191+
scanned,
1192+
];
1193+
const workspace = yield* loadOpenCode2Workspace(
1194+
Effect.sync(() => (calls.push("read"), reads.shift()!)),
1195+
Effect.sync(() => void calls.push("scanned")),
1196+
Effect.sync(() => (calls.push("loaded"), false)),
1197+
);
1198+
NodeAssert.deepEqual(workspace, scanned);
1199+
NodeAssert.deepEqual(calls, ["loaded", "read", "scanned", "read"]);
1200+
}),
1201+
);
1202+
1203+
it.effect("reads an already loaded OpenCode 2 directory once without waiting for a scan", () =>
10911204
Effect.gen(function* () {
1092-
const reads: Array<OpenCode2Workspace> = [{ commands: [], skills: [] }, scanned];
1093-
const scanEnded = yield* Deferred.make<void>();
1094-
const load = yield* loadOpenCode2Workspace(
1095-
Effect.sync(() => reads.shift()!),
1096-
Deferred.await(scanEnded),
1097-
).pipe(Effect.forkChild);
1098-
yield* Effect.yieldNow;
1099-
NodeAssert.equal(reads.length, 1);
1100-
yield* Deferred.succeed(scanEnded, undefined);
1101-
NodeAssert.deepEqual(yield* Fiber.join(load), scanned);
1205+
const calls: Array<string> = [];
1206+
const workspace = yield* loadOpenCode2Workspace(
1207+
Effect.sync(() => (calls.push("read"), scanned)),
1208+
Effect.sync(() => void calls.push("scanned")),
1209+
Effect.succeed(true),
1210+
);
1211+
NodeAssert.deepEqual(workspace, scanned);
1212+
NodeAssert.deepEqual(calls, ["read"]);
1213+
}),
1214+
);
1215+
1216+
it.effect("lists a catalog whose cold server start outlasts the settle budget", () =>
1217+
Effect.gen(function* () {
1218+
const catalog: ReadonlyArray<OpenCode2Model> = [
1219+
{ providerID: "opencode", id: "big-pickle", name: "Big Pickle", variants: [] },
1220+
];
1221+
let reads = 0;
1222+
// The loader's read includes spawning the server; a loaded host can take 6s to start it.
1223+
const read = Effect.suspend(() =>
1224+
reads++ === 0 ? Effect.sleep("6 seconds").pipe(Effect.as(catalog)) : Effect.succeed(catalog),
1225+
);
1226+
const load = yield* makeOpenCode2ModelLoader(read);
1227+
const fiber = yield* Effect.forkChild(load);
1228+
yield* TestClock.adjust("10 seconds");
1229+
NodeAssert.deepEqual(yield* Fiber.join(fiber), catalog);
11021230
}),
11031231
);

‎packages/provider-opencode/src/server/usageLimits.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,14 @@ export const readOpenCodeGoUsageLimits = Effect.fn("readOpenCodeGoUsageLimits")(
149149
// OpenCode 2 imports auth.json once, so a replaced Go key only exists here.
150150
yield* readStoredCredential(databasePath, "opencode-go"),
151151
];
152-
const credentials = [
153-
...new Map(
154-
[...storedCredentials, goKey ? { token: goKey } : undefined]
155-
.filter((credential): credential is StoredCredential => credential !== undefined)
156-
.map((credential) => [credential.token, credential]),
157-
).values(),
158-
];
152+
// The first source of a token wins, so a duplicate auth.json key keeps its workspace.
153+
const credentials = [storedCredentials, goKey ? [{ token: goKey }] : []]
154+
.flat()
155+
.filter(
156+
(credential, index, all): credential is StoredCredential =>
157+
credential !== undefined &&
158+
all.findIndex((other) => other?.token === credential.token) === index,
159+
);
159160
let credential = credentials[0];
160161
if (!credential) return unsupported;
161162

0 commit comments

Comments
 (0)