Skip to content

Commit fdb3153

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/cliproxy-account-pooling
2 parents 01d3a68 + e54e71d commit fdb3153

22 files changed

Lines changed: 570 additions & 1721 deletions

‎apps/mobile/app.config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ const config: ExpoConfig = {
226226
slug: "t3-code",
227227
platforms: ["ios", "android"],
228228
scheme: variant.scheme,
229-
version: repoEnv.T3CODE_MOBILE_VERSION ?? "2.0.0",
229+
version: repoEnv.T3CODE_MOBILE_VERSION ?? "2.1.0",
230230
runtimeVersion: {
231231
// Development manifests resolve on every launch, so avoid fingerprint's
232232
// expensive native-project calculation there. Preview and production stay

‎apps/server/src/os-jank.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ effectIt.effect("replaces a Path-spelled Windows PATH instead of adding a second
143143
yield* fixPath().pipe(
144144
Effect.provideService(HostProcess.Platform, "win32"),
145145
Effect.provideService(HostProcess.Environment, env),
146-
Effect.provideService(WindowsShellEnvironment, () => ({ PATH: "C:\\Tools" })),
146+
Effect.provideService(WindowsShellEnvironment, () => Effect.succeed({ PATH: "C:\\Tools" })),
147147
Effect.provideService(CommandAvailability, () => Effect.succeed(true)),
148148
Effect.provide(NodeServices.layer),
149149
);

‎apps/server/src/provider/Drivers/codexUsage.test.ts‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,123 @@ describe("parseCodexLine", () => {
182182
}
183183
});
184184

185+
it.each(["continuation", "fork", "subagent"])(
186+
"bills only the latest request when a %s has no initial counter baseline",
187+
(source) => {
188+
const state = initialCodexScanState();
189+
parseCodexLine(
190+
JSON.stringify({
191+
type: "session_meta",
192+
timestamp: "2026-08-01T05:17:41.000Z",
193+
payload: {
194+
id: "child",
195+
model_provider: "openai",
196+
...(source === "fork" ? { forked_from_id: "parent" } : {}),
197+
...(source === "subagent"
198+
? { source: { subagent: { thread_spawn: { parent_thread_id: "parent" } } } }
199+
: { source: "vscode" }),
200+
},
201+
}),
202+
state,
203+
);
204+
parseCodexLine(turnContext, state);
205+
const request = {
206+
...counters(100, 20),
207+
cached_input_tokens: 30,
208+
cache_write_input_tokens: 10,
209+
reasoning_output_tokens: 5,
210+
};
211+
const cumulative = {
212+
...counters(500, 60),
213+
cached_input_tokens: 200,
214+
cache_write_input_tokens: 20,
215+
reasoning_output_tokens: 15,
216+
};
217+
const first = counted(request, cumulative);
218+
expect(parseCodexLine(first, state)).toMatchObject({
219+
sessionId: "child",
220+
totals: {
221+
uncachedInputTokens: 60,
222+
cachedInputTokens: 30,
223+
cacheCreationTokens: 10,
224+
outputTokens: 20,
225+
reasoningTokens: 5,
226+
},
227+
});
228+
expect(parseCodexLine(first, state)).toBeNull();
229+
expect(parseCodexLine(counted(counters(10, 2), cumulative), state)).toBeNull();
230+
expect(
231+
parseCodexLine(
232+
counted(counters(10, 2), {
233+
...cumulative,
234+
input_tokens: 510,
235+
output_tokens: 62,
236+
total_tokens: 572,
237+
}),
238+
state,
239+
)?.totals.outputTokens,
240+
).toBe(2);
241+
expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
242+
},
243+
);
244+
245+
it.each([
246+
{
247+
...counters(90, 40),
248+
cached_input_tokens: 60,
249+
cache_write_input_tokens: 20,
250+
reasoning_output_tokens: 10,
251+
},
252+
{
253+
...counters(200, 10),
254+
cached_input_tokens: 60,
255+
cache_write_input_tokens: 20,
256+
reasoning_output_tokens: 5,
257+
},
258+
{
259+
...counters(200, 40),
260+
cached_input_tokens: 20,
261+
cache_write_input_tokens: 20,
262+
reasoning_output_tokens: 10,
263+
},
264+
{
265+
...counters(200, 40),
266+
cached_input_tokens: 60,
267+
cache_write_input_tokens: 5,
268+
reasoning_output_tokens: 10,
269+
},
270+
{
271+
...counters(200, 40),
272+
cached_input_tokens: 60,
273+
cache_write_input_tokens: 20,
274+
reasoning_output_tokens: 3,
275+
},
276+
{
277+
...counters(110, 40),
278+
cached_input_tokens: 60,
279+
cache_write_input_tokens: 20,
280+
reasoning_output_tokens: 10,
281+
},
282+
{
283+
...counters(200, 30),
284+
cached_input_tokens: 60,
285+
cache_write_input_tokens: 20,
286+
reasoning_output_tokens: 20,
287+
},
288+
])("rejects an initial snapshot with an impossible inherited baseline: %j", (cumulative) => {
289+
const state = initialCodexScanState();
290+
parseCodexLine(turnContext, state);
291+
const request = {
292+
...counters(100, 20),
293+
cached_input_tokens: 30,
294+
cache_write_input_tokens: 10,
295+
reasoning_output_tokens: 5,
296+
};
297+
expect(parseCodexLine(counted(request, cumulative), state)).toBeNull();
298+
expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(1);
299+
expect(state.lastCumulativeUsage).toEqual(cumulative);
300+
});
301+
185302
it.each(["request", "cumulative"])("skips malformed %s counters and recovers", (malformed) => {
186303
const state = initialCodexScanState();
187304
parseCodexLine(turnContext, state);

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,15 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): UsageRecord |
243243
if (cumulative !== null) {
244244
// A session resumed in a new Codex process restarts its cumulative counters at this request.
245245
const restarted = CODEX_USAGE_FIELDS.every((key) => cumulative[key] === last[key]);
246-
if (
247-
!restarted &&
248-
!CODEX_USAGE_FIELDS.every((key) => cumulative[key] - (previous?.[key] ?? 0) === last[key])
249-
) {
246+
// Forks and paginated continuations can inherit totals without copying a counter snapshot.
247+
// Only bill the latest request, and require the unseen prefix to be valid usage too.
248+
const reconciled =
249+
previous === null
250+
? readCodexTokenUsage(
251+
Object.fromEntries(CODEX_USAGE_FIELDS.map((key) => [key, cumulative[key] - last[key]])),
252+
) !== null
253+
: CODEX_USAGE_FIELDS.every((key) => cumulative[key] - previous[key] === last[key]);
254+
if (!restarted && !reconciled) {
250255
state.malformedRecords += 1;
251256
return null;
252257
}

‎apps/server/src/usage/UsageService.test.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,7 +1881,7 @@ describe("UsageService", () => {
18811881
}).pipe(Effect.scoped),
18821882
);
18831883

1884-
it.live.each([4, 5, 6, 7])(
1884+
it.live.each([4, 5, 6, 7, 8])(
18851885
"upgrades a v%s cache: reparses live Codex usage, keeps deleted rollouts and the old file",
18861886
(version) =>
18871887
Effect.gen(function* () {
@@ -1903,7 +1903,10 @@ describe("UsageService", () => {
19031903
timestamp: "2026-08-01T10:00:00Z",
19041904
payload: {
19051905
type: "token_count",
1906-
info: { last_token_usage: { input_tokens: 0, output_tokens: outputTokens } },
1906+
info: {
1907+
last_token_usage: { input_tokens: 0, output_tokens: outputTokens },
1908+
total_token_usage: { input_tokens: 100, output_tokens: outputTokens + 30 },
1909+
},
19071910
},
19081911
},
19091912
]
@@ -1929,13 +1932,14 @@ describe("UsageService", () => {
19291932
yield* first.awaitPersisted;
19301933

19311934
const legacy = yield* Effect.promise(async () => {
1935+
const livePath = await NodeFSP.realpath(live);
19321936
const document = decodeUnknownJsonString(await NodeFSP.readFile(cachePath, "utf8")) as {
1933-
files: Record<string, { r: unknown[][]; cs: Record<string, unknown> }>;
1937+
files: Record<string, { r: unknown[][]; mr: number; cs: Record<string, unknown> }>;
19341938
};
19351939
for (const [path, file] of Object.entries(document.files)) {
19361940
file.r = file.r.map((row) => [
19371941
...row.slice(0, 6),
1938-
path === live ? 99 : row[6],
1942+
path === livePath ? 99 : row[6],
19391943
...row.slice(7, 10),
19401944
version === 4 ? 0 : 1,
19411945
]);
@@ -1944,6 +1948,11 @@ describe("UsageService", () => {
19441948
delete file.cs.lastCumulativeUsage;
19451949
delete file.cs.malformedRecords;
19461950
}
1951+
if (version === 8 && path === livePath) {
1952+
file.r = [];
1953+
file.mr = 1;
1954+
file.cs.malformedRecords = 1;
1955+
}
19471956
}
19481957
const text = encodeUnknownJsonString({ ...document, version });
19491958
await NodeFSP.rm(cachePath);
@@ -1971,7 +1980,7 @@ describe("UsageService", () => {
19711980
60 + 20 * (version === 4 ? 1 : 2),
19721981
);
19731982
yield* upgraded.awaitPersisted;
1974-
assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v8.json");
1983+
assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v9.json");
19751984
assert.strictEqual(
19761985
yield* Effect.promise(() => NodeFSP.readFile(legacyPath, "utf8")),
19771986
legacy,

‎apps/server/src/usage/usageScanCache.test.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,14 @@ describe("scan cache round trip", () => {
162162
expect(restored).toEqual(original);
163163
});
164164

165-
it.each([5, 6, 7])(
165+
it.each([5, 6, 7, 8])(
166166
"retains v%s history and compatible positions but invalidates obsolete state",
167167
(version) => {
168168
const original = cacheWith([["/gone.jsonl", 100, [record({ speed: "fast" })]]]);
169169
original.set("/gone-codex.jsonl", {
170170
...original.get("/gone.jsonl")!,
171171
provider: "codex",
172+
malformedRecords: 1,
172173
records: [record({ provider: "codex", speed: "fast", dedupeKey: null })],
173174
position: position({
174175
state: {
@@ -191,6 +192,11 @@ describe("scan cache round trip", () => {
191192
malformedRecords: 3,
192193
position: position({ state: { sessionId: "session-a" } }),
193194
});
195+
const cleanCodex = {
196+
...original.get("/gone-codex.jsonl")!,
197+
malformedRecords: 0,
198+
};
199+
original.set("/clean-codex.jsonl", cleanCodex);
194200
const encoded = encodeScanCache(original);
195201
const entry = encoded.files["/gone.jsonl"]!;
196202
const legacy = {
@@ -219,6 +225,15 @@ describe("scan cache round trip", () => {
219225
size: -1,
220226
position: { resumeOffset: 0, guardLength: 0, guardHash: 0, state: null },
221227
});
228+
expect(restored.get("/clean-codex.jsonl")).toEqual(
229+
version >= 8
230+
? cleanCodex
231+
: {
232+
...cleanCodex,
233+
size: -1,
234+
position: { resumeOffset: 0, guardLength: 0, guardHash: 0, state: null },
235+
},
236+
);
222237
expect(restored.get("/gone-pi.jsonl")).toEqual(
223238
version >= 6
224239
? original.get("/gone-pi.jsonl")

‎apps/server/src/usage/usageScanCache.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ import * as Schema from "effect/Schema";
99
import { CLAUDE_SYNTHETIC_MODEL } from "../provider/Drivers/claudeUsage.ts";
1010
import { GUARD_LENGTH, type TranscriptParsePosition } from "./usageTranscriptReader.ts";
1111

12-
const USAGE_SCAN_CACHE_VERSION = 8 as const;
12+
const USAGE_SCAN_CACHE_VERSION = 9 as const;
1313
const SPEED_COMPATIBLE_SINCE_VERSION = 4;
1414

1515
/**
1616
* An older server sharing that directory cannot read a newer cache and would
1717
* replace it, dropping saved usage for deleted transcripts. Separate files keep
1818
* both.
1919
*/
20-
export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v8.json";
20+
export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v9.json";
2121
export const LEGACY_SCAN_CACHE_FILE_NAMES = [
22+
"usage-scan-cache-v8.json",
2223
"usage-scan-cache-v7.json",
2324
"usage-scan-cache-v6.json",
2425
"usage-scan-cache-v5.json",
@@ -311,9 +312,11 @@ export function decodeScanCache(
311312
continue;
312313
}
313314
// Keep history for deleted rollouts, but re-parse incompatible reducer state.
314-
// Older Codex entries lack provenance state (v7) or misread compaction and resume counters (v8).
315+
// v9 repairs skipped initial Codex requests; clean v8 entries can still resume.
315316
const legacy =
316-
format.state !== undefined && (version < 6 || (entry.p === "codex" && version < 8));
317+
format.state !== undefined &&
318+
(version < 6 ||
319+
(entry.p === "codex" && (version < 8 || (version < 9 && malformedRecords > 0))));
317320
// A corrupt state disqualifies the entry: resuming with it would attach
318321
// appended usage to the wrong model or replay fork-copied history.
319322
if (!legacy && !isValidState.get(entry.p)?.(entry.cs)) continue;

‎apps/server/src/usage/usageTranscriptReader.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,65 @@ describe("readTranscriptRecords resume", () => {
221221
assert.strictEqual(second.tailRecords.length, 0);
222222
});
223223

224+
it.each([
225+
{ mode: "buffered", streamingThresholdBytes: Infinity },
226+
{ mode: "streaming", streamingThresholdBytes: 0 },
227+
])(
228+
"resumes an initial Codex carried-total tail without rebilling it ($mode)",
229+
async (options) => {
230+
const path = NodePath.join(dir, "continuation.jsonl");
231+
const usage = (input: number, output: number) => ({
232+
input_tokens: input,
233+
output_tokens: output,
234+
});
235+
const event = (last: unknown, cumulative: unknown) =>
236+
JSON.stringify({
237+
type: "event_msg",
238+
timestamp: "2026-08-01T10:00:05Z",
239+
payload: {
240+
type: "token_count",
241+
info: { last_token_usage: last, total_token_usage: cumulative },
242+
},
243+
});
244+
const firstLine = event(usage(100, 20), usage(500, 60));
245+
await NodeFSP.writeFile(
246+
path,
247+
codexMetaLine() + codexModelLine("gpt-6-astra") + codexTierLine("ultrafast") + firstLine,
248+
);
249+
const first = await readTranscriptRecords(path, TEST_FORMATS.codex, undefined, options);
250+
assert.isNotNull(first);
251+
assert.strictEqual(first.records.length, 0);
252+
assert.deepStrictEqual(
253+
first.tailRecords.map((r) => [r.totals.outputTokens, r.speed]),
254+
[[20, "ultrafast"]],
255+
);
256+
assert.strictEqual(first.malformedRecords, 0);
257+
258+
await NodeFSP.appendFile(path, `\n${firstLine}\n${event(usage(10, 2), usage(510, 62))}\n`);
259+
const resumed = await readTranscriptRecords(
260+
path,
261+
TEST_FORMATS.codex,
262+
JSON.parse(JSON.stringify(first.position)),
263+
options,
264+
);
265+
const full = await readTranscriptRecords(path, TEST_FORMATS.codex, undefined, options);
266+
assert.isNotNull(resumed);
267+
assert.isNotNull(full);
268+
assert.isTrue(resumed.resumed);
269+
assert.deepStrictEqual(resumed.records, full.records);
270+
assert.deepStrictEqual(
271+
resumed.records.map((r) => [r.totals.outputTokens, r.speed]),
272+
[
273+
[20, "ultrafast"],
274+
[2, "ultrafast"],
275+
],
276+
);
277+
assert.deepStrictEqual(resumed.tailRecords, []);
278+
assert.strictEqual(resumed.malformedRecords, 0);
279+
assert.deepStrictEqual(resumed.position, full.position);
280+
},
281+
);
282+
224283
it("re-parses from the start when the guard bytes no longer match", async () => {
225284
const path = NodePath.join(dir, "claude.jsonl");
226285
await NodeFSP.writeFile(path, claudeLine(1, 5));

0 commit comments

Comments
 (0)