@bitkyc08/opencodex 2.99.0 → 2.100.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS_INSTALL.md +19 -1
- package/README.md +37 -33
- package/SPONSORS.md +5 -1
- package/bin/ocx.mjs +1 -1
- package/gui/dist/assets/{App-C1hGwaN1.css → App-COo8Yopd.css} +1 -1
- package/gui/dist/assets/App-CitvEefy.js +59 -0
- package/gui/dist/assets/Tray-CeKVWQyB.js +1 -0
- package/gui/dist/assets/{brand-mark-wzPe3R-J.js → brand-mark-M92UnxX2.js} +1 -1
- package/gui/dist/assets/index-BP47do6-.js +94 -0
- package/gui/dist/assets/index-DmDooSAG.css +2 -0
- package/gui/dist/assets/{segment-thumb-features-fYEMxBaW.js → segment-thumb-features-CcxZBnxv.js} +1 -1
- package/gui/dist/classic/assets/App-aYgIQMxI.js +58 -0
- package/gui/dist/classic/assets/{Tray-BqZueZOx.js → Tray-D8jfUXpQ.js} +1 -1
- package/gui/dist/classic/assets/index-CfGqHooN.js +94 -0
- package/gui/dist/classic/assets/quota-summary-R-ih7OlS.js +1 -0
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/native/ocx-child/Cargo.lock +215 -0
- package/native/ocx-child/Cargo.toml +31 -0
- package/native/ocx-child/src/cli.rs +1901 -0
- package/native/ocx-child/src/codex_home.rs +429 -0
- package/native/ocx-child/src/codex_settings.rs +231 -0
- package/native/ocx-child/src/daemon.rs +363 -0
- package/native/ocx-child/src/http_wire.rs +619 -0
- package/native/ocx-child/src/journal.rs +977 -0
- package/native/ocx-child/src/lib.rs +22 -0
- package/native/ocx-child/src/main.rs +9 -0
- package/native/ocx-child/src/parity.rs +102 -0
- package/native/ocx-child/src/platform.rs +1487 -0
- package/native/ocx-child/src/relay.rs +1288 -0
- package/native/ocx-child/src/relay_auth.rs +166 -0
- package/native/ocx-child/src/routes.rs +236 -0
- package/native/ocx-child/src/state.rs +190 -0
- package/native/ocx-child/src/test_hooks.rs +58 -0
- package/native/ocx-child/src/toml_surgeon.rs +1156 -0
- package/package.json +8 -2
- package/src/adapters/anthropic.ts +28 -2
- package/src/adapters/claude-cli/adapter.ts +5 -25
- package/src/adapters/claude-cli/instruction-images.ts +31 -0
- package/src/adapters/claude-cli/stable-replay.ts +42 -14
- package/src/adapters/claude-cli/usage-admission.ts +53 -64
- package/src/adapters/coding-agent/protocol.ts +1 -1
- package/src/adapters/devin/live-models.ts +3 -0
- package/src/adapters/google-http.ts +3 -0
- package/src/adapters/google-vertex-key-project.ts +10 -2
- package/src/adapters/google.ts +40 -8
- package/src/adapters/kiro/usage.ts +3 -13
- package/src/adapters/openai-responses/passthrough.ts +5 -2
- package/src/adapters/openai-responses/request-strips.ts +32 -0
- package/src/adapters/openai-responses/tool-schema.ts +9 -4
- package/src/chat/inbound.ts +1 -1
- package/src/claude/auth-detect.ts +26 -8
- package/src/claude/inbound-content-options.ts +18 -3
- package/src/claude/inbound.ts +5 -2
- package/src/cli/bundled-bun-diagnostic.ts +91 -0
- package/src/cli/capabilities-agents-routing.ts +2 -2
- package/src/cli/capabilities-observe-system.ts +60 -0
- package/src/cli/combo-input.ts +6 -0
- package/src/cli/combo-result.ts +1 -1
- package/src/cli/combo.ts +4 -0
- package/src/cli/config-command.ts +16 -2
- package/src/cli/connect.ts +12 -10
- package/src/cli/dispatch.ts +4 -0
- package/src/cli/doctor.ts +11 -0
- package/src/cli/help.ts +1 -0
- package/src/cli/init.ts +4 -0
- package/src/cli/link-enrollment.ts +152 -25
- package/src/cli/link-local-control.ts +49 -0
- package/src/cli/link.ts +37 -10
- package/src/cli/provider-lifecycle-runtime.ts +4 -2
- package/src/cli/registry.ts +13 -0
- package/src/cli/star-prompt.ts +2 -0
- package/src/cli/status.ts +1 -1
- package/src/cli/telemetry-profile.ts +206 -0
- package/src/cli/telemetry.ts +260 -0
- package/src/client/hub-relay.ts +26 -0
- package/src/client/machine-api.ts +3 -0
- package/src/clients/config-export/t3code.ts +44 -5
- package/src/clients/omo-role-models.ts +1 -1
- package/src/codex/catalog/bundled.ts +44 -2
- package/src/codex/runtime.ts +12 -0
- package/src/codex/shim-restore-lock.ts +67 -30
- package/src/combos/types.ts +21 -1
- package/src/config/atomic-write.ts +24 -1
- package/src/config/diagnostics.ts +15 -0
- package/src/config/live-reconcile.ts +17 -0
- package/src/config/load-degrade.ts +16 -0
- package/src/config/provider-validation.ts +29 -0
- package/src/config/schema/config-schema.ts +24 -0
- package/src/config/schema/leaf-validators.ts +17 -0
- package/src/config/telemetry-carry.ts +30 -0
- package/src/config.ts +13 -1
- package/src/generated/compatibility-version.json +405 -153
- package/src/generated/model-metadata.ts +3 -3
- package/src/integrations/aside-profiles.ts +7 -1
- package/src/integrations/owned-refresh.ts +9 -2
- package/src/lab/artifacts/secure-fs.ts +12 -11
- package/src/lab/fabric/scratch.ts +11 -9
- package/src/lab/public/community.ts +2 -3
- package/src/lab/public/file-safety.ts +18 -20
- package/src/lab/public/mutation-lock.ts +109 -337
- package/src/lab/public/private-file.ts +4 -3
- package/src/lib/bun-runtime-preflight.ts +1 -1
- package/src/lib/codex-catalog-read-only.ts +122 -0
- package/src/lib/config-ownership.ts +49 -18
- package/src/lib/errors.ts +11 -0
- package/src/lib/gcp-adc.ts +167 -48
- package/src/lib/jsonc.ts +4 -2
- package/src/lib/local-control-socket.ts +88 -0
- package/src/lib/privacy.ts +15 -0
- package/src/lib/provider-client-headers.ts +3 -0
- package/src/lib/token-estimate.ts +13 -2
- package/src/link/child-binary-source.ts +228 -0
- package/src/link/ephemeral-child.ts +45 -0
- package/src/link/remote-plant.ts +306 -0
- package/src/link/remote-windows.ts +59 -5
- package/src/link/ssh-argv.ts +71 -8
- package/src/link/status-projection.ts +2 -0
- package/src/link/store.ts +5 -1
- package/src/oauth/claude-keychain-read.ts +165 -0
- package/src/oauth/local-token-detect.ts +14 -17
- package/src/protocols/envelope.ts +20 -2
- package/src/providers/api-key-selection.ts +8 -2
- package/src/providers/claude-cli-usage-state.ts +93 -0
- package/src/providers/github-copilot-auto.ts +25 -5
- package/src/providers/meta-muse-plan-refusal.ts +94 -0
- package/src/providers/model-rename-fields.ts +3 -0
- package/src/providers/quota/vendor-probes-key.ts +23 -4
- package/src/providers/quota/vendor-probes-oauth.ts +59 -0
- package/src/providers/quota/workbuddy.ts +2 -2
- package/src/providers/quota.ts +25 -18
- package/src/providers/registry/entries-core.ts +7 -4
- package/src/providers/registry/entries-extended.ts +39 -7
- package/src/providers/registry/model-seeds.ts +10 -2
- package/src/providers/resolved-model-policy.ts +4 -0
- package/src/providers/routed-config.ts +30 -3
- package/src/providers/stale-no-vision-seed-migration.ts +7 -0
- package/src/providers/vertex-environment-authority.ts +50 -0
- package/src/providers/workbuddy-credit-balance.ts +38 -6
- package/src/quota-pool/assemble.ts +14 -6
- package/src/router.ts +1 -0
- package/src/server/adapter-resolve.ts +11 -6
- package/src/server/audio-dictation.ts +21 -7
- package/src/server/auth-cors.ts +19 -0
- package/src/server/background-lifecycle.ts +71 -0
- package/src/server/chat-completions.ts +2 -0
- package/src/server/claude-messages.ts +30 -3
- package/src/server/index/live-sideband.ts +15 -1
- package/src/server/index/local-control-listener.ts +515 -0
- package/src/server/index/optional-listeners.ts +21 -1
- package/src/server/index/serve-options.ts +4 -3
- package/src/server/index/websocket-handler.ts +2 -0
- package/src/server/index.ts +3 -0
- package/src/server/management/combo-routes.ts +6 -1
- package/src/server/management/context.ts +1 -0
- package/src/server/management/link-child-preflight.ts +74 -0
- package/src/server/management/link-enrollment.ts +190 -25
- package/src/server/management/link-route-state.ts +6 -1
- package/src/server/management/link-routes.ts +27 -9
- package/src/server/management/model-rows.ts +4 -0
- package/src/server/management/oauth-account-routes.ts +9 -4
- package/src/server/management/provider-overwrite-carry.ts +9 -2
- package/src/server/management/provider-routes.ts +14 -0
- package/src/server/management/provider-thinking-display-patch.ts +54 -0
- package/src/server/management/route-registry.ts +7 -0
- package/src/server/management/session-routes.ts +4 -1
- package/src/server/management/telemetry-routes.ts +37 -0
- package/src/server/management/usage-aggregate-cache.ts +290 -67
- package/src/server/management-api.ts +11 -2
- package/src/server/messages-native-preflight.ts +166 -0
- package/src/server/messages-native.ts +143 -43
- package/src/server/messages-shared.ts +14 -0
- package/src/server/proxy-liveness.ts +99 -6
- package/src/server/request-log-conversation.ts +18 -2
- package/src/server/request-log.ts +7 -2
- package/src/server/responses/adapter-continuation.ts +1 -0
- package/src/server/responses/adapter-dispatch.ts +11 -28
- package/src/server/responses/adapter-failure-message.ts +68 -0
- package/src/server/responses/combo-stream-preflight.ts +14 -2
- package/src/server/responses/core-auth.ts +26 -5
- package/src/server/responses/core-codex-account.ts +8 -1
- package/src/server/responses/core-combo-failure.ts +6 -1
- package/src/server/responses/core-combo-native.ts +150 -37
- package/src/server/responses/core-combo.ts +15 -9
- package/src/server/responses/core-normalize.ts +1 -0
- package/src/server/responses/core-options.ts +7 -1
- package/src/server/responses/core-pipeline.ts +21 -1
- package/src/server/responses/fetch-helpers.ts +5 -0
- package/src/server/responses/opengateway-completion-guard.ts +7 -3
- package/src/server/responses/passthrough-delivery.ts +8 -1
- package/src/server/responses/passthrough-error.ts +22 -0
- package/src/server/responses/request-prepare.ts +7 -1
- package/src/server/responses/request-transport.ts +28 -5
- package/src/server/responses/run-turn-execution.ts +13 -0
- package/src/server/sse-frame-buffer.ts +5 -0
- package/src/server/ws-bridge.ts +10 -0
- package/src/service/launchd.ts +78 -4
- package/src/telemetry/aggregate.ts +348 -0
- package/src/telemetry/consent.ts +128 -0
- package/src/telemetry/contract.ts +403 -0
- package/src/telemetry/disclosure.ts +19 -0
- package/src/telemetry/install-id.ts +226 -0
- package/src/telemetry/payload.ts +129 -0
- package/src/telemetry/profile/client.ts +150 -0
- package/src/telemetry/profile/contract.ts +288 -0
- package/src/telemetry/profile/disclosure.ts +14 -0
- package/src/telemetry/profile/paths.ts +13 -0
- package/src/telemetry/profile/routes.ts +99 -0
- package/src/telemetry/profile/service.ts +234 -0
- package/src/telemetry/profile/state.ts +189 -0
- package/src/telemetry/profile/sync.ts +276 -0
- package/src/telemetry/routes.ts +183 -0
- package/src/telemetry/scheduler.ts +386 -0
- package/src/telemetry/sender.ts +97 -0
- package/src/telemetry/state.ts +334 -0
- package/src/types/combo-jev-effort-mode.ts +11 -0
- package/src/types/config.ts +3 -0
- package/src/types/provider.ts +11 -1
- package/src/types/request.ts +2 -0
- package/src/types/telemetry.ts +15 -0
- package/src/types/wire.ts +10 -0
- package/src/types.ts +5 -0
- package/src/update/index.ts +7 -1
- package/src/update/minisign.d.mts +17 -0
- package/src/update/minisign.mjs +88 -0
- package/src/update/signed-supply.d.mts +59 -0
- package/src/update/signed-supply.mjs +289 -0
- package/src/update/trust-root.json +38 -0
- package/src/update/update-bounded-json.d.mts +4 -0
- package/src/update/update-bounded-json.mjs +113 -0
- package/src/update/update-bytes.d.mts +2 -0
- package/src/update/update-bytes.mjs +23 -0
- package/src/update/update-candidate-id.d.mts +2 -0
- package/src/update/update-candidate-id.mjs +23 -0
- package/src/update/update-checkpoint-codec.d.mts +33 -0
- package/src/update/update-checkpoint-codec.mjs +217 -0
- package/src/update/update-contracts.d.mts +24 -0
- package/src/update/update-contracts.mjs +207 -0
- package/src/update/update-signature-threshold.d.mts +12 -0
- package/src/update/update-signature-threshold.mjs +47 -0
- package/src/update/update-signed-version.d.mts +5 -0
- package/src/update/update-signed-version.mjs +25 -0
- package/src/update/update-trust-anchor.d.mts +2 -0
- package/src/update/update-trust-anchor.mjs +2 -0
- package/src/usage/cache-diagnostic.ts +195 -50
- package/src/usage/expected-prices.ts +20 -2
- package/src/usage/filter-index.ts +1469 -0
- package/src/usage/ledger-scanner.ts +245 -4
- package/src/usage/source-filter.ts +10 -2
- package/src/usage/summary.ts +16 -0
- package/src/web-search/parse.ts +244 -38
- package/gui/dist/assets/App-BepstgEN.js +0 -59
- package/gui/dist/assets/Tray-BinxbEZo.js +0 -1
- package/gui/dist/assets/index-C-9X2yaI.js +0 -94
- package/gui/dist/assets/index-bnUWypyz.css +0 -2
- package/gui/dist/classic/assets/App-DX88_GCW.js +0 -58
- package/gui/dist/classic/assets/index-fSP71pLB.js +0 -94
- package/gui/dist/classic/assets/quota-summary-y2fl0WBv.js +0 -1
|
@@ -12,6 +12,8 @@ import {
|
|
|
12
12
|
} from "./log";
|
|
13
13
|
|
|
14
14
|
export const USAGE_LEDGER_READ_CHUNK_BYTES = 1024 * 1024;
|
|
15
|
+
/** First window tried when reading one indexed row; most rows fit many times over. */
|
|
16
|
+
export const USAGE_LEDGER_ROW_READ_WINDOW_BYTES = 64 * 1024;
|
|
15
17
|
// Normalized writer rows carry a <=16 KiB route trace and <=500-character captured
|
|
16
18
|
// upstream error, so 1 MiB leaves wide headroom even for an extreme multi-attempt row.
|
|
17
19
|
// Hand-edited rows can still exceed it; those are reported separately instead of making
|
|
@@ -21,7 +23,11 @@ export const USAGE_LEDGER_BOUNDARY_DIGEST_BYTES = 64 * 1024;
|
|
|
21
23
|
|
|
22
24
|
export interface ScanUsageLedgerOptions {
|
|
23
25
|
signal?: AbortSignal;
|
|
24
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Receives each complete row and the absolute byte offset it starts at, so a derived
|
|
28
|
+
* index can address the same row later without re-reading the whole ledger.
|
|
29
|
+
*/
|
|
30
|
+
onEntry: (entry: PersistedUsageEntry, rowStartBytes: number) => void;
|
|
25
31
|
/** Absolute LF boundary returned by a previous scan. Defaults to byte zero. */
|
|
26
32
|
startAtBytes?: number;
|
|
27
33
|
/** Stable path/dev/ino/birthtime identity; required when startAtBytes is nonzero. */
|
|
@@ -207,6 +213,19 @@ async function digestRangeCooperatively(
|
|
|
207
213
|
return hash.digest("hex");
|
|
208
214
|
}
|
|
209
215
|
|
|
216
|
+
/** Synchronous digest of a short range; used to re-validate a 64 KiB boundary. */
|
|
217
|
+
function digestRangeSync(fd: number, from: number, to: number, buffer: Buffer): string {
|
|
218
|
+
const hash = createHash("sha256");
|
|
219
|
+
for (let position = from; position < to;) {
|
|
220
|
+
const requested = Math.min(buffer.byteLength, to - position);
|
|
221
|
+
const read = readSync(fd, buffer, 0, requested, position);
|
|
222
|
+
if (read === 0) throw rebuildRequired("shrink");
|
|
223
|
+
hash.update(buffer.subarray(0, read));
|
|
224
|
+
position += read;
|
|
225
|
+
}
|
|
226
|
+
return hash.digest("hex");
|
|
227
|
+
}
|
|
228
|
+
|
|
210
229
|
/**
|
|
211
230
|
* Cooperatively scans a full ledger or validated append suffix with bounded memory.
|
|
212
231
|
*
|
|
@@ -304,7 +323,7 @@ export async function scanUsageLedgerCooperatively(
|
|
|
304
323
|
}
|
|
305
324
|
}
|
|
306
325
|
|
|
307
|
-
const publishLine = (): void => {
|
|
326
|
+
const publishLine = (rowStartBytes: number): void => {
|
|
308
327
|
if (lineLength === 0 || isJsonWhitespace(line, lineLength)) return;
|
|
309
328
|
let entry: PersistedUsageEntry | undefined;
|
|
310
329
|
try {
|
|
@@ -318,7 +337,7 @@ export async function scanUsageLedgerCooperatively(
|
|
|
318
337
|
invalidRows += 1;
|
|
319
338
|
return;
|
|
320
339
|
}
|
|
321
|
-
options.onEntry(entry);
|
|
340
|
+
options.onEntry(entry, rowStartBytes);
|
|
322
341
|
parsedRows += 1;
|
|
323
342
|
};
|
|
324
343
|
|
|
@@ -360,6 +379,7 @@ export async function scanUsageLedgerCooperatively(
|
|
|
360
379
|
|
|
361
380
|
if (newline < 0 || newline >= read) break;
|
|
362
381
|
linesSinceYield += 1;
|
|
382
|
+
const rowStartBytes = processedThroughBytes;
|
|
363
383
|
processedThroughBytes = chunkStart + newline + 1;
|
|
364
384
|
if (pendingLineBytes + 1 >= USAGE_LEDGER_BOUNDARY_DIGEST_BYTES) {
|
|
365
385
|
committedTail.reset();
|
|
@@ -371,7 +391,7 @@ export async function scanUsageLedgerCooperatively(
|
|
|
371
391
|
if (droppingOversizedLine) {
|
|
372
392
|
droppingOversizedLine = false;
|
|
373
393
|
} else {
|
|
374
|
-
publishLine();
|
|
394
|
+
publishLine(rowStartBytes);
|
|
375
395
|
}
|
|
376
396
|
lineLength = 0;
|
|
377
397
|
cursor = newline + 1;
|
|
@@ -447,3 +467,224 @@ export async function scanUsageLedgerCooperatively(
|
|
|
447
467
|
closeSync(fd);
|
|
448
468
|
}
|
|
449
469
|
}
|
|
470
|
+
|
|
471
|
+
export interface ReadUsageLedgerRowsOptions {
|
|
472
|
+
/** Ascending, distinct row-start offsets reported by an earlier scan of the same file. */
|
|
473
|
+
offsets: readonly number[];
|
|
474
|
+
/** Identity those offsets were captured under; a replaced file invalidates all of them. */
|
|
475
|
+
expectedIdentityKey: string;
|
|
476
|
+
/** Revision the offsets were captured under. Any movement re-validates the boundary. */
|
|
477
|
+
expectedRevisionKey?: string;
|
|
478
|
+
/** Snapshot boundary the offsets belong to; every offset must fall before it. */
|
|
479
|
+
expectedProcessedThroughBytes?: number;
|
|
480
|
+
/** Trailing digest at that boundary, verified when the revision moved. */
|
|
481
|
+
expectedProcessedThroughDigest?: string;
|
|
482
|
+
/**
|
|
483
|
+
* Physical file size when the snapshot was published. A torn suffix makes this larger than
|
|
484
|
+
* the processed boundary, so growth is measured against it rather than the boundary.
|
|
485
|
+
*/
|
|
486
|
+
expectedFileSize?: number;
|
|
487
|
+
signal?: AbortSignal;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
export interface UsageLedgerRowRead {
|
|
491
|
+
entries: PersistedUsageEntry[];
|
|
492
|
+
/** Offsets that no longer frame a complete, normalizable row. */
|
|
493
|
+
unreadableOffsets: number[];
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Re-reads only the rows a derived index selected, addressed by their recorded byte offsets.
|
|
498
|
+
*
|
|
499
|
+
* This is the read half of the offset index: the caller already knows which rows match, so a
|
|
500
|
+
* filtered query pays to decode and normalize its candidates instead of the whole ledger. Every
|
|
501
|
+
* bound still belongs to this scanner -- the 1 MiB row ceiling, the opened-file identity, and the
|
|
502
|
+
* refusal to publish a torn final row -- because an offset reader with its own limits would be a
|
|
503
|
+
* second storage policy.
|
|
504
|
+
*
|
|
505
|
+
* A row that no longer parses at its recorded offset means the index and the ledger disagree;
|
|
506
|
+
* the caller is expected to drop the index and rebuild rather than serve a partial answer.
|
|
507
|
+
*/
|
|
508
|
+
export async function readUsageLedgerRowsAtOffsets(
|
|
509
|
+
options: ReadUsageLedgerRowsOptions,
|
|
510
|
+
): Promise<UsageLedgerRowRead> {
|
|
511
|
+
throwIfAborted(options.signal);
|
|
512
|
+
const path = usageLogPath();
|
|
513
|
+
let fd: number;
|
|
514
|
+
try {
|
|
515
|
+
fd = openSync(path, "r");
|
|
516
|
+
} catch (error) {
|
|
517
|
+
if (isMissingFileError(error)) {
|
|
518
|
+
// An empty selection against a missing ledger is an empty installation. A non-empty
|
|
519
|
+
// selection against one means the projection outlived its source.
|
|
520
|
+
if (options.offsets.length === 0
|
|
521
|
+
&& options.expectedIdentityKey === usageLogIdentityKey(null)) {
|
|
522
|
+
return { entries: [], unreadableOffsets: [] };
|
|
523
|
+
}
|
|
524
|
+
throw rebuildRequired("identity_mismatch");
|
|
525
|
+
}
|
|
526
|
+
throw error;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
530
|
+
const window = Buffer.allocUnsafe(USAGE_LEDGER_ROW_READ_WINDOW_BYTES);
|
|
531
|
+
// One byte past the ceiling: a row of exactly USAGE_LEDGER_MAX_LINE_BYTES is accepted by the
|
|
532
|
+
// scanner, so its terminating LF has to fit too.
|
|
533
|
+
const row = Buffer.allocUnsafe(USAGE_LEDGER_MAX_LINE_BYTES + 1);
|
|
534
|
+
let windowStart = -1;
|
|
535
|
+
let windowLength = 0;
|
|
536
|
+
|
|
537
|
+
try {
|
|
538
|
+
const openedRevision = revisionFromStat(path, fd, fstatSync(fd));
|
|
539
|
+
if (usageLogIdentityKey(openedRevision) !== options.expectedIdentityKey) {
|
|
540
|
+
throw rebuildRequired("identity_mismatch");
|
|
541
|
+
}
|
|
542
|
+
const size = openedRevision.size;
|
|
543
|
+
const boundary = options.expectedProcessedThroughBytes;
|
|
544
|
+
if (boundary !== undefined) {
|
|
545
|
+
if (size < boundary) throw rebuildRequired("shrink");
|
|
546
|
+
for (const offset of options.offsets) {
|
|
547
|
+
// An offset at or past the boundary belongs to a different snapshot than the one the
|
|
548
|
+
// caller validated.
|
|
549
|
+
if (offset < 0 || offset >= boundary) throw rebuildRequired("content_changed");
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
// The revision moved since the index snapshot: verify the boundary the offsets were
|
|
553
|
+
// captured at before trusting any of them. A same-size rewrite of that tail is caught
|
|
554
|
+
// here; an append past the boundary leaves it unchanged.
|
|
555
|
+
const revisionMoved = options.expectedRevisionKey !== undefined
|
|
556
|
+
&& usageLogRevisionKey(openedRevision) !== options.expectedRevisionKey;
|
|
557
|
+
const physical = options.expectedFileSize ?? boundary;
|
|
558
|
+
if (revisionMoved) {
|
|
559
|
+
if (boundary === undefined || options.expectedProcessedThroughDigest === undefined) {
|
|
560
|
+
throw rebuildRequired("content_changed");
|
|
561
|
+
}
|
|
562
|
+
// Growth is measured against the snapshot's physical size, not the processed boundary: a
|
|
563
|
+
// torn suffix leaves the file larger than the boundary without being new content. A file
|
|
564
|
+
// that did not grow cannot be an append, so a moved revision is an in-place rewrite and
|
|
565
|
+
// the trailing digest cannot see one earlier than its 64 KiB window.
|
|
566
|
+
if (physical === undefined || size <= physical) throw rebuildRequired("content_changed");
|
|
567
|
+
const digest = digestRangeSync(
|
|
568
|
+
fd,
|
|
569
|
+
Math.max(0, boundary - USAGE_LEDGER_BOUNDARY_DIGEST_BYTES),
|
|
570
|
+
boundary,
|
|
571
|
+
row,
|
|
572
|
+
);
|
|
573
|
+
if (digest !== options.expectedProcessedThroughDigest) {
|
|
574
|
+
throw rebuildRequired("content_changed");
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const lineAt = (offset: number): Buffer | null => {
|
|
579
|
+
if (offset >= windowStart && offset < windowStart + windowLength) {
|
|
580
|
+
const from = offset - windowStart;
|
|
581
|
+
// Search only the bytes actually read: the rest of the buffer is uninitialized.
|
|
582
|
+
const newline = window.subarray(from, windowLength).indexOf(0x0a);
|
|
583
|
+
if (newline >= 0) return window.subarray(from, from + newline);
|
|
584
|
+
// The cached window ends without a newline; a torn suffix has no complete row.
|
|
585
|
+
if (windowStart + windowLength >= size) return null;
|
|
586
|
+
}
|
|
587
|
+
const requested = Math.min(window.byteLength, size - offset);
|
|
588
|
+
if (requested <= 0) return null;
|
|
589
|
+
const read = readSync(fd, window, 0, requested, offset);
|
|
590
|
+
if (read <= 0) return null;
|
|
591
|
+
windowStart = offset;
|
|
592
|
+
windowLength = read;
|
|
593
|
+
const newline = window.subarray(0, read).indexOf(0x0a);
|
|
594
|
+
if (newline >= 0) return window.subarray(0, newline);
|
|
595
|
+
// The row spans more than one window: copy the prefix and grow up to the row ceiling.
|
|
596
|
+
window.copy(row, 0, 0, read);
|
|
597
|
+
let length = read;
|
|
598
|
+
while (length <= USAGE_LEDGER_MAX_LINE_BYTES) {
|
|
599
|
+
const want = Math.min(row.byteLength - length, size - offset - length);
|
|
600
|
+
if (want <= 0) return null;
|
|
601
|
+
const more = readSync(fd, row, length, want, offset + length);
|
|
602
|
+
if (more <= 0) return null;
|
|
603
|
+
const found = row.subarray(length, length + more).indexOf(0x0a);
|
|
604
|
+
if (found >= 0) {
|
|
605
|
+
windowStart = -1;
|
|
606
|
+
windowLength = 0;
|
|
607
|
+
return row.subarray(0, length + found);
|
|
608
|
+
}
|
|
609
|
+
length += more;
|
|
610
|
+
}
|
|
611
|
+
return null;
|
|
612
|
+
};
|
|
613
|
+
|
|
614
|
+
const walk = (visit: (bytes: Buffer, offset: number) => boolean): number[] => {
|
|
615
|
+
const unreadableOffsets: number[] = [];
|
|
616
|
+
for (const offset of options.offsets) {
|
|
617
|
+
throwIfAborted(options.signal);
|
|
618
|
+
if (!Number.isSafeInteger(offset) || offset < 0 || offset >= size) {
|
|
619
|
+
unreadableOffsets.push(offset);
|
|
620
|
+
continue;
|
|
621
|
+
}
|
|
622
|
+
const bytes = lineAt(offset);
|
|
623
|
+
if (!bytes) {
|
|
624
|
+
unreadableOffsets.push(offset);
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
if (!visit(bytes, offset)) unreadableOffsets.push(offset);
|
|
628
|
+
}
|
|
629
|
+
return unreadableOffsets;
|
|
630
|
+
};
|
|
631
|
+
|
|
632
|
+
const entries: PersistedUsageEntry[] = [];
|
|
633
|
+
const captured = createHash("sha256");
|
|
634
|
+
const unreadableOffsets = walk((bytes, offset) => {
|
|
635
|
+
captured.update(bytes);
|
|
636
|
+
captured.update("\0");
|
|
637
|
+
captured.update(String(offset));
|
|
638
|
+
let entry: PersistedUsageEntry | undefined;
|
|
639
|
+
try {
|
|
640
|
+
entry = normalizePersistedUsageRow(JSON.parse(decoder.decode(bytes)));
|
|
641
|
+
} catch {
|
|
642
|
+
entry = undefined;
|
|
643
|
+
}
|
|
644
|
+
// The index only holds rows the scanner normalized. A candidate that no longer parses or
|
|
645
|
+
// normalizes means the ledger moved under the projection, so it is unreadable rather than
|
|
646
|
+
// silently dropped: the caller rebuilds instead of reporting a smaller complete result.
|
|
647
|
+
if (!entry) return false;
|
|
648
|
+
entries.push(entry);
|
|
649
|
+
return true;
|
|
650
|
+
});
|
|
651
|
+
const capturedDigest = captured.digest("hex");
|
|
652
|
+
|
|
653
|
+
throwIfAborted(options.signal);
|
|
654
|
+
const endingRevision = revisionFromStat(path, fd, fstatSync(fd));
|
|
655
|
+
if (usageLogIdentityKey(endingRevision) !== options.expectedIdentityKey) {
|
|
656
|
+
throw rebuildRequired("identity_mismatch");
|
|
657
|
+
}
|
|
658
|
+
if (endingRevision.size < size) throw rebuildRequired("shrink");
|
|
659
|
+
const pathRevision = currentUsageLogRevision();
|
|
660
|
+
if (!pathRevision || usageLogIdentityKey(pathRevision) !== options.expectedIdentityKey) {
|
|
661
|
+
throw rebuildRequired("identity_mismatch");
|
|
662
|
+
}
|
|
663
|
+
// Any write during the read can be an in-place rewrite, not just an append. Re-read the
|
|
664
|
+
// same offsets and compare, so a rewrite that lands between two batches cannot publish a
|
|
665
|
+
// mixture of old and new rows. A pure append leaves every selected row unchanged.
|
|
666
|
+
const mutated = usageLogRevisionKey(endingRevision) !== usageLogRevisionKey(openedRevision)
|
|
667
|
+
|| usageLogRevisionKey(pathRevision) !== usageLogRevisionKey(openedRevision);
|
|
668
|
+
if (mutated) {
|
|
669
|
+
// A write that did not grow the file is an in-place rewrite. Re-reading the selected rows
|
|
670
|
+
// cannot see a rewrite that turns a non-candidate into a match, so this must rebuild.
|
|
671
|
+
if (physical !== undefined && endingRevision.size <= physical) {
|
|
672
|
+
throw rebuildRequired("content_changed");
|
|
673
|
+
}
|
|
674
|
+
const verified = createHash("sha256");
|
|
675
|
+
const recheck = walk((bytes, offset) => {
|
|
676
|
+
verified.update(bytes);
|
|
677
|
+
verified.update("\0");
|
|
678
|
+
verified.update(String(offset));
|
|
679
|
+
return true;
|
|
680
|
+
});
|
|
681
|
+
if (recheck.length !== unreadableOffsets.length
|
|
682
|
+
|| verified.digest("hex") !== capturedDigest) {
|
|
683
|
+
throw rebuildRequired("content_changed");
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
return { entries, unreadableOffsets };
|
|
687
|
+
} finally {
|
|
688
|
+
closeSync(fd);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
@@ -4,6 +4,13 @@ export const USAGE_SOURCE_CLIENTS = [
|
|
|
4
4
|
"all", "codex", "claude_code", "claude_desktop", "grok", "opencode", "aside", "other", "unknown",
|
|
5
5
|
] as const;
|
|
6
6
|
export type UsageSourceClient = (typeof USAGE_SOURCE_CLIENTS)[number];
|
|
7
|
+
/**
|
|
8
|
+
* The client labels a `codex` source filter includes, beyond legacy Codex-surface rows.
|
|
9
|
+
*
|
|
10
|
+
* The SQL candidate index derives its `IN (...)` list from this roster, so adding a Codex
|
|
11
|
+
* client here cannot silently narrow a filtered usage query.
|
|
12
|
+
*/
|
|
13
|
+
export const USAGE_CODEX_CLIENT_LABELS = ["codex_cli", "codex_desktop", "codex_ide"] as const;
|
|
7
14
|
export type UsageDeviceFilter = { kind: "all" | "no-key" | "unconfigured" } | { kind: "key"; id: string };
|
|
8
15
|
export interface UsageSourceFilter { client: UsageSourceClient; device: UsageDeviceFilter }
|
|
9
16
|
|
|
@@ -33,8 +40,9 @@ export function matchesUsageSource(entry: PersistedUsageEntry, filter: UsageSour
|
|
|
33
40
|
|| !entry.apiKeyId || configuredIds.has(entry.apiKeyId))) return false;
|
|
34
41
|
if (filter.client === "all") return true;
|
|
35
42
|
if (filter.client === "codex") {
|
|
36
|
-
return isLegacyCodexUsage(entry)
|
|
37
|
-
|| entry.client
|
|
43
|
+
return isLegacyCodexUsage(entry)
|
|
44
|
+
|| (entry.client !== undefined
|
|
45
|
+
&& (USAGE_CODEX_CLIENT_LABELS as readonly string[]).includes(entry.client));
|
|
38
46
|
}
|
|
39
47
|
if (filter.client === "unknown") return entry.client === "unknown" || (entry.client === undefined && !isLegacyCodexUsage(entry));
|
|
40
48
|
return entry.client === filter.client;
|
package/src/usage/summary.ts
CHANGED
|
@@ -807,6 +807,13 @@ export interface UsageSummaryAccumulator {
|
|
|
807
807
|
add(entry: PersistedUsageEntry): void;
|
|
808
808
|
/** Return a mutation-independent snapshot that may continue accepting rows. */
|
|
809
809
|
clone(): UsageSummaryAccumulator;
|
|
810
|
+
/**
|
|
811
|
+
* Widen the reported ledger span with rows this accumulator was never fed.
|
|
812
|
+
*
|
|
813
|
+
* A filtered query backed by a candidate index only reads the rows that match, so the span
|
|
814
|
+
* it would derive from those rows is narrower than the ledger it describes.
|
|
815
|
+
*/
|
|
816
|
+
noteLedgerSpan(start: number | null, end: number | null): void;
|
|
810
817
|
summarize(
|
|
811
818
|
range: UsageRange,
|
|
812
819
|
now: number,
|
|
@@ -1344,6 +1351,15 @@ class StreamingUsageSummaryAccumulator implements UsageSummaryAccumulator {
|
|
|
1344
1351
|
return { start: this.snapshotStart, end: this.snapshotEnd };
|
|
1345
1352
|
}
|
|
1346
1353
|
|
|
1354
|
+
noteLedgerSpan(start: number | null, end: number | null): void {
|
|
1355
|
+
if (start !== null && Number.isFinite(start)) {
|
|
1356
|
+
this.snapshotStart = this.snapshotStart === null ? start : Math.min(this.snapshotStart, start);
|
|
1357
|
+
}
|
|
1358
|
+
if (end !== null && Number.isFinite(end)) {
|
|
1359
|
+
this.snapshotEnd = this.snapshotEnd === null ? end : Math.max(this.snapshotEnd, end);
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1347
1363
|
get estimatedBytes(): number {
|
|
1348
1364
|
return this.estimatedRetainedBytes;
|
|
1349
1365
|
}
|