Skip to content
Draft
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
13 changes: 7 additions & 6 deletions src/routing/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,14 @@ function parseCapability(raw: unknown, caps: ParseCaps): RouteCapabilityEvidence
if (image !== undefined) out.image = image;
const structuredOutput = unknownable(raw.structuredOutput);
if (structuredOutput !== undefined) out.structuredOutput = structuredOutput;
if (Array.isArray(raw.reasoningEfforts)
&& raw.reasoningEfforts.slice(0, 8).every((value): value is string => typeof value === "string")) {
if (raw.reasoningEfforts.some((value: unknown) => typeof value === "string"
const reasoningEfforts = Array.isArray(raw.reasoningEfforts)
? raw.reasoningEfforts.slice(0, 8)
: undefined;
if (reasoningEfforts
&& reasoningEfforts.every((value): value is string => typeof value === "string")) {
if (reasoningEfforts.some((value: unknown) => typeof value === "string"
&& value.length > MAX_TRACE_STRING)) caps.strings = true;
out.reasoningEfforts = raw.reasoningEfforts
.slice(0, 8)
.map(value => value.slice(0, MAX_TRACE_STRING));
out.reasoningEfforts = reasoningEfforts.map(value => value.slice(0, MAX_TRACE_STRING));
Comment on lines +468 to +475

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

bun -e '
const sparse = new Array(8);
const mapped = sparse.map(value => value);

if (!sparse.every(value => typeof value === "string")) {
  throw new Error("every() did not skip sparse holes");
}
if (0 in mapped) {
  throw new Error("map() did not preserve sparse holes");
}
'

Repository: luvs01/opencodex

Length of output: 195


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '%s\n' '--- target file ---'
sed -n '430,500p' src/routing/trace.ts

printf '%s\n' '--- declarations and consumers ---'
rg -n -C 3 'reasoningEfforts|MAX_TRACE_STRING' src test tests 2>/dev/null || true

printf '%s\n' '--- runtime sparse-array semantics ---'
node - <<'JS'
const sparse = new Array(8);
const sliced = sparse.slice(0, 8);
const mapped = sliced.map(value => value);
const densified = Array.from(sliced);

console.log(JSON.stringify({
  every: sliced.every(value => typeof value === "string"),
  mappedHasIndex0: 0 in mapped,
  densifiedHasIndex0: 0 in densified,
  densifiedEvery: densified.every(value => typeof value === "string"),
}));
JS

Repository: luvs01/opencodex

Length of output: 50372


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '%s\n' '--- exact parser context ---'
sed -n '1,80p' src/routing/trace.ts
sed -n '455,485p' src/routing/trace.ts

printf '%s\n' '--- trace-related tests and parser callers ---'
rg -n -C 4 'parseCapability|reasoningEfforts|trace' tests src/routing/trace.ts \
  -g '*trace*' -g '*routing*' -g '*.test.ts' | head -n 240

printf '%s\n' '--- standalone sparse-array probe ---'
node - <<'JS'
const sparse = new Array(8);
const sliced = sparse.slice(0, 8);
const mapped = sliced.map(value => value);
const densified = Array.from(sliced);

console.log({
  everyAcceptsSparse: sliced.every(value => typeof value === "string"),
  mappedHasIndex0: 0 in mapped,
  densifiedHasIndex0: 0 in densified,
  densifiedEveryAccepts: densified.every(value => typeof value === "string"),
});
JS

Repository: luvs01/opencodex

Length of output: 21731


Reject sparse retained arrays before normalization.

At src/routing/trace.ts:468-475, every() skips holes and map() preserves them. A sparse array in the retained first eight entries therefore passes validation and remains a sparse string[]. Reject the holes with an indexed loop or densify the slice before validation. Add a sparse-array regression test.

Proposed localized fix
 const reasoningEfforts = Array.isArray(raw.reasoningEfforts)
-  ? raw.reasoningEfforts.slice(0, 8)
+  ? Array.from(raw.reasoningEfforts.slice(0, 8))
   : undefined;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const reasoningEfforts = Array.isArray(raw.reasoningEfforts)
? raw.reasoningEfforts.slice(0, 8)
: undefined;
if (reasoningEfforts
&& reasoningEfforts.every((value): value is string => typeof value === "string")) {
if (reasoningEfforts.some((value: unknown) => typeof value === "string"
&& value.length > MAX_TRACE_STRING)) caps.strings = true;
out.reasoningEfforts = raw.reasoningEfforts
.slice(0, 8)
.map(value => value.slice(0, MAX_TRACE_STRING));
out.reasoningEfforts = reasoningEfforts.map(value => value.slice(0, MAX_TRACE_STRING));
const reasoningEfforts = Array.isArray(raw.reasoningEfforts)
? Array.from(raw.reasoningEfforts.slice(0, 8))
: undefined;
if (reasoningEfforts
&& reasoningEfforts.every((value): value is string => typeof value === "string")) {
if (reasoningEfforts.some((value: unknown) => typeof value === "string"
&& value.length > MAX_TRACE_STRING)) caps.strings = true;
out.reasoningEfforts = reasoningEfforts.map(value => value.slice(0, MAX_TRACE_STRING));
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/routing/trace.ts` around lines 468 - 475, Update the reasoningEfforts
normalization in the trace parsing flow to reject sparse arrays within the
retained first eight entries, rather than relying on every() and map() which
skip and preserve holes. Use an indexed validation loop or densify the slice
before the existing string checks, and add a regression test covering a sparse
reasoningEfforts array.

}
if (raw.serviceTier === "unknown") {
out.serviceTier = "unknown";
Expand Down
27 changes: 27 additions & 0 deletions tests/route-decision-trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,33 @@ describe("route decision traces (RI-01)", () => {
expect(trace.candidates.every(candidate => candidate.exclusions.length === MAX_EXCLUSIONS_PER_CANDIDATE)).toBe(true);
});

test("normalization only inspects retained reasoning efforts", () => {
const reasoningEfforts = Array.from({ length: 1_000_000 }) as unknown[];
reasoningEfforts.fill("medium", 0, 8);
Object.defineProperty(reasoningEfforts, reasoningEfforts.length - 1, {
get: () => { throw new Error("reasoning effort outside the retained range was inspected"); },
});
const raw = {
version: 1,
decisionId: "abcdef012345",
createdAt: 1,
requestedModel: "a/m1",
routeKind: "policy",
requirements: [],
candidates: [{
provider: "a",
model: "m1",
eligible: true,
exclusions: [],
capability: { reasoningEfforts },
}],
selected: { candidateIndex: 0, provider: "a", model: "m1", reason: "policy" },
};

expect(normalizeRouteDecisionTrace(raw)?.candidates[0]?.capability?.reasoningEfforts)
.toEqual(Array.from({ length: 8 }, () => "medium"));
});

test("startup hydration reads trace-sized usage rows", () => {
const trace = oversizedTrace();
for (let index = 0; index < 20; index++) {
Expand Down
Loading