Skip to content

fix(cost): snapshot estimatedCostUsd instead of accumulating (#69347)#69403

Merged
steipete merged 3 commits into
openclaw:mainfrom
MrMiaigi:fix/cost-snapshot-not-accumulate-69347
Apr 21, 2026
Merged

fix(cost): snapshot estimatedCostUsd instead of accumulating (#69347)#69403
steipete merged 3 commits into
openclaw:mainfrom
MrMiaigi:fix/cost-snapshot-not-accumulate-69347

Conversation

@MrMiaigi

Copy link
Copy Markdown
Contributor

The bug: three persist sites accumulated cost instead of snapshotting it like tokens. This caused cost to be inflated 1x-72x on multi-persist sessions because the same cumulative usage was added repeatedly.

Root cause: persistSessionUsageUpdate, updateSessionStoreAfterAgentRun, and the cron isolated-agent run path all used:
estimatedCostUsd = existingCost + runCost

But runCost was already computed from cumulative run usage, so this added the same cost repeatedly on redundant persists.

Fix: snapshot cost directly like tokens already do:
estimatedCostUsd = runCost

Files affected:

  • src/auto-reply/reply/session-usage.ts
  • src/agents/command/session-store.ts
  • src/cron/isolated-agent/run.ts

Tests added:

  • session-store.test.ts: verify cost is snapshotted, not accumulated
  • session.test.ts: updated existing test to verify snapshot behavior

Fixes #69347

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Apr 20, 2026
@greptile-apps

greptile-apps Bot commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR correctly fixes a cost-inflation bug where estimatedCostUsd was accumulated across every persist call instead of being snapshotted from the already-cumulative run usage. The fix is applied consistently across all three affected persist sites (session-usage.ts, session-store.ts, cron/isolated-agent/run.ts) and is well-tested.

  • P1 (syntax): session.test.ts line 2417 has a malformed comment / Before fix: (missing a /), which is a bare division expression and will cause a parse error in the test file.

Confidence Score: 4/5

Core fix is correct and well-tested, but the test file has a syntax error that will prevent it from compiling.

The production fix is solid. However, the malformed comment on line 2417 of session.test.ts (/ Before fix:) is a division expression, not a comment, and will cause a parse/compile error in the test suite.

src/auto-reply/reply/session.test.ts (line 2417 — malformed comment will break compilation)

Comments Outside Diff (1)

  1. src/auto-reply/reply/session-usage.ts, line 135 (link)

    P2 Dead variable left from the old accumulation logic

    existingEstimatedCostUsd is computed but never referenced after the fix — it was previously used in existingCost + runCost. It can be safely removed.

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: src/auto-reply/reply/session-usage.ts
    Line: 135
    
    Comment:
    **Dead variable left from the old accumulation logic**
    
    `existingEstimatedCostUsd` is computed but never referenced after the fix — it was previously used in `existingCost + runCost`. It can be safely removed.
    
    
    
    How can I resolve this? If you propose a fix, please make it concise.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/auto-reply/reply/session-usage.ts
Line: 135

Comment:
**Dead variable left from the old accumulation logic**

`existingEstimatedCostUsd` is computed but never referenced after the fix — it was previously used in `existingCost + runCost`. It can be safely removed.

```suggestion
```

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/auto-reply/reply/session.test.ts
Line: 2417

Comment:
**Malformed comment (missing leading slash)**

The line starts with `/ Before fix:` instead of `// Before fix:`, making it a division expression rather than a comment. This will cause a syntax/parse error.

```suggestion
    // Before fix: cost would accumulate to $0.0155 (2x)
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "fix(cost): snapshot estimatedCostUsd ins..." | Re-trigger Greptile

expect(stored1[sessionKey].estimatedCostUsd).toBeCloseTo(0.007725, 8);

// Second persist with SAME cumulative usage (e.g., heartbeat or redundant persist)
// Before fix: cost would accumulate to $0.0155 (2x)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Malformed comment (missing leading slash)

The line starts with / Before fix: instead of // Before fix:, making it a division expression rather than a comment. This will cause a syntax/parse error.

Suggested change
// Before fix: cost would accumulate to $0.0155 (2x)
// Before fix: cost would accumulate to $0.0155 (2x)
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/auto-reply/reply/session.test.ts
Line: 2417

Comment:
**Malformed comment (missing leading slash)**

The line starts with `/ Before fix:` instead of `// Before fix:`, making it a division expression rather than a comment. This will cause a syntax/parse error.

```suggestion
    // Before fix: cost would accumulate to $0.0155 (2x)
```

How can I resolve this? If you propose a fix, please make it concise.

@steipete

Copy link
Copy Markdown
Contributor

Maintainer pass: root cause and production fix look right.

I pushed 8df7230ed7 to fix the red checks:

  • removed the now-unused existingEstimatedCostUsd local after switching to snapshot semantics
  • fixed the new session-store.test.ts usage-format mock to use a typed provider models[] shape instead of unsafe property access

Local verification on the PR branch:

pnpm check:changed
pnpm test src/agents/command/session-store.test.ts src/auto-reply/reply/session.test.ts

Both pass locally. Waiting on GitHub CI for the updated branch.

MrMiaigi and others added 3 commits April 21, 2026 03:36
The bug: three persist sites accumulated cost instead of snapshotting
it like tokens. This caused cost to be inflated 1x-72x on multi-persist
sessions because the same cumulative usage was added repeatedly.

Root cause: persistSessionUsageUpdate, updateSessionStoreAfterAgentRun,
and the cron isolated-agent run path all used:
  estimatedCostUsd = existingCost + runCost

But runCost was already computed from cumulative run usage, so this
added the same cost repeatedly on redundant persists.

Fix: snapshot cost directly like tokens already do:
  estimatedCostUsd = runCost

Files affected:
- src/auto-reply/reply/session-usage.ts
- src/agents/command/session-store.ts
- src/cron/isolated-agent/run.ts

Tests added:
- session-store.test.ts: verify cost is snapshotted, not accumulated
- session.test.ts: updated existing test to verify snapshot behavior

Fixes #69347
@steipete
steipete merged commit 9efd2d1 into openclaw:main Apr 21, 2026
92 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Landed via temp rebase onto main.

  • Gate: pnpm check; pnpm test; pnpm build; post-rebase pnpm check:changed
  • Land commit: c2ddf75
  • Merge commit: 9efd2d1

Thanks @MrMiaigi!

loongfay pushed a commit to YuanbaoTeam/openclaw that referenced this pull request Apr 21, 2026
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
zhonghe0615 pushed a commit to zhonghe0615/openclaw that referenced this pull request May 7, 2026
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 9, 2026
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request May 13, 2026
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: sessions.json.estimatedCostUsd inflated 1x-72x by three persist sites that accumulate cost while snapshotting tokens

2 participants