Skip to content

AI-3515: list a carried-on session once, guard late closes - #2279

Merged
martinmitrevski merged 7 commits into
mainfrom
martinmitrevski/ai-3515-a-session-continued-on-another-shard
Nov 23, 2026
Merged

martinmitrevski merged 7 commits into
mainfrom
martinmitrevski/ai-3515-a-session-continued-on-another-shard

Conversation

@martinmitrevski

@martinmitrevski martinmitrevski commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

How it works

A session carried on in another shard still gets a row there, since that shard's turns need one. The older row is now marked superseded, and every list skips it in SQL.

a router in shard B carries on a session whose row is in shard A
│
├─ Upsert in B         opened_at = when B opened it, superseded_at = NULL
│                      (a write of a run opened before the row's changes nothing)
└─ Supersede           through the recorder's retry, safe to run twice
   ├─ every shard      read the session's rows, superseded or not
   ├─ one opened after B's run?   then this is a late write: mark B's own row
   │                              superseded by that run, and stop
   ├─ B's row          takes the pin, review score, last answer, modality and
   │                   remembers of the row opened last before it
   └─ every shard      SET superseded_at = GREATEST(superseded_at, B's opened_at)
                       WHERE opened_at < B's

Select, Search, Latest   WHERE superseded_at IS NULL: once, on every page
Get                      this shard's row, unless superseded or missing;
                         only then the other shards

superseded_at is when the run that superseded the row opened. A write opened before it keeps the mark.

A close only closes the run it belongs to, and a late write of an earlier run changes nothing:

sequenceDiagram
    participant R1 as Router 1
    participant R2 as Router 2
    participant PG as Postgres
    Note over R1: reopens the session at t1, its writes held up behind a slow one
    R2->>PG: carry on: Upsert, opened_at t2
    R1->>PG: Upsert, opened_at t1
    Note over PG: t1 < t2: the conflict update changes nothing
    R1->>PG: Close, opened t1: WHERE opened_at <= t1
    Note over PG: no row matches, the session stays open
    R2->>PG: Close, opened t2
    Note over PG: closed
Loading

Why

https://linear.app/stream/issue/AI-3515

Both items are real on main (ce41c39):

  • Duplicate. A text session that ended is carried on wherever the next message lands (reopenedFromRow, internal/api/sessions.go:739). On a router of another shard, the recorder upserts a new row there, without the pin, review score or last-answer time of the first. Get read this shard first (whicheverShard, internal/store/shards.go:130) and so answered the stale row from the shard the session left. pageOfSessions (internal/store/session.go:677) dropped the older row only within one page. A cursor put the two rows on different pages, so the list showed the session twice (["chat", "other", "chat"] in the new test run on main's behaviour). A filter on state or review_score could match only the old row (a session running in B listed as closed), and Latest (org activity) listed both.
  • Late close. Manager.Close releases the session in the directory (internal/session/manager.go:1700) before the closer stamps and queues the close (:1702). Another router can carry it on in between. The queued Sessions.Close (internal/store/session.go:356) then matched the reopened row by id and closed_at IS NULL, and closed the live session.

Since #2203 the recorder retries every write, and one failing write can hold its queue for up to 10 s. So a router's write of its own reopen can also land after another router carried the session on. The guards below hold for that too.

Why not one row. Continuing on the shard that holds the row, or moving the row, was the first choice, but neither fits the code:

  • Each shard's agent_responses, agent_branch_messages and agent_session_tools have a foreign key to that shard's agent_sessions (migrations/shard/20270110000000_baseline.sql:787-799). The shard writing the turns must hold a row.
  • Moving the row means deleting it from A, which cascades to every turn before the move.
  • Continuing on A would mean forwarding an ended session to a node of another shard (the directory tracks nodes by session, not by shard) or writing every turn across shards.

So the per-shard rows stay as they are. The superseded marker gives each session one listed row in SQL. This is a root fix, not more read-side merging: every page, filter and Latest sees one row.

Changes

Change Result Test
Shard migration 20270201000500: nullable opened_at, superseded_at on agent_sessions Metadata-only ADD COLUMN, 5 s lock timeout TestTheSchemaFilesAreCurrent, schema regenerated
sessionRow stamps OpenedAt with when this router opened the session; Upsert writes it A reopened row says which run holds it TestTheRowOfASessionCarriedOnIsOpenedWhenThisRouterOpenedIt
Upsert's conflict update has WHERE the write's opened_at is not before the row's (a write with no opened_at passes) A late write of an earlier run neither moves the row back nor reopens it TestALateReopenAndItsCloseLeaveTheRunOpenedSinceOpen
Upsert keeps a superseded_at later than the write's opened_at A late write in the shard the session left keeps the row out of lists TestALateReopenWrittenInTheShardASessionLeftKeepsItListedOnce
Sessions.Close(…, opened, at, …) adds opened_at IS NULL OR opened_at <= opened, and refuses a zero opened; the recorder and the session's closer pass the run's OpenedAt A late close from the earlier owner leaves the reopened row open TestACloseFromTheRouterThatHeldASessionBeforeItWasReopenedLeavesItOpen, TestACloseQueuedByTheRouterThatHeldASessionBeforeItWasReopenedLeavesItOpen, TestACloseThatDoesNotSayWhenItsRunOpenedIsRefused
New Sessions.Supersede, queued by the recorder after a carried-on row's Upsert (CarriedOn), through #2203's retry like every recorder write Older rows in every shard are marked superseded. The new row takes pin, review score, last answer, modality (it only moves up) and remembers, so mayRemember still forgets on delete TestASessionCarriedOnInAnotherShardKeepsWhatItsRowHereSaid, TestAChatReopenedInAnotherShardIsListedByTheRowItRunsIn (through Manager.Create)
Supersede reads every row first, copies, and marks last with GREATEST; every write only moves one way A call that failed partway, retried, ends as one that did not TestASupersedeTriedAgainAfterAShardFailedTakesWhatTheEarlierRowSaid
Supersede only marks rows opened before; finding a run opened after its own, it marks its own row instead A late write from an earlier owner never hides the current row, and is not listed itself TestALateRowFromARouterThatHeldASessionEarlierLeavesItListedOnceByTheRunOpenedSince
narrowSessions (Select, Search) and Latest filter superseded_at IS NULL Listed once, on every page and under every filter TestASessionCarriedOnInAnotherShardIsListedOnceOnEveryPageWithItsPin
Get reads this shard's row, and the other shards only when that row is superseded or missing Read from the shard it left, a session shows as it is now; a shard that is down fails no read of a session it does not hold TestASessionCarriedOnInAnotherShardIsFoundAsItIsThereFromTheShardItLeft, TestASessionHereIsFoundWhileAnotherShardIsDown, TestASessionCarriedOnInAShardThatIsDownIsFoundAsItLeftHere

SQL

BeforeAfter
-- Upsert
INSERT ... ON CONFLICT (id) DO UPDATE
SET ...
-- Select / Search (narrowSessions)
WHERE (customer_id = ?) ...
-- Latest
WHERE customer_id = app.id AND created_at >= ?
-- Close
WHERE id = ? AND customer_id = ?
  AND closed_at IS NULL
-- Get: this shard, then peers on a miss
WHERE id = ? AND customer_id = ? LIMIT 1
INSERT ... ON CONFLICT (id) DO UPDATE
SET ..., opened_at = EXCLUDED.opened_at,
  superseded_at = CASE WHEN asn.superseded_at
    > EXCLUDED.opened_at THEN asn.superseded_at END
WHERE EXCLUDED.opened_at IS NULL
  OR asn.opened_at IS NULL
  OR asn.opened_at <= EXCLUDED.opened_at

WHERE (customer_id = ?) ... AND (superseded_at IS NULL)

WHERE customer_id = app.id AND created_at >= ?
  AND superseded_at IS NULL

WHERE id = ? AND customer_id = ? AND closed_at IS NULL
  AND (opened_at IS NULL OR opened_at <= ?)

-- Get: this shard, then peers on a miss or a
-- superseded row
WHERE id = ? AND customer_id = ?

New statements, only when a session is carried on: one SELECT per shard by primary key, then one UPDATE of this shard's row by primary key and one UPDATE … SET superseded_at per shard by primary key when an earlier row was found (or one UPDATE of this shard's row when a later run was).

Plans were taken with EXPLAIN (ANALYZE, BUFFERS) on the store suite's seeded database (100,000 agent_sessions rows over 200 customers), local Postgres, warm cache, third run of each:

Statement Before After
Upsert Insert, arbiter agent_sessions_pkey, no conflict filter same arbiter, Conflict Filter on opened_at, shared hit=69
Select, default order Index Scan agent_sessions_last_message_idx, shared hit=29 same index, Filter: (superseded_at IS NULL), shared hit=29
Search call Bitmap Heap Scan, shared hit=509 same plan, shared hit=509
Latest, 2 apps Bitmap Index Scan agent_sessions_customer_idx, shared hit=16 same, shared hit=16
Close Index Scan agent_sessions_pkey, shared hit=63 same, shared hit=63
Supersede, read (new) Index Scan agent_sessions_pkey, shared hit=4
Supersede, copy (new) Index Scan agent_sessions_pkey, shared hit=63
Supersede, mark (new) Index Scan agent_sessions_pkey, shared hit=8
Get, per shard read Index Scan agent_sessions_pkey same

No plan or buffer count of an existing statement changed. Get reads one shard for a live session, as on main; TestSessionQueryBudgets holds it to one query. A carry-on makes one more primary-key SELECT and UPDATE per shard.

Plan before: Select, default order
Limit  (actual time=0.010..0.024 rows=26.00 loops=1)
  Buffers: shared hit=29
  ->  Index Scan using agent_sessions_last_message_idx on agent_sessions asn  (actual rows=26.00 loops=1)
        Index Cond: (customer_id = 'customer-7'::text)
        Buffers: shared hit=29
Execution Time: 0.126 ms
Plan after: Select, default order
Limit  (actual time=0.012..0.023 rows=26.00 loops=1)
  Buffers: shared hit=29
  ->  Index Scan using agent_sessions_last_message_idx on agent_sessions asn  (actual rows=26.00 loops=1)
        Index Cond: (customer_id = 'customer-7'::text)
        Filter: (superseded_at IS NULL)
        Buffers: shared hit=29
Execution Time: 0.047 ms
Plan before / after: Latest
before
Nested Loop  (actual time=0.031..0.046 rows=10.00 loops=1)
  Buffers: shared hit=16
  ->  Bitmap Heap Scan on agent_sessions (actual rows=5.00 loops=2)
        Recheck Cond: ((customer_id = app.id) AND (created_at >= '2026-02-28 10:00:00+00'))
        ->  Bitmap Index Scan on agent_sessions_customer_idx
after
Nested Loop  (actual time=0.074..0.092 rows=10.00 loops=1)
  Buffers: shared hit=16
  ->  Bitmap Heap Scan on agent_sessions (actual rows=5.00 loops=2)
        Recheck Cond: ((customer_id = app.id) AND (created_at >= '2026-02-28 10:00:00+00'))
        Filter: (superseded_at IS NULL)
        ->  Bitmap Index Scan on agent_sessions_customer_idx
Plan before / after: Close, and Supersede's three statements
Close before
Update on agent_sessions asn  Buffers: shared hit=63
  ->  Index Scan using agent_sessions_pkey  Filter: ((closed_at IS NULL) AND (customer_id = 'customer-100'))
Close after
Update on agent_sessions asn  Buffers: shared hit=63
  ->  Index Scan using agent_sessions_pkey  Filter: ((closed_at IS NULL) AND ((opened_at IS NULL) OR (opened_at <= '2026-03-01 10:00:00+00')) AND (customer_id = 'customer-100'))
Supersede, read (each shard)
Index Scan using agent_sessions_pkey on agent_sessions asn  Buffers: shared hit=4
  Index Cond: (id = 'seeded-session-8'::text)
  Filter: (customer_id = 'customer-8'::text)
Supersede, copy (this shard)
Update on agent_sessions asn  Buffers: shared hit=63
  ->  Index Scan using agent_sessions_pkey  Filter: (customer_id = 'customer-8')
Supersede, mark (each shard)
Update on agent_sessions asn  Buffers: shared hit=8
  ->  Index Scan using agent_sessions_pkey  Filter: (((opened_at IS NULL) OR (opened_at < '2026-03-01 10:00:00+00')) AND (customer_id = 'customer-8'))
Plan after: Upsert
Insert on agent_sessions asn
  Conflict Resolution: UPDATE
  Conflict Arbiter Indexes: agent_sessions_pkey
  Conflict Filter: ((excluded.opened_at IS NULL) OR (asn.opened_at IS NULL) OR (asn.opened_at <= excluded.opened_at))
  Conflicting Tuples: 1
  Buffers: shared hit=69

Not changed

  • Endings and the activity counts (CustomerActivity, CustomerConcurrencyPeak) still count a carried-on session once per shard. They are counts, not lists. Worth a follow-up ticket if those numbers matter.
  • Rollout: routers of the release before neither stamp opened_at nor touch superseded_at. While a multi-shard deployment rolls, a session carried back onto an old router keeps its superseded mark, and is listed by its other shard's row until a new router opens it again. A write with no opened_at passes the new guard, so an old router's late write or close stays unguarded until every router runs this release.
  • Until the carry-on's Supersede reaches the shard the session left, that shard's row is still listed and Get there answers it, as on main. The recorder writes it right after the row, retried for up to 10 s; a Supersede that gives up (a shard down for longer) is logged and leaves the session listed twice until it is carried on again.
  • While the shard a session was carried on in is down, Get from the shard it left answers its superseded row, as it was when it left, rather than failing. Reopening it there, or the phone call's session check, still sees the caller's own session.
  • An unpin landing between Supersede's read and its copy is undone by the copy's COALESCE: a window of one statement, at a carry-on.
  • A late write of an earlier run still writes that run's tool list to agent_session_tools, which only the session tools endpoint reads back.
  • No API, OpenAPI or SDK change.

Validation

Every new test was run against the code with its fix taken back out. Each failed for the reason it names:

Fix taken out Test Failure
All of it: main's Get, no list filter, no Supersede, no Close guard the four first ShardedStoreSuite tests above ["chat","other","chat"], no pin, state closed, no last answer, live row closed
List filter only …ListedOnceOnEveryPageWithItsPin ["chat","other","chat"]
Get reads its own shard only …FoundAsItIsThereFromTheShardItLeft state closed, end_reason left
Supersede does nothing …KeepsWhatItsRowHereSaid LastResponseAt nil
Recorder drops opened TestACloseQueuedBy… (RecordFlushSuite) its own router cannot close it
Store Close guard removed TestACloseQueuedBy… (RecordFlushSuite) live row closed
OpenedAt from created TestTheRowOfASessionCarriedOnIsOpenedWhenThisRouterOpenedIt wrong opened time
Supersede marking first and copying what the mark returned TestASupersedeTriedAgainAfterAShardFailedTakesWhatTheEarlierRowSaid no pin, remembers false
Upsert's conflict WHERE TestALateReopenAndItsCloseLeaveTheRunOpenedSinceOpen live run closed, opened_at back at t1, title late
Upsert's superseded_at CASE (cleared instead) TestALateReopenWrittenInTheShardASessionLeftKeepsItListedOnce ["chat","other","chat"]
Supersede's own-row mark TestALateRowFromARouter…ListedOnceByTheRunOpenedSince ["chat","other","chat"]
Get reading every shard at once TestASessionHereIsFoundWhileAnotherShardIsDown, TestASessionCarriedOnInAShardThatIsDownIsFoundAsItLeftHere error from the shard that is down
Close accepting a zero opened TestACloseThatDoesNotSayWhenItsRunOpenedIsRefused no error
Create queueing Opened for a reopen, or the condition inverted TestAChatReopenedInAnotherShardIsListedByTheRowItRunsIn (RecordedCreateSuite) no pin; closed row read from the shard it left

Run locally, from acceleration/, against a database and Redis of this branch's own:

Rebased onto main at 49e60f1; no migration newer than 20270201000500 there.

  • go build ./..., go vet (with and without -tags integration) on internal/store, internal/session, internal/api, internal/phone/..., cmd/router, and gofmt -l: clean
  • go test -tags integration -p 1 ./internal/session/ -count=1: ok (196 s)
  • go test -tags integration -p 1 ./internal/store/ -count=1: ShardedStoreSuite and every session, shard, query-budget and Sessions.* index test pass. What fails is not this branch's: seven price-change tests in StoreSuite and two campaign tests in DataMoveSuite fail the same way on main (49e60f1), and the index test's seeding of requests hit the driver's read timeout on the shared test server under other runs' load (it passed in an earlier run of this branch)
  • go test -tags integration -p 1 ./internal/api/ -run 'TestAppModeNamedCallSuite|TestCallStatusSuite|TestDeploymentNamedCallSuite|TestOrganizationActivitySuite|TestPhoneSuite|TestResumeHistorySuite|TestSessionCreateSuite|TestSessionDeleteSuite|TestSessionDeleteWithoutMemorySuite|TestSessionEndSuite|TestSessionListSuite|TestSessionReopenSettingsSuite|TestSessionVerbsSuite|TestSessionVoiceSuite|TestResponseBranchesSuite|TestScheduledTasksSuite': ok, 16 suites (one earlier run failed TestPhoneSuite at the suite level with no test named; it passed five times alone and in the next full run)
  • go test -tags integration -p 1 ./internal/phone/ ./internal/phone/outcome/ ./cmd/router/ -run 'TestOutcomeSuite|TestKeeperSuite|TestUsageCommandsSuite': ok

The other packages' integration suites run in CI once the PR is marked ready.

🤖 Generated with Claude Code

@martinmitrevski martinmitrevski left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewed #2279 at e4c7b31. CI on this head: GitGuardian and labeler passed, CodeQL Analyze still pending; the integration suites have not run in CI (draft).

Merge conflict: GitHub reports CONFLICTING. acceleration/internal/session/records.go conflicts with #2203 (7e3bbbaa4), which wraps every recorder write in attempt (retry for up to 10 s). Once rebased, Supersede runs inside attempt, so it has to be safe to run twice (see the comment on Supersede). The carried-on write also belongs in TestEveryWriteCutOffByADroppedConnectionIsStored.

Verdict: request changes. Two of these must land before merge: the unguarded conflict update in Upsert, which lets a late write from an earlier owner bring back both ticket bugs, and Get failing whenever any peer shard is down. The Supersede retry issue comes with the rebase. The missing wiring test and the Nit can follow.

Checked and sound:

  • Migration 20270201000500: it adds nullable columns with no default, so it only changes the catalog and does not rewrite the table. It uses the same 5 s lock_timeout as 20270127000200. The down migration drops only the two marker columns.
  • Close: a row with opened_at NULL (every row from before the deploy) still closes. The only production caller passes the run's OpenedAt, truncated to microseconds, which is the same value the row holds.
  • A→B→A: the Upsert in A clears A's mark. Supersede marks B's row, which opened earlier, and copies its fields back. Pin, unpin, review score, Describe, ChooseConnection and ClaimGuest write to every shard, so the two rows agree.
  • Session delete removes the row on every shard (onEveryShard). BranchMessages.DeleteUser and the forget paths select by user across all rows, superseded ones included. A sharded deployment refuses to be an export source (errShardedSource), so no export carries two rows with one id.
  • Get callers run once per request or per call end, never per turn or frame, and none of them loops.
  • go run ./cmd/openapi leaves openapi.yaml unchanged. No Huma operation changed.
  • Known gap beside the listed counts: CustomerTurns (store.go:544) sums COUNT(DISTINCT session_id) over the shards, so it also counts a carried-on session once per shard. This was already true on main.

Runs (from acceleration/, -tags integration -p 1, a database of this review's own): the PR's new store tests in TestStoreSuite and TestShardedStoreSuite all pass. I also ran four throwaway probes, then removed them; each one failed, confirming the findings inline.

Comment thread acceleration/internal/store/session.go Resolved
Comment thread acceleration/internal/store/session.go Resolved
Comment thread acceleration/internal/store/session.go Resolved
Comment thread acceleration/internal/store/session.go Resolved
@martinmitrevski
martinmitrevski force-pushed the martinmitrevski/ai-3515-a-session-continued-on-another-shard branch from e4c7b31 to b694a46 Compare November 23, 2026 20:44

@martinmitrevski martinmitrevski left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Re-reviewed at b694a464a, against my review of e4c7b31. The branch merges cleanly into current main (git merge-tree, no conflict); GitHub shows it as MERGEABLE. CodeQL, GitGuardian and labeler passed; the integration jobs were skipped.

All five findings are addressed:

  1. Late Upsert (dfb4fde35): fixed. A late same-shard write now changes nothing, so the earlier router's close leaves the live run open. A late write in the shard the session left keeps its superseded mark. A late insert is superseded by the newer run in another shard.
  2. Get with a peer down (0a015cc00): fixed. Get reads its own shard first and reads the other shards only when its own row is superseded or missing. The trade-off, a stale row returned while the shard the session moved to is down, is limited to reads: a reopen still fails in that state, because ActiveLeaf reads every shard.
  3. Supersede safe to retry (969dc3004): fixed. It now reads every earlier row, copies from them, and marks them last. Each write only moves one way, so running it twice ends the same.
  4. Wiring test (b694a464a): fixed. I inverted the Reopened check in Create and the new test failed on both assertions.
  5. Zero opened (ffdc85512): fixed; Close now refuses it. The refusal is not a transient error, so attempt does not retry it.

Rebase onto #2203: Close and Supersede now go through attempt. A retried Close is safe because of its closed_at IS NULL and opened_at guards; a retried Supersede is safe for the reasons in item 3.

Runs (-tags integration -p 1, a database of this review's own):

  • Every new and changed store test passes, as do TestAChatReopenedInAnotherShardIsListedByTheRowItRunsIn and the RecordFlushSuite retry tests.
  • My four earlier probes now pass. Two new probes also pass: a late insert followed by a retried Supersede, and A→B→A with retries (one listed row; running and pinned from both shards).

Notes, not blocking:

  • An unpin that lands between Supersede's read and its copy is still re-pinned by COALESCE.
  • The new Upsert guard compares router clocks. A carry-on by a router whose clock is behind by more than the previous run's length is now ignored completely, where before it only weakened the close guard.

Verdict: the findings are addressed, and I have no new ones.

@martinmitrevski
martinmitrevski marked this pull request as ready for review November 23, 2026 20:54
@martinmitrevski
martinmitrevski force-pushed the martinmitrevski/ai-3515-a-session-continued-on-another-shard branch from b694a46 to e08b01a Compare November 23, 2026 21:18
martinmitrevski added a commit that referenced this pull request Nov 23, 2026
#2279 (20270201000500) and #2276 (20270201000700) merge first, and goose
refuses a migration older than the newest one applied, so this branch's
migrations move above both: global 20270201001100 (phone_number_orders),
global 20270201001110 (dlc registration requests) and shard
20270201001120 (call_legs_transfer_idx). Nothing else names the versions;
the schema files are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
martinmitrevski and others added 7 commits November 23, 2026 22:39
A text session carried on in another shard gets a row there as well, which
the shard's turns need. Lists showed it twice across pages, the new row
lacked the pin, review score and last-answer time, and Get read the stale
row of the shard it left.

The row now records when its router opened it (opened_at). Carrying a
session on supersedes its rows opened before, in every shard, and copies
their pin, review score, last answer, modality and remembers onto the new
one. Select, Search and Latest skip superseded rows; Get reads every shard
and returns the one not superseded.

Sessions.Close takes when the closing router opened the session and closes
only a row not opened since, so a close queued by the router that held it
no longer closes the run another router carried on.

Refs AI-3515

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Supersede marked the earlier rows and copied only what that mark returned.
A call that failed after the mark committed in one shard (another shard
down, a dropped connection, the copy failing) left nothing to copy for
the retry the recorder now makes: the new row lost the pin, the review
score, the last answer and remembers, and with remembers lost, deleting
the session left that run's memories in the memory store.

It now reads the earlier rows, superseded or not, copies from them, and
marks them last, keeping the first mark. Run twice, it ends the same.

Refs AI-3515

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Upsert set opened_at and cleared superseded_at whatever the row held.
A router's write of its own reopen, held up behind a slow write, could
land after another router carried the session on. In the same shard it
moved opened_at back, so its queued close then closed the live run; in
the shard the session left it cleared the superseded mark, so the
session was listed twice.

The conflict update now changes nothing for a write opened before the
run the row holds (a write with no opened_at, from a router before this
release, still passes). superseded_at is when the run that superseded
the row opened, and a write opened before it keeps the mark. Supersede
marks this shard's own row when another shard holds a run opened since,
for a late write that inserted the row.

Refs AI-3515

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Get read every shard at once and failed when any one failed, so one
shard's outage broke reading, reopening and the phone call's session
check on every shard, the last answering 409 for the caller's own
session. It also sent every read to every shard.

Get reads this shard's row first and answers with it unless it is
superseded or missing; only then are the other shards read, as before
this branch. While the shard a session was carried on in is down, its
superseded row here answers, as it was when it left.

Refs AI-3515

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A zero opened matched only rows written before opened_at was kept, so a
caller that left it out closed nothing and was told nothing, while
Supersede refuses a zero time. The only caller passes the run's
OpenedAt, which is always set, and a row from before the column is
matched by its NULL whatever time is passed, so Close now refuses a zero
time too. The tests pass when their rows opened.

Refs AI-3515

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Only the branch in Create that queues CarriedOn for a reopened session
makes Supersede run, and no test went through it: inverting it, or
queueing Opened in both cases, passed the suite. The new test closes a
chat on a router of one shard, pins it, reopens it through Create on a
router of the other, and reads both: the row it runs in has the pin, and
read from the shard it left it is running.

Refs AI-3515

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#2258 added shard tests that call Sessions.Close without the time the
closing router opened the session, and a second carriedOn helper. Both
merged cleanly as text but did not compile: pass each row's open time
and name #2258's helper carriedOnAt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@martinmitrevski
martinmitrevski force-pushed the martinmitrevski/ai-3515-a-session-continued-on-another-shard branch from e08b01a to 8431575 Compare November 23, 2026 21:44
martinmitrevski added a commit that referenced this pull request Nov 23, 2026
#2279 (20270201000500) and #2276 (20270201000700) merge first, and goose
refuses a migration older than the newest one applied, so this branch's
migrations move above both: global 20270201001100 (phone_number_orders),
global 20270201001110 (dlc registration requests) and shard
20270201001120 (call_legs_transfer_idx). Nothing else names the versions;
the schema files are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@martinmitrevski
martinmitrevski merged commit 6d447f6 into main Nov 23, 2026
13 checks passed
@martinmitrevski
martinmitrevski deleted the martinmitrevski/ai-3515-a-session-continued-on-another-shard branch November 23, 2026 21:56
martinmitrevski added a commit that referenced this pull request Nov 23, 2026
#2279 and this branch both guard the session save's ON CONFLICT update:
this one to leave another customer's row alone, #2279 to let a late
write of an earlier run change nothing. Keep both conditions. When
nothing is written, look whether the row is the customer's own: a late
write of its own run is no error; another customer's id is ErrIDTaken.
Also pass the open time to the one Close call of this branch's tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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