Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions apps/server/src/provider/Drivers/ClaudeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,13 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv, Path.
catalog,
scopedLimitNames,
(version) =>
ClaudeResetCredits.readClaudeResetCredits(configDir, version).pipe(
Effect.provideService(HttpClient.HttpClient, httpClient),
Effect.provideService(FileSystem.FileSystem, fileSystem),
Effect.provideService(Path.Path, path),
resetCreditCoordinator.shareRead(
configDir,
ClaudeResetCredits.readClaudeResetCredits(configDir, version).pipe(
Effect.provideService(HttpClient.HttpClient, httpClient),
Effect.provideService(FileSystem.FileSystem, fileSystem),
Effect.provideService(Path.Path, path),
),
),
),
),
Expand Down
69 changes: 69 additions & 0 deletions apps/server/src/provider/ProviderInstanceRegistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,75 @@ describe("ProviderInstanceRegistry — multi-instance codex slice", () => {
}),
);

const countClaudeUsageReads = (instanceCount: number) =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const fixtures = yield* makeTildeProviderFixtures();
yield* fs.writeFileString(
path.join(fixtures.claudeHomePath, ".credentials.json"),
'{"claudeAiOauth":{"accessToken":"fake-token"}}',
);
yield* fs.writeFileString(
path.join(fixtures.claudeHomePath, ".claude.json"),
'{"oauthAccount":{"organizationUuid":"fake-org"}}',
);
let usageReads = 0;
const client = HttpClient.make((request) => {
if (request.url.endsWith("/api/oauth/usage")) usageReads += 1;
return Effect.succeed(
HttpClientResponse.fromWeb(
request,
Response.json({
version: "0.0.0",
cedar_ember: {
eligible: true,
next_grant_id: "grant_a",
grants: [{ id: "grant_a", resets_left: 1, usable_now: true }],
},
}),
),
);
});
const instanceIds = Array.from({ length: instanceCount }, (_, index) =>
ProviderInstanceId.make(`claude_${index}`),
);
const { registry } = yield* makeProviderInstanceRegistry({
drivers: [ClaudeDriver],
configMap: Object.fromEntries(
instanceIds.map((instanceId) => [
instanceId,
{
driver: ProviderDriverKind.make("claudeAgent"),
enabled: true,
config: makeClaudeConfig({
enabled: true,
binaryPath: fixtures.claudeBinaryPath,
homePath: fixtures.claudeHomePath,
}),
},
]),
),
}).pipe(Effect.provideService(HttpClient.HttpClient, client));
const instances = yield* Effect.forEach(instanceIds, (id) => registry.getInstance(id));
usageReads = 0;
const snapshots = yield* Effect.forEach(instances, (instance) => instance!.snapshot.refresh, {
concurrency: "unbounded",
});
for (const snapshot of snapshots) {
expect(snapshot.usageLimits?.resetCredits?.nextCreditId).toBe("grant_a");
}
return usageReads;
}).pipe(Effect.provideService(HostProcess.Platform, "linux"), Effect.provide(layerTest));

it.live.skipIf(windowsHost)("Claude instances on one home share their reset-credit reads", () =>
Effect.gen(function* () {
const alone = yield* countClaudeUsageReads(1);
expect(alone).toBeGreaterThan(0);
expect(yield* countClaudeUsageReads(2)).toBe(alone);
}),
);

