Skip to content

fix: make agent cost accounting updates transactional#405

Open
sahu-virendra-1908 wants to merge 2 commits into
LF-Decentralized-Trust-labs:mainfrom
sahu-virendra-1908:fix/agent-cost-accounting-race-condition
Open

fix: make agent cost accounting updates transactional#405
sahu-virendra-1908 wants to merge 2 commits into
LF-Decentralized-Trust-labs:mainfrom
sahu-virendra-1908:fix/agent-cost-accounting-race-condition

Conversation

@sahu-virendra-1908

Copy link
Copy Markdown

Summary

This PR fixes a race condition in agent cost accounting caused by non-atomic multi-table database updates in server/src/core/heartbeat.ts.

Previously, updates to:

  • agentRuntimeState
  • costEvents
  • agents.spentMonthlyCents

were executed as separate database operations outside a transaction. Under concurrent agent execution or process crashes, this could produce inconsistent financial state and allow budget enforcement to read stale spend values.

This change wraps all related accounting operations inside a single database transaction to ensure atomicity and consistency.


Related Issue

Fixes #404


Type of Change

  • fix
  • feat
  • docs
  • refactor
  • perf
  • test
  • build
  • breaking
  • chore

How Was This Tested?

  • Local run
  • Unit tests
  • Integration tests
  • Not tested (explain why)

Notes:

  • Verified successful local server startup after changes.
  • Verified transaction syntax and successful runtime execution.
  • Confirmed no build/runtime errors introduced by transaction wrapper.

CE & Security Check

  • Targets GitMesh CE only (no EE code)
  • No secrets or credentials committed

Screenshots / Demos (if UI or UX)

N/A — backend concurrency and transactional consistency fix.


Checklist

  • Code follows project style
  • Self-reviewed
  • Tests updated/added where needed

@sahu-virendra-1908

Copy link
Copy Markdown
Author

@Ronit-Raj9 @Parvm1102 @vibhor-5
Please review this PR.
This PR fixes a race condition in agent cost accounting caused by non-atomic multi-table updates in server/src/core/heartbeat.ts, which could lead to inconsistent financial state and potential budget enforcement bypass under concurrent runs.

If any improvements, corrections, or additional changes are needed, please let me know and I will update the PR accordingly.

Signed-off-by: sahu-virendra-1908 <virendrashivsahu@gmail.com>
Signed-off-by: sahu-virendra-1908 <virendrashivsahu@gmail.com>
@sahu-virendra-1908
sahu-virendra-1908 force-pushed the fix/agent-cost-accounting-race-condition branch from d64b81f to e2398a7 Compare May 28, 2026 09:52

@vibhor-5 vibhor-5 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

lgtm

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

critical race condition in agent cost accounting allows budget enforcement bypass

2 participants