@devries-cameron20 / t3code Mirror · read-only / commits / f4e3f250c6
Fork 0

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, 23:15 UTC

fix(server): resolve Codex usage tiers from initial run history (#21898)

Julius Marminge committed Oct 19, 2026 at 22:05 UTC f4e3f250c6f1f8ff77e262ff706845b772592224
7 files changed +400 -2
apps/server/src/persistence/Migrations.ts
+2
@@ -67,6 +67,7 @@ import Migration0061 from "./Migrations/061_PullRequestReviewGuides.ts";
67 import Migration0062 from "./Migrations/062_ThreadFailureLookupIndex.ts";
68 import Migration0063 from "./Migrations/063_DropUnusedV1Projections.ts";
69 import Migration0064 from "./Migrations/064_CanonicalizeThreadPullRequestLinks.ts";
70 +import Migration0065 from "./Migrations/065_CodexUsageNativeTurnIndex.ts";
71
72 export const migrationEntries = [
73 [1, "OrchestrationEvents", Migration0001],
@@ -134,6 +135,7 @@ export const migrationEntries = [
135 [62, "ThreadFailureLookupIndex", Migration0062],
136 [63, "DropUnusedV1Projections", Migration0063],
137 [64, "CanonicalizeThreadPullRequestLinks", Migration0064],
138 + [65, "CodexUsageNativeTurnIndex", Migration0065],
139 ] as const;
140
141 export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
apps/server/src/persistence/Migrations/055_OrchestrationV2.test.ts
+72 -1
@@ -1,11 +1,13 @@
1 import { assert, it } from "@effect/vitest";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 +import * as Schema from "effect/Schema";
5 import * as SqlClient from "effect/sql/SqlClient";
6
7 import { migrationEntries, runMigrations } from "../Migrations.ts";
8 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
9
10 +const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
11 const layer = it.layer(Layer.mergeAll(NodeSqliteClient.layer({ filename: ":memory:" })));
12
13 layer("055_OrchestrationV2", (it) => {
@@ -13,7 +15,7 @@ layer("055_OrchestrationV2", (it) => {
15 Effect.sync(() => {
16 assert.deepStrictEqual(
17 migrationEntries.map(([id]) => id),
16 - Array.from({ length: 64 }, (_, index) => index + 1),
18 + Array.from({ length: 65 }, (_, index) => index + 1),
19 );
20 }),
21 );
@@ -36,6 +38,7 @@ layer("055_OrchestrationV2", (it) => {
38 [62, "ThreadFailureLookupIndex"],
39 [63, "DropUnusedV1Projections"],
40 [64, "CanonicalizeThreadPullRequestLinks"],
41 + [65, "CodexUsageNativeTurnIndex"],
42 ]);
43 assert.deepStrictEqual(yield* runMigrations(), []);
44
@@ -66,6 +69,7 @@ layer("055_OrchestrationV2", (it) => {
69 { migration_id: 62, name: "ThreadFailureLookupIndex" },
70 { migration_id: 63, name: "DropUnusedV1Projections" },
71 { migration_id: 64, name: "CanonicalizeThreadPullRequestLinks" },
72 + { migration_id: 65, name: "CodexUsageNativeTurnIndex" },
73 ]);
74
75 const tables = yield* sql<{ readonly name: string }>`
@@ -140,3 +144,70 @@ layer("055_OrchestrationV2", (it) => {
144 }),
145 );
146 });
147 +
148 +it.effect("indexes batched Codex native turns without discarding duplicate candidates", () =>
149 + Effect.gen(function* () {
150 + const sql = yield* SqlClient.SqlClient;
151 + yield* runMigrations({ toMigrationInclusive: 64 });
152 + for (const [id, driver, nativeId] of [
153 + ["match", "codex", "requested"],
154 + ["duplicate", "codex", "requested"],
155 + ["second", "codex", "requested-second"],
156 + ["other-provider", "claudeAgent", "requested"],
157 + ["unrelated", "codex", "unrelated"],
158 + ] as const) {
159 + yield* sql`INSERT INTO orchestration_v2_projection_run_attempts ${sql.insert({
160 + attempt_id: `attempt-${id}`,
161 + thread_id: `thread-${id}`,
162 + run_id: `run-${id}`,
163 + attempt_ordinal: 1,
164 + root_node_id: `node-${id}`,
165 + provider: driver,
166 + provider_thread_id: `provider-thread-${id}`,
167 + status: "completed",
168 + payload_json: "{}",
169 + })}`;
170 + yield* sql`INSERT INTO orchestration_v2_projection_provider_turns ${sql.insert({
171 + provider_turn_id: `turn-${id}`,
172 + thread_id: `thread-${id}`,
173 + provider_thread_id: `provider-thread-${id}`,
174 + node_id: `node-${id}`,
175 + run_attempt_id: `attempt-${id}`,
176 + ordinal: 1,
177 + status: "completed",
178 + payload_json: encodeJson({ nativeTurnRef: { driver, nativeId, strength: "strong" } }),
179 + })}`;
180 + }
181 + const lookup = sql<{ readonly provider_turn_id: string }>`
182 + SELECT turn.provider_turn_id
183 + FROM orchestration_v2_projection_provider_turns AS turn
184 + JOIN orchestration_v2_projection_run_attempts AS attempt
185 + ON attempt.attempt_id = turn.run_attempt_id
186 + AND attempt.thread_id = turn.thread_id
187 + AND attempt.provider_thread_id = turn.provider_thread_id
188 + WHERE json_extract(turn.payload_json, '$.nativeTurnRef.driver') = 'codex'
189 + AND json_extract(turn.payload_json, '$.nativeTurnRef.nativeId') IN (
190 + SELECT value FROM json_each(${encodeJson(["requested", "requested-second", "missing"])})
191 + )
192 + `;
193 + const before = (yield* lookup).map((row) => row.provider_turn_id).toSorted();
194 + assert.deepStrictEqual(before, ["turn-duplicate", "turn-match", "turn-second"]);
195 + yield* runMigrations();
196 + assert.deepStrictEqual((yield* lookup).map((row) => row.provider_turn_id).toSorted(), before);
197 +
198 + const [statement, parameters] = lookup.compile();
199 + const plan = yield* sql.unsafe<{ readonly detail: string }>(
200 + `EXPLAIN QUERY PLAN ${statement}`,
201 + parameters,
202 + );
203 + assert.isTrue(
204 + plan.some(({ detail }) =>
205 + /SEARCH turn USING INDEX orchestration_v2_provider_turns_codex_native_id_idx\b/.test(
206 + detail,
207 + ),
208 + ),
209 + plan.map(({ detail }) => detail).join("\n"),
210 + );
211 + assert.isFalse(plan.some(({ detail }) => /SCAN turn\b/.test(detail)));
212 + }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
213 +);
apps/server/src/persistence/Migrations/064_CanonicalizeThreadPullRequestLinks.test.ts
+4 -1
@@ -123,7 +123,10 @@ layer("064_CanonicalizeThreadPullRequestLinks", (it) => {
123 'user', ${v1Payload}, '{}', 1)
124 `;
125
126 - assert.deepEqual(yield* runMigrations(), [[64, "CanonicalizeThreadPullRequestLinks"]]);
126 + assert.deepEqual(yield* runMigrations(), [
127 + [64, "CanonicalizeThreadPullRequestLinks"],
128 + [65, "CodexUsageNativeTurnIndex"],
129 + ]);
130 assert.deepEqual(yield* runMigrations(), []);
131 const rows = yield* sql<{ readonly thread_id: string; readonly payload_json: string }>`
132 SELECT thread_id, payload_json FROM orchestration_v2_projection_threads
apps/server/src/persistence/Migrations/065_CodexUsageNativeTurnIndex.ts new
+14
@@ -0,0 +1,14 @@
1 +import * as Effect from "effect/Effect";
2 +import * as SqlClient from "effect/sql/SqlClient";
3 +
4 +export default Effect.gen(function* () {
5 + const sql = yield* SqlClient.SqlClient;
6 +
7 + yield* sql`
8 + CREATE INDEX orchestration_v2_provider_turns_codex_native_id_idx
9 + ON orchestration_v2_projection_provider_turns (
10 + json_extract(payload_json, '$.nativeTurnRef.nativeId')
11 + )
12 + WHERE json_extract(payload_json, '$.nativeTurnRef.driver') = 'codex'
13 + `;
14 +});
apps/server/src/persistence/reconcileV2PreviewMigration.test.ts
+2
@@ -44,6 +44,7 @@ describe("V2 preview upgrade", () => {
44 [62, "ThreadFailureLookupIndex"],
45 [63, "DropUnusedV1Projections"],
46 [64, "CanonicalizeThreadPullRequestLinks"],
47 + [65, "CodexUsageNativeTurnIndex"],
48 ]);
49 assert.deepStrictEqual(yield* runMigrations(), []);
50 assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
@@ -131,6 +132,7 @@ describe("V2 preview upgrade", () => {
132 [62, "ThreadFailureLookupIndex"],
133 [63, "DropUnusedV1Projections"],
134 [64, "CanonicalizeThreadPullRequestLinks"],
135 + [65, "CodexUsageNativeTurnIndex"],
136 ]);
137 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
138 );
apps/server/src/usage/CodexUsageHistory.ts new
+118
@@ -0,0 +1,118 @@
1 +import { ModelSelection } from "@t3tools/contracts";
2 +import type { UsageSpeed } from "@t3tools/provider-core/server/usage";
3 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
4 +import * as Context from "effect/Context";
5 +import * as Effect from "effect/Effect";
6 +import * as Layer from "effect/Layer";
7 +import * as Option from "effect/Option";
8 +import * as Schema from "effect/Schema";
9 +import * as SqlClient from "effect/sql/SqlClient";
10 +
11 +import { getCodexServiceTierOptionValue } from "../codexModelOptions.ts";
12 +
13 +export class CodexUsageHistoryReadError extends Schema.TaggedError<CodexUsageHistoryReadError>()(
14 + "CodexUsageHistoryReadError",
15 + { cause: Schema.Defect() },
16 +) {
17 + override get message(): string {
18 + return "Could not read historical Codex usage tiers.";
19 + }
20 +}
21 +
22 +const decodeSelection = Schema.decodeUnknownOption(Schema.fromJsonString(ModelSelection));
23 +const encodeTurnIds = Schema.encodeSync(Schema.fromJsonString(Schema.Array(Schema.String)));
24 +
25 +export interface CodexUsageHistoryRequest {
26 + readonly sessionId: string;
27 + readonly nativeTurnId: string;
28 + readonly model: string;
29 +}
30 +
31 +export class CodexUsageHistory extends Context.Service<
32 + CodexUsageHistory,
33 + {
34 + readonly resolve: (
35 + requests: readonly CodexUsageHistoryRequest[],
36 + ) => Effect.Effect<
37 + ReadonlyMap<CodexUsageHistoryRequest, UsageSpeed>,
38 + CodexUsageHistoryReadError
39 + >;
40 + }
41 +>()("t3/usage/CodexUsageHistory") {}
42 +
43 +const make = Effect.gen(function* () {
44 + const sql = yield* SqlClient.SqlClient;
45 + const resolve = Effect.fn("CodexUsageHistory.resolve")(function* (
46 + requests: readonly CodexUsageHistoryRequest[],
47 + ) {
48 + const resolved = new Map<CodexUsageHistoryRequest, UsageSpeed>();
49 + if (requests.length === 0) return resolved;
50 + const turnIdsJson = encodeTurnIds([
51 + ...new Set(requests.map((request) => request.nativeTurnId)),
52 + ]);
53 +
54 + // The first running event for this attempt precedes any steering changes
55 + // to the selection. A missing selection there must not fall through to a later event.
56 + const rows = yield* sql<{
57 + readonly native_thread_id: string | null;
58 + readonly native_turn_id: string;
59 + readonly selection_json: string | null;
60 + }>`
61 + SELECT json_extract(attempt.payload_json, '$.nativeThreadId') AS native_thread_id,
62 + json_extract(turn.payload_json, '$.nativeTurnRef.nativeId') AS native_turn_id,
63 + (
64 + SELECT json_extract(event.payload_json, '$.modelSelection')
65 + FROM orchestration_events AS event
66 + WHERE event.aggregate_kind = 'thread'
67 + AND event.application_event_version = 2
68 + AND event.stream_id = attempt.thread_id
69 + AND event.event_type = 'run.updated'
70 + AND json_extract(event.payload_json, '$.id') = attempt.run_id
71 + AND json_extract(event.payload_json, '$.activeAttemptId') = attempt.attempt_id
72 + AND json_extract(event.payload_json, '$.status') = 'running'
73 + ORDER BY event.sequence
74 + LIMIT 1
75 + ) AS selection_json
76 + FROM orchestration_v2_projection_provider_turns AS turn
77 + LEFT JOIN orchestration_v2_projection_run_attempts AS attempt
78 + ON attempt.attempt_id = turn.run_attempt_id
79 + AND attempt.thread_id = turn.thread_id
80 + AND attempt.provider_thread_id = turn.provider_thread_id
81 + WHERE json_extract(turn.payload_json, '$.nativeTurnRef.driver') = 'codex'
82 + AND json_extract(turn.payload_json, '$.nativeTurnRef.nativeId') IN (
83 + SELECT value FROM json_each(${turnIdsJson})
84 + )
85 + `.pipe(
86 + NodeSqliteClient.readOnly,
87 + Effect.mapError((cause) => new CodexUsageHistoryReadError({ cause })),
88 + );
89 +
90 + // Keep duplicates ambiguous even when an attempt is missing or its destination differs.
91 + const byTurn = new Map<string, (typeof rows)[number] | null>();
92 + for (const row of rows) {
93 + byTurn.set(row.native_turn_id, byTurn.has(row.native_turn_id) ? null : row);
94 + }
95 + for (const request of requests) {
96 + const match = byTurn.get(request.nativeTurnId);
97 + if (!match || match.native_thread_id !== request.sessionId) continue;
98 + const selection = decodeSelection(match.selection_json);
99 + if (Option.isNone(selection) || selection.value.model !== request.model) continue;
100 + if (
101 + selection.value.options?.some(
102 + (option) => option.id === "serviceTier" && typeof option.value !== "string",
103 + )
104 + )
105 + continue;
106 + const tier = getCodexServiceTierOptionValue(selection.value);
107 + if (tier === "fast" || tier === "priority") resolved.set(request, "fast");
108 + else if (tier === "ultrafast") resolved.set(request, "ultrafast");
109 + else if (tier === undefined || tier === "default" || tier === "standard") {
110 + resolved.set(request, "standard");
111 + }
112 + }
113 + return resolved;
114 + });
115 + return CodexUsageHistory.of({ resolve });
116 +});
117 +
118 +export const layer = Layer.effect(CodexUsageHistory, make);
apps/server/src/usage/UsageService.test.ts
+188
@@ -32,10 +32,13 @@ import * as Layer from "effect/Layer";
32 import * as Scheduler from "effect/Scheduler";
33 import * as Schema from "effect/Schema";
34 import * as TestClock from "effect/testing/TestClock";
35 +import * as SqlClient from "effect/sql/SqlClient";
36 import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http";
37
38 import * as ServerConfig from "../config.ts";
39 import * as ServerSettings from "../serverSettings.ts";
40 +import * as Sqlite from "../persistence/Sqlite.ts";
41 +import * as CodexUsageHistory from "./CodexUsageHistory.ts";
42 import * as CursorAccountReader from "@t3tools/provider-cursor/server/CursorAccountReader";
43 import * as CursorKeychain from "@t3tools/provider-cursor/server/CursorKeychain";
44 import * as CursorUsageAccounts from "@t3tools/provider-cursor/server/CursorUsageAccounts";
@@ -266,6 +269,191 @@ function cursorSource(summary: { readonly sources: readonly UsageSource[] }) {
269 return summary.sources.find((source) => source.fingerprint.provider === "cursor");
270 }
271
272 +const seedCodexHistory = (input: {
273 + id: string;
274 + nativeTurnId?: string;
275 + sessionId?: string;
276 + model?: string;
277 + options?: readonly { id: string; value: string | boolean }[];
278 +}) =>
279 + Effect.gen(function* () {
280 + const sql = yield* SqlClient.SqlClient;
281 + const { id } = input;
282 + const selection = {
283 + instanceId: "codex",
284 + model: input.model ?? "gpt-6-astra",
285 + options: input.options ?? [{ id: "serviceTier", value: "priority" }],
286 + };
287 + const run = {
288 + id: `run-${id}`,
289 + activeAttemptId: `attempt-${id}`,
290 + status: "running",
291 + modelSelection: selection,
292 + };
293 + yield* sql`INSERT INTO orchestration_v2_projection_run_attempts ${sql.insert({
294 + attempt_id: run.activeAttemptId,
295 + thread_id: `thread-${id}`,
296 + run_id: run.id,
297 + attempt_ordinal: 1,
298 + root_node_id: `node-${id}`,
299 + provider: "codex",
300 + provider_thread_id: `provider-thread-${id}`,
301 + provider_turn_id: `turn-${id}`,
302 + status: "completed",
303 + payload_json: encodeUnknownJsonString({ nativeThreadId: input.sessionId ?? "session" }),
304 + })}`;
305 + yield* sql`INSERT INTO orchestration_v2_projection_provider_turns ${sql.insert({
306 + provider_turn_id: `turn-${id}`,
307 + thread_id: `thread-${id}`,
308 + provider_thread_id: `provider-thread-${id}`,
309 + node_id: `node-${id}`,
310 + run_attempt_id: run.activeAttemptId,
311 + ordinal: 1,
312 + status: "completed",
313 + payload_json: encodeUnknownJsonString({
314 + nativeTurnRef: { driver: "codex", nativeId: input.nativeTurnId ?? id, strength: "strong" },
315 + }),
316 + })}`;
317 + const changedSelection = {
318 + ...selection,
319 + options: [{ id: "serviceTier", value: "ultrafast" }],
320 + };
321 + // These selections disagree so reading the current projection or the wrong event is visible.
322 + yield* sql`INSERT INTO orchestration_v2_projection_runs ${sql.insert({
323 + run_id: run.id,
324 + thread_id: `thread-${id}`,
325 + ordinal: 1,
326 + provider: "codex",
327 + provider_thread_id: `provider-thread-${id}`,
328 + status: "completed",
329 + requested_at: "2026-08-01T10:00:00Z",
330 + payload_json: encodeUnknownJsonString({
331 + ...run,
332 + status: "completed",
333 + modelSelection: { ...selection, options: [] },
334 + }),
335 + })}`;
336 + for (const [index, payload] of [
337 + { ...run, activeAttemptId: "earlier-attempt", modelSelection: changedSelection },
338 + { ...run, status: "queued", modelSelection: changedSelection },
339 + run,
340 + { ...run, modelSelection: changedSelection },
341 + ].entries()) {
342 + yield* sql`INSERT INTO orchestration_events ${sql.insert({
343 + event_id: `event-${id}-${index}`,
344 + aggregate_kind: "thread",
345 + stream_id: `thread-${id}`,
346 + stream_version: index + 1,
347 + event_type: "run.updated",
348 + occurred_at: "2026-08-01T10:00:00Z",
349 + actor_kind: "system",
350 + metadata_json: "{}",
351 + application_event_version: 2,
352 + payload_json: encodeUnknownJsonString(payload),
353 + })}`;
354 + }
355 + });
356 +
357 +describe("Codex usage history", () => {
358 + const historyLayer = CodexUsageHistory.layer.pipe(Layer.provideMerge(Sqlite.layerMemory));
359 + const request = (nativeTurnId: string) => ({
360 + sessionId: "session",
361 + nativeTurnId,
362 + model: "gpt-6-astra",
363 + });
364 +
365 + it.effect("resolves a batch from each attempt's first running selection", () =>
366 + Effect.gen(function* () {
367 + yield* seedCodexHistory({ id: "fast" });
368 + yield* seedCodexHistory({
369 + id: "ultrafast",
370 + options: [{ id: "serviceTier", value: "ultrafast" }],
371 + });
372 + yield* seedCodexHistory({ id: "legacy-option", options: [{ id: "fastMode", value: true }] });
373 + yield* seedCodexHistory({ id: "standard", options: [] });
374 + const requests = ["fast", "ultrafast", "legacy-option", "standard", "fast"].map(request);
375 + const history = yield* CodexUsageHistory.CodexUsageHistory;
376 + const resolved = yield* history.resolve(requests);
377 + assert.strictEqual(resolved.size, requests.length);
378 + assert.deepStrictEqual(
379 + requests.map((item) => resolved.get(item)),
380 + ["fast", "ultrafast", "fast", "standard", "fast"],
381 + );
382 + }).pipe(Effect.provide(historyLayer)),
383 + );
384 +
385 + it.effect("leaves ambiguous, mismatched, and missing V2 evidence unresolved", () =>
386 + Effect.gen(function* () {
387 + for (const id of [
388 + "valid",
389 + "duplicate-model",
390 + "duplicate-session",
391 + "imported",
392 + "v1-event",
393 + "queued",
394 + "missing-selection",
395 + ]) {
396 + yield* seedCodexHistory({ id });
397 + }
398 + yield* seedCodexHistory({
399 + id: "second-model",
400 + nativeTurnId: "duplicate-model",
401 + model: "other-model",
402 + });
403 + yield* seedCodexHistory({
404 + id: "second-session",
405 + nativeTurnId: "duplicate-session",
406 + sessionId: "other-session",
407 + });
408 + yield* seedCodexHistory({ id: "wrong-model", model: "other-model" });
409 + yield* seedCodexHistory({ id: "wrong-session", sessionId: "other-session" });
410 + const sql = yield* SqlClient.SqlClient;
411 + yield* sql`UPDATE orchestration_v2_projection_provider_turns
412 + SET run_attempt_id = NULL WHERE provider_turn_id = 'turn-imported'`;
413 + yield* sql`DELETE FROM orchestration_v2_projection_run_attempts WHERE attempt_id = 'attempt-imported'`;
414 + yield* sql`UPDATE orchestration_events SET application_event_version = 1
415 + WHERE stream_id = 'thread-v1-event'`;
416 + yield* sql`UPDATE orchestration_events
417 + SET payload_json = json_set(payload_json, '$.status', 'queued')
418 + WHERE stream_id = 'thread-queued'`;
419 + yield* sql`UPDATE orchestration_events
420 + SET payload_json = json_remove(payload_json, '$.modelSelection')
421 + WHERE event_id = 'event-missing-selection-2'`;
422 + const requests = [
423 + "valid",
424 + "duplicate-model",
425 + "duplicate-session",
426 + "wrong-model",
427 + "wrong-session",
428 + "missing-turn",
429 + "imported",
430 + "v1-event",
431 + "queued",
432 + "missing-selection",
433 + ].map(request);
434 + const history = yield* CodexUsageHistory.CodexUsageHistory;
435 + const resolved = yield* history.resolve(requests);
436 + assert.deepStrictEqual([...resolved], [[requests[0], "fast"]]);
437 + }).pipe(Effect.provide(historyLayer)),
438 + );
439 +
440 + it.effect(
441 + "reports SQL failures through the typed channel and can resolve again after repair",
442 + () =>
443 + Effect.gen(function* () {
444 + yield* seedCodexHistory({ id: "retry" });
445 + const history = yield* CodexUsageHistory.CodexUsageHistory;
446 + const sql = yield* SqlClient.SqlClient;
447 + const input = request("retry");
448 + yield* sql`ALTER TABLE orchestration_events RENAME TO unavailable_events`;
449 + const error = yield* Effect.flip(history.resolve([input]));
450 + assert.instanceOf(error, CodexUsageHistory.CodexUsageHistoryReadError);
451 + yield* sql`ALTER TABLE unavailable_events RENAME TO orchestration_events`;
452 + assert.strictEqual((yield* history.resolve([input])).get(input), "fast");
453 + }).pipe(Effect.provide(historyLayer)),
454 + );
455 +});
456 +
457 describe("UsageService", () => {
458 it.live(
459 "reads Pi per-model usage and auxiliary costs once across forks and repeated entries",