const makeClaudeCacheFixture = Effect.fnUntraced(function* (
state: "ready" | "signed-out" | "usage-failed",
) {
Expand Down
16 changes: 16 additions & 0 deletions apps/server/src/provider/resetCreditCoordinator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ describe("ResetCreditCoordinator", () => {
}).pipe(Effect.provide(ResetCreditCoordinator.layerTest)),
);

it.effect("shares a read per account until the window passes or a redemption", () =>
Effect.gen(function* () {
const { redeem, shareRead } = yield* ResetCreditCoordinator.ResetCreditCoordinator;
const reads = yield* Ref.make(0);
const read = Ref.updateAndGet(reads, (n) => n + 1);

assert.strictEqual(yield* shareRead("acct", read), 1);
assert.strictEqual(yield* shareRead("acct", read), 1);
assert.strictEqual(yield* shareRead("other", read), 2);
yield* redeem("acct", () => Effect.succeed("reset" as const));
assert.strictEqual(yield* shareRead("acct", read), 3);
yield* TestClock.adjust("10 seconds");
assert.strictEqual(yield* shareRead("acct", read), 4);
}).pipe(Effect.provide(ResetCreditCoordinator.layerTest)),
);

it.effect("serialises concurrent redemptions on the same account, not per caller", () =>
Effect.gen(function* () {
const { redeem } = yield* ResetCreditCoordinator.ResetCreditCoordinator;
Expand Down
55 changes: 53 additions & 2 deletions apps/server/src/provider/resetCreditCoordinator.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
/** Instances sharing a login directory share credits; serialize redemptions on that directory. */
/** Instances sharing a login directory share credits; serialize redemptions and share reads on it. */
import type { ProviderConsumeResetCreditOutcome, ServerProvider } from "@t3tools/contracts";
import * as Context from "effect/Context";
import * as SecureRandom from "effect/crypto/SecureRandom";
import * as Clock from "effect/Clock";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import type * as PlatformError from "effect/PlatformError";
import * as Ref from "effect/Ref";
import * as Schedule from "effect/Schedule";
import * as Semaphore from "effect/Semaphore";

// Instances refresh within moments of each other, each after its own CLI probe; one read serves them.
const SHARED_READ_WINDOW_MS = 10_000;

interface AccountRedemptionState {
readonly lock: Semaphore.Semaphore;
readonly pendingKey: Ref.Ref<string | null>;
Expand All @@ -23,13 +28,26 @@ export class ResetCreditCoordinator extends Context.Service<
consume: (idempotencyKey: string) => Effect.Effect<ProviderConsumeResetCreditOutcome, E, R>,
isSettled?: (error: E) => boolean,
) => Effect.Effect<ProviderConsumeResetCreditOutcome, E | PlatformError.PlatformError, R>;
/** Reads for one account within a short window share one result; a redemption drops it. */
readonly shareRead: <A, E>(
accountKey: string,
read: Effect.Effect<A, E>,
) => Effect.Effect<A, E>;
}
>()("t3/provider/resetCreditCoordinator") {}

/** @public */
export const make = Effect.gen(function* () {
const random = yield* SecureRandom.SecureRandom;
const statesRef = yield* Ref.make<ReadonlyMap<string, AccountRedemptionState>>(new Map());
const scope = yield* Effect.scope;
const readsLock = yield* Semaphore.make(1);
const readsRef = yield* Ref.make<
ReadonlyMap<
string,
{ readonly fiber: Fiber.Fiber<unknown, unknown>; readonly startedAt: number }
>
>(new Map());

// Create account locks atomically so concurrent first redemptions cannot use separate locks.
const stateFor = Effect.fn("ResetCreditCoordinator.stateFor")(function* (accountKey: string) {
Expand All @@ -48,6 +66,15 @@ export const make = Effect.gen(function* () {
});
});

const forgetRead = (accountKey: string) =>
readsLock.withPermits(1)(
Ref.update(readsRef, (reads) => {
const next = new Map(reads);
next.delete(accountKey);
return next;
}),
);

const redeem: ResetCreditCoordinator["Service"]["redeem"] = Effect.fn(
"resetCreditCoordinator.redeem",
)(function* (accountKey, consume, isSettled) {
Expand All @@ -61,14 +88,38 @@ export const make = Effect.gen(function* () {
Effect.tapError((error) =>
isSettled?.(error) ? Ref.set(state.pendingKey, null) : Effect.void,
),
Effect.ensuring(forgetRead(accountKey)),
);
yield* Ref.set(state.pendingKey, null);
return outcome;
}),
);
});

return { redeem } satisfies ResetCreditCoordinator["Service"];
const shareRead: ResetCreditCoordinator["Service"]["shareRead"] = Effect.fn(
"resetCreditCoordinator.shareRead",
)(function* <A, E>(accountKey: string, read: Effect.Effect<A, E>) {
const fiber = yield* readsLock.withPermits(1)(
Effect.uninterruptible(
Effect.gen(function* () {
const now = yield* Clock.currentTimeMillis;
const shared = (yield* Ref.get(readsRef)).get(accountKey);
if (shared && now - shared.startedAt < SHARED_READ_WINDOW_MS) {
return shared.fiber as Fiber.Fiber<A, E>;
}
// Forked into the coordinator's scope so one caller's interruption never cancels the others.
const fiber = yield* Effect.forkIn(read, scope);
yield* Ref.update(readsRef, (reads) =>
new Map(reads).set(accountKey, { fiber, startedAt: now }),
);
return fiber;
}),
),
);
return yield* Fiber.join(fiber);
});

return { redeem, shareRead } satisfies ResetCreditCoordinator["Service"];
});

type UsageSnapshot = Pick<ServerProvider, "usageLimits">;
Expand Down
Loading