Skip to content

fix(cloud-agent): serialize concurrent codex prompts to fix usage accounting#1721

Open
tatoalo wants to merge 1 commit intofix/codex-loadsession-task-runfrom
fix/codex-prompt-serialization
Open

fix(cloud-agent): serialize concurrent codex prompts to fix usage accounting#1721
tatoalo wants to merge 1 commit intofix/codex-loadsession-task-runfrom
fix/codex-prompt-serialization

Conversation

@tatoalo
Copy link
Copy Markdown
Contributor

@tatoalo tatoalo commented Apr 17, 2026

Problem

codex-acp and codex-rs serialize submissions at the conversation level, so concurrent prompts are safe from codex's perspective. But our adapter accumulates per-turn usage into sessionState.accumulatedUsage via the codex-client sessionUpdate handler, and each prompt() starts with resetUsage(). If two prompts run concurrently on the JS side — e.g. two concurrent /command POSTs (a Slack follow-up racing a web-UI follow-up, or a retry after an HTTP timeout) — the second prompt's resetUsage wipes the first's in-flight counters and both TURN_COMPLETE notifications report garbled totals.

Changes

Chain CodexAcpAgent.prompt calls through a FIFO promise mutex so the usage accumulator has a single owner per turn. Errors on one prompt do not block subsequent prompts (.catch(() => {}) on the previous tail).

How did you test this?

  • New unit test: two concurrent prompt() calls serialize — the second does not invoke codexConnection.prompt until the first resolves.
  • New unit test: a failing prompt does not block the next one.
  • Full @posthog/agent suite green (263 tests).

Copy link
Copy Markdown
Contributor Author

tatoalo commented Apr 17, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@tatoalo tatoalo force-pushed the fix/codex-prompt-serialization branch from 9e6b0fb to 0b35e19 Compare April 17, 2026 17:02
@tatoalo tatoalo marked this pull request as ready for review April 17, 2026 17:07
@tatoalo tatoalo requested a review from a team April 17, 2026 17:07
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.

1 participant