Skip to content

perf(conversations): Fence commits on caller-owned history cursors - #1228

Open
sentry-junior[bot] wants to merge 8 commits into
mainfrom
perf/conversation-event-append-cursor
Open

perf(conversations): Fence commits on caller-owned history cursors#1228
sentry-junior[bot] wants to merge 8 commits into
mainfrom
perf/conversation-event-append-cursor

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Stop durable agent-history checkpoints from reloading and deep-comparing the full active history on every write. Callers that already hold a committed projection can fence on that cursor and append only the delta.

Cursor-fenced commits

commitMessages accepts an optional base with committedSeq, historyVersion, messageSeqs, messages, and provenance. When present, it verifies the live cursor, then writes only new durable messages. Turn-session checkpoints pass the already-materialized session as that base and persist messageSeqs for the next fence.

Fence hardening

Session checkpoints only reuse a prior base when the next durable messages are a true prefix extension, so handoff/compaction replacements take the cold path. After handoff, resume materialization adopts the replacement epoch's fence cursors instead of keeping the pre-handoff committedSeq/messageSeqs. When the live store advanced past a stale base, commits adopt concurrent same-prefix agent writes (for example a racing turn_end before timeout continuation) and still reject divergent rewrites. Host-only cursor advances remain allowed.

Session lifecycle guard

History adoption alone is not enough: a delayed running checkpoint must not overwrite awaiting_resume/completed/failed session metadata that landed meanwhile. After the history commit, upsertAgentTurnSessionRecord re-reads the live session and rejects lifecycle regressions (and older same-rank slices). Completed delivery retries stay idempotent once a terminal record exists.

Separate appends

Native agent-history messages and host-only turn_context events are appended in separate store calls. Message sequence assignment no longer depends on mixed-event ordering.

Append result

append returns { historyVersion, inserted: [{ seq }], committedSeq } so commit paths advance without a post-write full history reload. Session summaries strip fence fields (messageSeqs, committedSeq, historyVersion) so strict summary readers keep working.

Cold paths without a base still load current history once for prefix validation. Coverage includes fenced deltas, host-only cursor advances, concurrent same-prefix adoption, delayed running-vs-awaiting_resume session races, handoff yield/resume, tool-timeout continuation, and product-path mid-turn checkpoints after MCP connect.

sentry-junior Bot and others added 2 commits August 4, 2026 17:18
Make append return the inserted delta and live history cursor so
commitMessages can advance committedSeq/messageSeqs without a second
loadCurrentHistory of the entire active version.

Co-Authored-By: David Cramer <david@sentry.io>
Co-Authored-By: David Cramer <david@sentry.io>
@vercel

vercel Bot commented Aug 4, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
junior-docs Ready Ready Preview Aug 4, 2026 9:45pm

Request Review

@dcramer
dcramer marked this pull request as ready for review August 4, 2026 18:20
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Aug 4, 2026
Separate message and turn-context appends, return committedSeq from
append, and let turn-session checkpoints pass an already-materialized
base so commitMessages only writes the delta after verifying the live
cursor.

Co-Authored-By: David Cramer <david@sentry.io>
@sentry-junior sentry-junior Bot changed the title perf(conversations): Avoid full history reload after append commits perf(conversations): Fence commits on caller-owned history cursors Aug 4, 2026
Comment thread packages/junior/src/chat/conversations/projection.ts
Comment thread packages/junior/src/chat/state/turn-session.ts
Exact global-cursor equality rejected checkpoints after MCP connects,
auth requests, and other non-agent events advanced seq. Fence on agent
message prefix and message seqs instead, and adopt the live cursor when
only host-only facts moved it.
Session summaries use a strict schema without fence fields. messageSeqs
was left on the summary payload, so index readers rejected every new
summary and hid sessions from operational listings.
Reject non-prefix session bases so handoff/compaction take the cold path,
adopt concurrent same-prefix agent commits past a stale fence, and cover the
product checkpoint/resume path with integration tests.

Co-Authored-By: David Cramer <david@sentry.io>
@github-actions github-actions Bot added risk: high PR risk score: high and removed risk: medium PR risk score: medium labels Aug 4, 2026
Comment thread packages/junior/src/chat/conversations/projection.ts
History fences can adopt concurrent same-prefix commits, but session
metadata must not regress. Re-check the live turn session after commit
and refuse delayed running checkpoints that would overwrite
awaiting_resume/completed/failed state.

Co-Authored-By: David Cramer <david@sentry.io>

@cursor cursor Bot 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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit ab58929. Configure here.

Comment thread packages/junior/src/chat/state/turn-session.ts
expectedVersion must prefer the stale caller base over a fresh store
read at upsert entry; otherwise delayed running checkpoints skip the
lifecycle guard and can still overwrite awaiting_resume.

Co-Authored-By: David Cramer <david@sentry.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants