Daemon/Usage: normalize token values and clamp invalid counters#32560
Daemon/Usage: normalize token values and clamp invalid counters#32560zwright8 wants to merge 3 commits intoopenclaw:mainfrom
Conversation
Greptile SummaryThis PR makes two targeted fixes: (1) it normalizes gateway service token values by trimming whitespace and stripping surrounding quotes before drift comparisons, preventing false Changes:
Confidence Score: 4/5
Last reviewed commit: 7a339b3 |
Additional Comments (1)
Now that Consider removing the dead branch and updating the comment to reflect the new "return undefined for negatives" behaviour: And update (or remove) the comment block at lines 96–98 to match the actual semantics. Prompt To Fix With AIThis is a comment left during a code review.
Path: src/agents/usage.ts
Line: 96-102
Comment:
**Dead code and stale comment after `asFiniteNumber` change**
Now that `asFiniteNumber` returns `undefined` for any `value < 0`, `rawInput` can never be negative. The clamping expression on line 102 (`rawInput !== undefined && rawInput < 0 ? 0 : rawInput`) and the comment above it ("Clamp to 0") are both dead/misleading — the condition `rawInput < 0` will never be true.
Consider removing the dead branch and updating the comment to reflect the new "return undefined for negatives" behaviour:
```suggestion
const rawInput = asFiniteNumber(
raw.input ?? raw.inputTokens ?? raw.input_tokens ?? raw.promptTokens ?? raw.prompt_tokens,
);
const input = rawInput;
```
And update (or remove) the comment block at lines 96–98 to match the actual semantics.
How can I resolve this? If you propose a fix, please make it concise. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7a339b3d2d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| expectedGatewayToken?: string, | ||
| ) { | ||
| const expectedToken = expectedGatewayToken?.trim(); | ||
| const expectedToken = normalizeGatewayToken(expectedGatewayToken); |
There was a problem hiding this comment.
Compare configured gateway token without unquoting
Applying normalizeGatewayToken to expectedGatewayToken/configToken makes drift checks treat wrapped and unwrapped values as equivalent, so a real mismatch is missed when the configured token legitimately starts and ends with quote characters (for example "abc" vs abc). gateway.auth.token is defined as a free-form string (src/config/zod-schema.ts) and credential resolution only trims whitespace (src/gateway/credentials.ts), so quote characters are meaningful; suppressing gateway-token-mismatch/gateway-token-drift here can hide a restart-breaking auth mismatch.
Useful? React with 👍 / 👎.
7a339b3 to
cd97848
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cd97848f13
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (value < 0) { | ||
| return undefined; |
There was a problem hiding this comment.
Keep negative input counters clamped to zero
Filtering all negative numbers in asFiniteNumber makes the existing normalizeUsage clamp path for pre-subtracted prompt/input tokens unreachable, so inputs that previously normalized to 0 now become undefined. That changes established behavior for providers that emit negative prompt counters and conflicts with current expectations in src/agents/usage.test.ts:91 and src/agents/usage.test.ts:108, which means this commit will regress usage normalization semantics (and fail the default src/**/*.test.ts suite) unless the clamp behavior is preserved.
Useful? React with 👍 / 👎.
Summary
Validation
pnpm exec vitest run src/daemon/service-audit.test.ts src/agents/usage.normalization.test.tsContext
This PR is one focused slice extracted from: