Skip to content

Daemon/Usage: normalize token values and clamp invalid counters#32560

Open
zwright8 wants to merge 3 commits intoopenclaw:mainfrom
zwright8:codex/pr26712-token-normalization-fixes
Open

Daemon/Usage: normalize token values and clamp invalid counters#32560
zwright8 wants to merge 3 commits intoopenclaw:mainfrom
zwright8:codex/pr26712-token-normalization-fixes

Conversation

@zwright8
Copy link
Copy Markdown

@zwright8 zwright8 commented Mar 3, 2026

Summary

Validation

  • pnpm exec vitest run src/daemon/service-audit.test.ts src/agents/usage.normalization.test.ts

Context

This PR is one focused slice extracted from:

@openclaw-barnacle openclaw-barnacle bot added gateway Gateway runtime agents Agent runtime and tooling size: S labels Mar 3, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 3, 2026

Greptile Summary

This PR makes two targeted fixes: (1) it normalizes gateway service token values by trimming whitespace and stripping surrounding quotes before drift comparisons, preventing false gatewayTokenMismatch / gatewayTokenDrift issues caused by shell-quoted env values; (2) it silently discards negative token counters in normalizeUsage by extending asFiniteNumber to return undefined for any value below zero.

Changes:

  • src/daemon/service-audit.ts: New normalizeGatewayToken helper applied to both auditGatewayToken and checkTokenDrift — handles double-quoted, single-quoted, and whitespace-padded token strings correctly.
  • src/agents/usage.ts: asFiniteNumber now returns undefined for negative values instead of passing them through; the existing per-field clamping expression for input (line 102) is now dead code and the accompanying "Clamp to 0" comment is stale.
  • Tests for both changes are present and thorough.

Confidence Score: 4/5

  • Safe to merge — both fixes are correct and well-tested; one minor dead-code/stale-comment issue in usage.ts.
  • Logic is sound in both changed areas. The only nit is that the clamping expression rawInput !== undefined && rawInput < 0 ? 0 : rawInput and its associated comment in usage.ts are now unreachable dead code, but this doesn't affect correctness. All new behaviour is covered by the added tests.
  • src/agents/usage.ts — dead clamping expression and stale comment on line 102 should be cleaned up.

Last reviewed commit: 7a339b3

Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

4 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 3, 2026

Additional Comments (1)

src/agents/usage.ts
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:

  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.

Prompt To Fix With AI
This 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.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 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);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

@zwright8 zwright8 force-pushed the codex/pr26712-token-normalization-fixes branch from 7a339b3 to cd97848 Compare March 3, 2026 05:07
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 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".

Comment on lines +76 to +77
if (value < 0) {
return undefined;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling gateway Gateway runtime size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant