Skip to content

Commit 2926dae

Browse files
fix(usage): narrow deduplication to approved collector scope
1 parent ef7e627 commit 2926dae

18 files changed

Lines changed: 90 additions & 561 deletions

‎apps/server/src/provider/Drivers/ClaudeDriver.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv> = {
200200
lookup: () =>
201201
probeClaudeCapabilities(effectiveConfig, processEnv, cwd).pipe(
202202
Effect.provideService(Path.Path, path),
203-
Effect.provideService(FileSystem.FileSystem, fileSystem),
204203
),
205204
});
206205
const capabilitiesCacheKey = yield* makeClaudeCapabilitiesCacheKey(

‎apps/server/src/provider/Drivers/CodexDriver.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,6 @@ export const CodexDriver: ProviderDriver<CodexSettings, CodexDriverEnv> = {
231231
),
232232
),
233233
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
234-
Effect.provideService(FileSystem.FileSystem, fileSystem),
235-
Effect.provideService(Path.Path, pathService),
236234
);
237235
const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
238236
const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<CodexSettings>>({

‎apps/server/src/provider/Drivers/CodexManagedProvider.ts‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { ProviderDriverKind, TextGenerationError, type CodexSettings } from "@t3tools/contracts";
22
import * as Effect from "effect/Effect";
3-
import * as FileSystem from "effect/FileSystem";
4-
import * as Path from "effect/Path";
53
import * as Stream from "effect/Stream";
64
import * as Option from "effect/Option";
75
import { ChildProcessSpawner } from "effect/unstable/process";
@@ -30,8 +28,6 @@ export const makeManagedCodexProvider = Effect.fn("makeManagedCodexProvider")(fu
3028
const { instanceId, enabled, displayName, accentColor, config } = input;
3129
const http = yield* HttpClient.HttpClient;
3230
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
33-
const fs = yield* FileSystem.FileSystem;
34-
const path = yield* Path.Path;
3531
const settings = yield* ServerSettingsService;
3632
const runtime = yield* makeCodexManagedRuntime({
3733
instanceId,
@@ -191,11 +187,7 @@ export const makeManagedCodexProvider = Effect.fn("makeManagedCodexProvider")(fu
191187
),
192188
Effect.scoped,
193189
);
194-
}).pipe(
195-
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
196-
Effect.provideService(FileSystem.FileSystem, fs),
197-
Effect.provideService(Path.Path, path),
198-
);
190+
}).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner));
199191
const snapshot = yield* makeManagedServerProvider({
200192
resolveMaintenance: () => Effect.succeed({ provider: DRIVER, packageName: null, update: null }),
201193
getSettings: settings.getSettings,

‎apps/server/src/provider/Layers/ClaudeCapabilitiesProbe.test.ts‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,13 @@ it("isolates Claude capability probes without dropping workspace setting sources
5353
});
5454

5555
it.layer(NodeServices.layer)("Claude capability probe SDK boundary", (it) => {
56-
it.effect.each([false, true])("resolves account config, relative: %s", (relativeConfigDir) =>
56+
it.effect("serializes strict no-MCP options and still resolves account capabilities", () =>
5757
Effect.gen(function* () {
5858
const fs = yield* FileSystem.FileSystem;
5959
const path = yield* Path.Path;
6060
const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-probe-sdk-" });
6161
const executablePath = path.join(tempDir, "fake-claude.mjs");
6262
const invocationPath = path.join(tempDir, "invocation.json");
63-
yield* fs.writeFileString(
64-
path.join(tempDir, ".claude.json"),
65-
'{"oauthAccount":{"organizationUuid":"org-a"}}',
66-
);
6763
// The probe aborts the SDK without awaiting the child's exit, and on
6864
// Windows a directory that is still some process's cwd cannot be
6965
// removed. Keep the workspace outside the scoped directory and let it
@@ -140,21 +136,16 @@ it.layer(NodeServices.layer)("Claude capability probe SDK boundary", (it) => {
140136
yield* fs.chmod(executablePath, 0o755);
141137

142138
const capabilities = yield* probeClaudeCapabilities(
143-
decodeClaudeSettings({
144-
binaryPath: executablePath,
145-
homePath: relativeConfigDir ? "" : tempDir,
146-
}),
139+
decodeClaudeSettings({ binaryPath: executablePath }),
147140
{
148141
...process.env,
149-
CLAUDE_CONFIG_DIR: path.relative(workspaceCwd, tempDir),
150142
T3_PROBE_INVOCATION_PATH: invocationPath,
151143
ENABLE_CLAUDEAI_MCP_SERVERS: "true",
152144
},
153145
workspaceCwd,
154146
);
155147

156148
assert.deepEqual(capabilities, {
157-
accountId: "org-a",
158149
159150
subscriptionType: "pro",
160151
tokenSource: "oauth",

‎apps/server/src/provider/Layers/ClaudeProvider.ts‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import {
4242
claudeUsageResponseToLimits,
4343
recordClaudeUsageResponse,
4444
} from "./claudeUsageLimits.ts";
45-
import { claudeAccountConfigPath, readClaudeAccountId } from "./claudeResetCredits.ts";
4645
import {
4746
BUNDLED_CLAUDE_MODEL_CATALOG,
4847
type ClaudeModelCatalog,
@@ -227,7 +226,6 @@ function nonEmptyProbeString(value: string): string | undefined {
227226
}
228227

229228
type ClaudeCapabilitiesProbe = {
230-
readonly accountId?: string;
231229
readonly email: string | undefined;
232230
readonly subscriptionType: string | undefined;
233231
readonly tokenSource: string | undefined;
@@ -339,10 +337,6 @@ const probeClaudeCapabilities = (
339337
const abort = new AbortController();
340338
return Effect.gen(function* () {
341339
const claudeEnvironment = yield* makeClaudeEnvironment(claudeSettings, environment);
342-
const accountConfigPath = yield* claudeAccountConfigPath(
343-
claudeEnvironment.CLAUDE_CONFIG_DIR,
344-
cwd,
345-
);
346340
const executablePath = yield* resolveClaudeSdkExecutablePath(
347341
claudeSettings.binaryPath,
348342
claudeEnvironment,
@@ -363,11 +357,11 @@ const probeClaudeCapabilities = (
363357
}),
364358
});
365359
const init = await q.initializationResult();
366-
return { q, init, accountConfigPath };
360+
return { q, init };
367361
});
368362
}).pipe(
369363
Effect.timeout(CAPABILITIES_PROBE_TIMEOUT_MS),
370-
Effect.flatMap(({ q, init, accountConfigPath }) =>
364+
Effect.flatMap(({ q, init }) =>
371365
Effect.gen(function* () {
372366
// Usage has its own deadline so a slow optional request cannot discard initialization.
373367
const usageResult = yield* Effect.tryPromise(() =>
@@ -387,11 +381,7 @@ const probeClaudeCapabilities = (
387381
readonly apiProvider?: string;
388382
}
389383
| undefined;
390-
const accountId = account?.subscriptionType
391-
? yield* readClaudeAccountId(accountConfigPath)
392-
: undefined;
393384
return {
394-
...(accountId ? { accountId } : {}),
395385
email: account?.email,
396386
subscriptionType: account?.subscriptionType,
397387
tokenSource: account?.tokenSource,
@@ -602,7 +592,6 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
602592
auth: {
603593
status: "authenticated",
604594
...(capabilities.email ? { email: capabilities.email } : {}),
605-
...(capabilities.accountId ? { accountId: capabilities.accountId } : {}),
606595
...(authMetadata ? authMetadata : {}),
607596
},
608597
...(versionUpgradeMessage ? { message: versionUpgradeMessage } : {}),

‎apps/server/src/provider/Layers/CodexProvider.test.ts‎

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
import { assert, it } from "@effect/vitest";
2-
import * as NodeServices from "@effect/platform-node/NodeServices";
3-
import * as Effect from "effect/Effect";
4-
import * as FileSystem from "effect/FileSystem";
5-
import * as Path from "effect/Path";
6-
import * as Schema from "effect/Schema";
72

8-
import {
9-
applyPreferredCodexDefaultModel,
10-
mapCodexModelCapabilities,
11-
readCodexAccountId,
12-
} from "./CodexProvider.ts";
13-
14-
const encodeIdTokenAuth = Schema.encodeEffect(
15-
Schema.fromJsonString(Schema.Struct({ tokens: Schema.Struct({ id_token: Schema.String }) })),
16-
);
3+
import { applyPreferredCodexDefaultModel, mapCodexModelCapabilities } from "./CodexProvider.ts";
174

185
it("maps current Codex model capability fields", () => {
196
const capabilities = mapCodexModelCapabilities({
@@ -184,32 +171,3 @@ it("ignores custom models that shadow a preferred slug", () => {
184171

185172
assert.deepStrictEqual(models.find((model) => model.isDefault)?.slug, "gpt-5.4");
186173
});
187-
188-
it.effect(
189-
"reads Codex subscription identity from the instance home without publishing tokens",
190-
() =>
191-
Effect.gen(function* () {
192-
const fs = yield* FileSystem.FileSystem;
193-
const path = yield* Path.Path;
194-
const home = yield* fs.makeTempDirectoryScoped();
195-
const authPath = path.join(home, "auth.json");
196-
yield* fs.writeFileString(
197-
authPath,
198-
'{"tokens":{"account_id":" account-a ","access_token":"private"}}',
199-
);
200-
assert.equal(yield* readCodexAccountId(home, { CODEX_HOME: "/other" }), "account-a");
201-
assert.equal(yield* readCodexAccountId("", { CODEX_HOME: home }), "account-a");
202-
const token = `${Buffer.from("{}").toString("base64url")}.${Buffer.from('{"https://api.openai.com/auth":{"chatgpt_account_id":"account-b"}}').toString("base64url")}.signature`;
203-
yield* fs.writeFileString(
204-
authPath,
205-
yield* encodeIdTokenAuth({ tokens: { id_token: token } }),
206-
);
207-
assert.equal(yield* readCodexAccountId(home, {}), "account-b");
208-
for (const encoded of ["not-json", "{}", '{"tokens":{"id_token":"not-a-jwt"}}']) {
209-
yield* fs.writeFileString(authPath, encoded);
210-
assert.equal(yield* readCodexAccountId(home, {}), undefined);
211-
}
212-
yield* fs.remove(authPath);
213-
assert.equal(yield* readCodexAccountId(home, {}), undefined);
214-
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
215-
);

‎apps/server/src/provider/Layers/CodexProvider.ts‎

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import * as NodeOS from "node:os";
2-
import { decodeJwt } from "jose";
3-
import * as FileSystem from "effect/FileSystem";
4-
import * as Path from "effect/Path";
51
import * as DateTime from "effect/DateTime";
62
import * as Duration from "effect/Duration";
73
import * as Effect from "effect/Effect";
@@ -75,7 +71,6 @@ const CODEX_PRESENTATION = {
7571
} as const;
7672

7773
export interface CodexAppServerProviderSnapshot {
78-
readonly accountId?: string;
7974
readonly account: CodexSchema.V2GetAccountResponse;
8075
readonly rateLimits?: CodexRateLimitsProbe;
8176
readonly version: string | undefined;
@@ -155,48 +150,6 @@ function codexAccountEmail(account: CodexSchema.V2GetAccountResponse["account"])
155150
return account.email;
156151
}
157152

158-
const CodexAuthIdentity = Schema.Struct({
159-
tokens: Schema.optional(
160-
Schema.Struct({
161-
account_id: Schema.optional(Schema.String),
162-
id_token: Schema.optional(Schema.String),
163-
}),
164-
),
165-
});
166-
const CodexIdentityClaims = Schema.Struct({
167-
"https://api.openai.com/auth": Schema.optional(
168-
Schema.Struct({
169-
chatgpt_account_id: Schema.optional(Schema.String),
170-
}),
171-
),
172-
});
173-
174-
/** Reads only grouping identity from the same native home used by the app server. */
175-
export const readCodexAccountId = Effect.fn("readCodexAccountId")(function* (
176-
homePath: string,
177-
environment: NodeJS.ProcessEnv,
178-
) {
179-
const fs = yield* FileSystem.FileSystem;
180-
const path = yield* Path.Path;
181-
const home = homePath.trim()
182-
? expandHomePath(homePath)
183-
: environment.CODEX_HOME || path.join(environment.HOME || NodeOS.homedir(), ".codex");
184-
const auth = yield* fs.readFileString(path.join(home, "auth.json")).pipe(
185-
Effect.flatMap(Schema.decodeEffect(Schema.fromJsonString(CodexAuthIdentity))),
186-
Effect.orElseSucceed(() => undefined),
187-
);
188-
const accountId = auth?.tokens?.account_id?.trim();
189-
if (accountId) return accountId;
190-
const idToken = auth?.tokens?.id_token;
191-
if (!idToken) return undefined;
192-
// This is grouping metadata, not authentication; the CLI verifies its tokens.
193-
const claims = yield* Effect.try(() => decodeJwt(idToken)).pipe(
194-
Effect.flatMap(Schema.decodeUnknownEffect(CodexIdentityClaims)),
195-
Effect.orElseSucceed(() => undefined),
196-
);
197-
return claims?.["https://api.openai.com/auth"]?.chatgpt_account_id?.trim() || undefined;
198-
});
199-
200153
export function mapCodexModelCapabilities(
201154
model: CodexSchema.V2ModelListResponse__Model,
202155
): ModelCapabilities {
@@ -485,10 +438,6 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
485438
} satisfies CodexAppServerProviderSnapshot;
486439
}
487440

488-
const accountId =
489-
!input.skipNativeUsage && accountResponse.account?.type === "chatgpt"
490-
? yield* readCodexAccountId(input.homePath ?? "", input.environment ?? process.env)
491-
: undefined;
492441
const [skillsResponse, models, rateLimits] = yield* Effect.all(
493442
[
494443
client.request("skills/list", {
@@ -523,7 +472,6 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
523472

524473
return {
525474
account: accountResponse,
526-
...(accountId ? { accountId } : {}),
527475
...(rateLimits ? { rateLimits } : {}),
528476
version,
529477
models: applyPreferredCodexDefaultModel(
@@ -630,14 +578,14 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
630578
}) => Effect.Effect<
631579
CodexAppServerProviderSnapshot,
632580
CodexErrors.CodexAppServerError,
633-
ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path | Scope.Scope
581+
ChildProcessSpawner.ChildProcessSpawner | Scope.Scope
634582
> = probeCodexAppServerProvider,
635583
environment?: NodeJS.ProcessEnv,
636584
managedAuth?: ServerProvider["auth"],
637585
): Effect.fn.Return<
638586
ServerProviderDraft,
639587
ServerSettingsError,
640-
ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path
588+
ChildProcessSpawner.ChildProcessSpawner
641589
> {
642590
const resolvedEnvironment = environment ?? process.env;
643591
const checkedAt = DateTime.formatIso(yield* DateTime.now);
@@ -753,10 +701,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
753701
installed: true,
754702
version: snapshot.version ?? null,
755703
status: accountStatus.status,
756-
auth: {
757-
...accountStatus.auth,
758-
...(snapshot.accountId ? { accountId: snapshot.accountId } : {}),
759-
},
704+
auth: accountStatus.auth,
760705
...(accountStatus.message ? { message: accountStatus.message } : {}),
761706
...(managedAuth ? {} : { usageLimits }),
762707
},

‎apps/server/src/provider/Layers/ProviderRegistry.test.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ function booleanDescriptor(id: string, label: string) {
153153
}
154154

155155
type TestClaudeCapabilities = {
156-
readonly accountId?: string;
157156
readonly email: string | undefined;
158157
readonly subscriptionType: string | undefined;
159158
readonly tokenSource: string | undefined;
@@ -410,7 +409,6 @@ it.layer(Layer.mergeAll(TestNodeServices, ServerSettingsModule.layerTest(), Test
410409
const status = yield* checkCodexProviderStatus(defaultCodexSettings, () =>
411410
Effect.succeed(
412411
makeCodexProbeSnapshot({
413-
accountId: "subscription-a",
414412
skills: [
415413
{
416414
name: "github:gh-fix-ci",
@@ -430,7 +428,6 @@ it.layer(Layer.mergeAll(TestNodeServices, ServerSettingsModule.layerTest(), Test
430428
assert.strictEqual(status.auth.type, "chatgpt");
431429
assert.strictEqual(status.auth.label, "ChatGPT Pro 20x Subscription");
432430
assert.strictEqual(status.auth.email, "[email protected]");
433-
assert.strictEqual(status.auth.accountId, "subscription-a");
434431
assert.deepStrictEqual(status.models, [
435432
{
436433
slug: "gpt-live-codex",
@@ -2929,12 +2926,11 @@ it.layer(Layer.mergeAll(TestNodeServices, ServerSettingsModule.layerTest(), Test
29292926
Effect.gen(function* () {
29302927
const status = yield* checkClaudeProviderStatus(
29312928
defaultClaudeSettings,
2932-
claudeCapabilities({ subscriptionType: "pro", accountId: "org-a" }),
2929+
claudeCapabilities(),
29332930
);
29342931
assert.strictEqual(status.status, "ready");
29352932
assert.strictEqual(status.installed, true);
29362933
assert.strictEqual(status.auth.status, "authenticated");
2937-
assert.strictEqual(status.auth.accountId, "org-a");
29382934
}).pipe(
29392935
Effect.provide(
29402936
mockSpawnerLayer((args) => {

‎apps/server/src/provider/Layers/claudeResetCredits.test.ts‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -257,21 +257,3 @@ effectIt.layer(NodeServices.layer)("consumeClaudeResetCredit", (it) => {
257257
}),
258258
);
259259
});
260-
261-
effectIt.effect(
262-
"reads Claude organization identity and tolerates missing or malformed account config",
263-
() =>
264-
Effect.gen(function* () {
265-
const fs = yield* FileSystem.FileSystem;
266-
const login = yield* writeLogin;
267-
expect(yield* ClaudeResetCredits.readClaudeAccountId(login.accountConfigPath)).toBe("org-1");
268-
yield* fs.writeFileString(login.accountConfigPath, "not-json");
269-
expect(
270-
yield* ClaudeResetCredits.readClaudeAccountId(login.accountConfigPath),
271-
).toBeUndefined();
272-
yield* fs.remove(login.accountConfigPath);
273-
expect(
274-
yield* ClaudeResetCredits.readClaudeAccountId(login.accountConfigPath),
275-
).toBeUndefined();
276-
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
277-
);

0 commit comments

Comments
 (0)