Skip to content

fix(session): the recorder retries a write that can pass and keeps others' items - #2203

Merged
thesyncim merged 6 commits into
mainfrom
fix/ai-3492-recorder-keeps-the-record
Nov 23, 2026
Merged

thesyncim merged 6 commits into
mainfrom
fix/ai-3492-recorder-keeps-the-record

Conversation

@thesyncim

@thesyncim thesyncim commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Problem
sessionRecorder.write and writeItems tried each write once. A session or response row lost to a dropped connection failed the foreign key of every later row of that conversation, and one item whose response was never stored failed the shared InsertMany for everybody. A lost session or call end left the row open for good.

Fix

  • attempt retries in place, keeping queue order, when store.Transient says the failure can pass: a failed dial, read or write, a deadline, class 08, 25006 (a demoted primary), 40001, 40P01, 53300, 57014 (statement_timeout or pg_cancel_backend; pgdriver sends no cancel, so a client deadline is a net timeout), 57P01-57P03. A local error such as a missing file is not. Backoff 50 ms to 1 s, 10 s per write; after one runs out, writes are tried once until one succeeds.
  • Retries rely on idempotent writes (closed_at IS NULL, ON CONFLICT).
  • An items batch refused with 23503 is written again per response; a one-turn batch is not resent.
  • Lost ends, ends a full queue dropped, and items of a turn never stored count in router_session_records_lost_total{write}.
  • Not done: a sweep closing a gone router's rows; no row records its router.

Tests
Each failed before the fix.

  • TestEveryWriteCutOffByADroppedConnectionIsStored: every write kind, a reopen's tools included, survives its backend being terminated.
  • TestAnEndQueuedWhileTheDatabaseIsBrieflyDownAfterAnOutageIsStored: a write that succeeds ends the try-once mode.
  • TestEndsThatCannotBeStoredAreCountedOnceTheRetriesRunOut: database down; one budget in all, an invalid end between included.
  • TestEndsAFullQueueDropsAreCounted.
  • TestAnItemWhoseResponseIsMissing... (two): stored items survive a missing turn's, in another session or the same; each dropped item counts.
  • TestTransientTellsAFailureAnotherTryCanPassFromOneItCannot (store): SQLSTATEs from a fake server, a silent server, a closed pool and local errors.

Fixes AI-3492

@thesyncim thesyncim changed the title fix(session): the recorder retries a write that can pass and keeps other sessions' items fix(session): the recorder retries a write that can pass and keeps others' items Nov 23, 2026
@thesyncim
thesyncim force-pushed the fix/ai-3492-recorder-keeps-the-record branch from 056e5fb to 1a77769 Compare November 23, 2026 13:46
@thesyncim
thesyncim marked this pull request as ready for review November 23, 2026 13:46
@thesyncim
thesyncim force-pushed the fix/ai-3492-recorder-keeps-the-record branch 2 times, most recently from ca87b58 to 2be3358 Compare November 23, 2026 14:07
@thesyncim thesyncim added the data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review label Nov 23, 2026
@thesyncim
thesyncim requested a review from kanat November 23, 2026 14:53

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed at 2be3358 merged onto main 1c87c73: the merge is clean, CI is green, and RecordFlushSuite plus the new store tests pass. -race -count=50 -cpu 1 on the order-dependent tests passes.

Checked and sound:

  • A retry after a commit whose answer was lost writes nothing twice: every recorder write is an ON CONFLICT insert, an update guarded by closed_at, finished_at or ended_at IS NULL, or an idempotent update. No write touches usage.
  • Queue order is kept.
  • A non-transient failure is tried once.
  • The 23503 split runs once per response and cannot loop.
  • The metric's labels are a fixed set of three.
  • Close still closes the queue first, so the send-on-closed window is the same as on main.
  • A write that succeeds on its first try behaves as on main. The only change on that path is one 5 s context per statement.

Verdict: NO-GO. Request changes for R1.1 and R1.2: rules the tests do not pin.

Comment thread acceleration/internal/store/postgres.go Resolved
Comment thread acceleration/internal/store/postgres.go Resolved
Comment thread acceleration/internal/store/postgres.go Resolved
@thesyncim thesyncim removed the data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review label Nov 23, 2026
@thesyncim
thesyncim force-pushed the fix/ai-3492-recorder-keeps-the-record branch from 2be3358 to bab0172 Compare November 23, 2026 16:41
@thesyncim
thesyncim requested a review from kanat November 23, 2026 16:43

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Delta round 2: reviewed bab0172 merged onto main eaaf409. The delta is one commit (the tests plus the narrowed store.Transient); the other four commits are identical in range-diff, and records.go is unchanged. Verdict: NO-GO, one Should fix left.

Prior findings:

  • R1.1 fixed. I re-ran my mutants on this head. Each write kind now fails its own subtest of TestEveryWriteCutOffByADroppedConnectionIsStored when it is reverted to one try (13 kinds, plus Sessions.Upsert through TestASessionWriteCut...). failing never cleared, failing cleared by a refused write, .Inc() per response, and uncounted full-queue session and call ends each fail a test. The one survivor is the per-response fallback InsertMany(own) tried once, which you named yourself; optional.
  • R1.2 fixed for the rows it listed: dropping any of 25006, 40001, 40P01, 53300, 57014, 57P01, 57P02, 57P03, the 08 class, or the timeout rule fails a row. See R2.1 for the rest of the rewritten branch.
  • R1.3, R1.4 fixed. The dead DeadlineExceeded and syscall checks are gone, and ENOENT and EACCES are rows.
  • R1.5, R1.6, R1.7 are AI-3606; fine as tickets.

New:

  • [Should fix] R2.1 on postgres_unit_test.go, see the inline comment.
  • [Nit] R2.2 on postgres.go, see the inline comment.

Checks: build, vet and gofmt are clean. RecordFlushSuite and the Transient table pass; -race -cpu 1 passes (-count=3 on the new session tests, -count=20 on the table). gh pr checks shows integration (api) red on TestAThreadTaskWhoseSessionEndsMidTurnSettles and TestAReplyNeverWrittenIsNoReply; nothing under internal/api changed here. I reproduced the first on main: run in one process after TestScheduledTasksSuite/TestAReplyNeverWrittenIsNoReply it fails 3 of 3 on main, and alone it passes 23 of 23 on this head. I did not find why CI's second test timed out, so please rerun the job.

Comment thread acceleration/internal/store/postgres.go Resolved

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Verdict: GO, re-graded under this repo's pr-review skill (no Blocker, and no Should fix that has to land before merge). R2.1 is a test-coverage gap on code that behaves correctly, so it is a follow-up: the ready test rows are in the earlier review. The Nits stay optional.

@thesyncim
thesyncim force-pushed the fix/ai-3492-recorder-keeps-the-record branch from bab0172 to 9b3b4cb Compare November 23, 2026 17:48
…her sessions' items

A write that fails for a dropped or refused connection, a timeout, a server
shutting down, or a serialization failure or deadlock is tried again in place,
in queue order, for up to 10 seconds. A batch of items refused for a missing
parent row is written again per session, and only the sessions whose row is
missing are dropped. Session and call ends that still cannot be stored, and
dropped items, are counted in router_session_records_lost_total.
…write is retried

A batch of items refused for a missing parent is written again per response,
the row the items' key names, so a stored turn's items are kept beside a
session's missing turn; a batch of one turn is not sent again. A write
refused with 25006 (read-only transaction, a demoted primary) or 57014
(statement cancelled) is retried, as a client-side timeout already was. A
session or call end dropped by a full queue is counted as lost, and a write
refused before it reached the database no longer marks the writer healthy.
…t counts

Each write kind is cut mid-statement by a terminated backend and must still be
stored, a reopen's tools included. A proxy outage longer than a budget, then a
write that gets through, must end the try-once mode; a write refused before it
reached the database must not. Two items of a missing turn count as two, and an
end a full queue drops is counted. store.Transient is tested against a fake
server's SQLSTATEs, a silent server, a closed pool and local errors; it now
retries a failed dial, read or write and a timeout, not every errno, and its
57014 comment says what sends one.
…anches

A wrapped driver.ErrBadConn, io.EOF, io.ErrUnexpectedEOF and
context.DeadlineExceeded, and a dial to a closed port, are each retried; the
57014 comment reads as a sentence.

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Verdict: GO at 9b3b4cb. The rebase leaves the earlier commits identical (range-diff). The new commit adds the R2.1 rows and fixes the R2.2 comment. All four mutants (dropping driver.ErrBadConn, io.EOF, io.ErrUnexpectedEOF or *net.OpError) now fail the Transient unit table.

@thesyncim
thesyncim force-pushed the fix/ai-3492-recorder-keeps-the-record branch from 9b3b4cb to a1e9cea Compare November 23, 2026 18:00
@thesyncim
thesyncim requested a review from kanat November 23, 2026 18:01
@thesyncim thesyncim added the data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review label Nov 23, 2026
@thesyncim
thesyncim merged commit 7e3bbba into main Nov 23, 2026
18 of 19 checks passed
@thesyncim
thesyncim deleted the fix/ai-3492-recorder-keeps-the-record branch November 23, 2026 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review docs project-info

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants