Skip to content

fix(opencode): clone part data in Bus event to preserve token values#15780

Merged
thdxr merged 1 commit intoanomalyco:devfrom
ryanskidmore:ryanskidmore/fix-token-counts-in-logs
Mar 3, 2026
Merged

fix(opencode): clone part data in Bus event to preserve token values#15780
thdxr merged 1 commit intoanomalyco:devfrom
ryanskidmore:ryanskidmore/fix-token-counts-in-logs

Conversation

@ryanskidmore
Copy link
Contributor

@ryanskidmore ryanskidmore commented Mar 3, 2026

Issue for this PR

Closes #15779

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Session.updatePart() passes the part object by reference into Bus.publish(PartUpdated) inside a Database.effect(). The fn() wrapper's Zod schema.parse() creates a new object from the input, and the deferred effect closure captures the original reference. If anything mutates that object between queue time and effect execution, JSONL subscribers receive stale data with zeroed token fields — while the TUI is unaffected since it reads from the DB post-persistence.

The bug is timing/provider-dependent: Database.effect() fires synchronously after Database.use() returns, so in most cases the reference is still valid. It manifests under specific async timing conditions depending on the provider.

The fix wraps the part in structuredClone(part) to snapshot the values at queue time, guaranteeing isolation regardless of timing.

How did you verify your code works?

  • bunx tsc --noEmit passes
  • bun test test/session/session.test.ts — 3 pass, 0 fail (includes new regression test with 10 assertions verifying non-zero tokens propagate through the Bus event)
  • Regression test also confirms the event part is a clone, not the same object reference
  • Verified bun dev --format json run produces non-zero tokens in step_finish JSONL output

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Mar 3, 2026
The PartUpdated Bus event was passing the original part object reference, which could be mutated by Zod parse before subscribers received it. Using structuredClone ensures JSONL step_finish events contain the correct non-zero token values. Includes regression test.
@ryanskidmore ryanskidmore force-pushed the ryanskidmore/fix-token-counts-in-logs branch from 3f7b23e to 92793dd Compare March 3, 2026 02:51
@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Mar 3, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Mar 3, 2026

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@thdxr thdxr merged commit fd6f713 into anomalyco:dev Mar 3, 2026
13 checks passed
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.

JSONL step_finish events emit zero tokens despite TUI showing correct values

2 participants