Skip to content

fix(agent): seed hydrated transcripts with estimated context usage#3335

Closed
tatoalo wants to merge 2 commits into
mainfrom
posthog-code/seed-hydrated-transcript-usage
Closed

fix(agent): seed hydrated transcripts with estimated context usage#3335
tatoalo wants to merge 2 commits into
mainfrom
posthog-code/seed-hydrated-transcript-usage

Conversation

@tatoalo

@tatoalo tatoalo commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Problem

When a cold resume rebuilds the session transcript from S3 logs, every rebuilt message carries usage: 0. The resumed SDK session therefore believes its context is nearly empty: it cannot trigger auto-compaction proactively and only learns the real context size from the first API response — or from a "prompt is too long" rejection, at which point compaction itself is already impossible. This was visible in a real failure where the SDK reported a compact boundary of ~16k pre-tokens while the actual request weighed over 1.1M tokens.

Follow-up to #3331.

Changes

  • After building the hydrated JSONL, seed the last assistant message's usage.input_tokens with the estimated token count of the rebuilt conversation, so the SDK's context accounting starts near reality and auto-compaction can fire before the first API call when warranted.

How did you test this?

  • New test: only the last assistant entry carries the non-zero estimate; earlier entries stay at zero.
  • pnpm exec vitest run src/adapters/claude/session/jsonl-hydration.test.ts — 58 passed, 1 pre-existing failure (sanitizeSessionJsonl warn-path test) that also fails on main.
  • tsc --noEmit on the agent package.

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Created with PostHog Code

Hydrated session transcripts write usage: 0 on every message, so a
resumed SDK session believes its context is empty and cannot trigger
auto-compaction until the first API response comes back (or fails).
Seed the last assistant message with the estimated token count of the
rebuilt conversation so context accounting starts near reality.

Generated-By: PostHog Code
Task-Id: c6065620-5694-4478-a20a-fec4ccde8f1a
@trunk-io

trunk-io Bot commented Jul 10, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@github-actions

github-actions Bot commented Jul 10, 2026

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit 03f7aa9.

…d lines

Generated-By: PostHog Code
Task-Id: c6065620-5694-4478-a20a-fec4ccde8f1a

tatoalo commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

with the hydration budgets capped at ~half the window (#3331) and the workspace transcript mirror making cold hydration rare (#3333), the SDK learns its real context size from the first API response anyway, so seeding estimated usage adds little. The prompt-too-long fallback (#3334) covers the residual risk.

@tatoalo tatoalo closed this Jul 10, 2026
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