Skip to content

fix(store): a session's later writes name the app that owns it - #2197

Merged
thesyncim merged 4 commits into
mainfrom
fix/ai-3480-session-writes-name-owner
Nov 23, 2026
Merged

thesyncim merged 4 commits into
mainfrom
fix/ai-3480-session-writes-name-owner

Conversation

@thesyncim

@thesyncim thesyncim commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Problem
Writes that found a session's, response's, call's or connection's row by id alone changed it whoever asked. Ids are chosen by callers and agent_sessions and its neighbours are keyed on id for all apps, so a write could land on another app's row of the same id.

Fix
Each of these names its customer and matches on it:

  • Session writes and the recorder that makes them.
  • Responses.Finish and the stamp it writes on the session.
  • Call writes, recordings and manual task runs.
  • Connection client and validation, simulation runs, and campaign attempts through their campaign.
  • SessionTools.Upsert, which writes only beside a session of the customer's.
  • Responses.Insert, BranchSwitches.Insert and ConversationDocuments.Insert refuse another customer's id with the new ErrIDTaken.

For callers:

  • SessionTools.Upsert on a missing session returns ErrNoSession.
  • Calls.ChangeModels and SimulationRuns.Finish read the customer from the struct and refuse an empty one.
  • A same-customer retry of the three inserts is now an update, not a skipped insert.
  • A branch switch refused with ErrIDTaken is answered with a 409.
  • The recorder writes no items for a turn whose row was refused with ErrIDTaken.

Left for after #2177: ending a session whose opening write was refused (or reserving its id), with a test that two apps opening one id leave one session running.

Tests
Each failed with its rule taken out.

  • TestAnotherCustomersWriteToASessionIdChangesNothing: the four session writes.
  • TestAnotherCustomersToolsForASessionIdAreNotKept: tools are neither replaced nor created.
  • TestAnotherCustomersWriteToAResponseIdChangesNothing: response insert and finish.
  • TestAnotherCustomersFinishedTurnDoesNotMoveASessionsClock and TestAnotherCustomersFinishOfAnAppsResponseDoesNotMoveItsSessionsClock: the session stamp.
  • TestTheSameCustomerWritingARowAgainUnderItsIdIsNotAnError: a retry of each of the three inserts.
  • TestAnOwnerScopedWriteWithoutACustomerIsRefused: each writer refuses an empty customer.
  • TestAnotherCustomersWriteToACallIdChangesNothing: call finish and model change.
  • TestAnotherCustomersWriteToARecordingIdChangesNothing: recording start and finish.
  • TestAnotherCustomersFinishOfARunIdChangesNothing: manual task run.
  • TestAnotherCustomersFinishOfASimulationRunIdChangesNothing.
  • TestAnotherCustomersEndOfADialIdChangesNothing.
  • TestAnotherCustomersWriteToAConnectionIdChangesNothing: client and validation.
  • TestAnotherCustomersSwitchUnderASwitchIdIsRefused and TestAnotherCustomersDocumentUnderADocumentIdIsRefused.
  • TestASwitchUnderAnIDAnotherCustomerHasIsAConflict: the 409.
  • TestAnotherCustomersCloseOfTheSameSessionIdLeavesItOpen (RecordFlushSuite): a close through the recorder for another customer leaves the session open.
  • TestTheItemsOfATurnWhoseIdIsAnotherCustomersAreNotWritten (RecordFlushSuite): no item lands under another customer's turn.

Part of AI-3480

@thesyncim
thesyncim force-pushed the fix/ai-3480-session-writes-name-owner branch from e38cb1f to fb80b64 Compare November 23, 2026 12:08
@thesyncim
thesyncim marked this pull request as ready for review November 23, 2026 12:30
@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

@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 648c9ce merged onto current main (129b3c4). CI on the head: all green.

[Should fix] R1.2 PR body: Fixes AI-3480 closes the ticket, but its fix step 2 is not here. That step is to end a session whose opening write was refused, or to reserve the id, plus a test that two apps opening one id leave one session running. It depends on #2177's refused-upsert error, which is not on main yet. Fix: change the body to Part of AI-3480 and keep step 2 open, or add it once #2177 lands.

[Nit] R1.3 internal/store/response.go:124: ErrIDTaken and #2177's ErrSessionTaken are two sentinels for the same thing, "this id belongs to another customer's row". The two PRs also conflict in call_test.go and session_test.go (a local merge of #2177 on top of this one). Whichever lands second can fold the two into one sentinel. Ticket.

Checked and sound:

  • Every write the ticket lists now has a customer predicate.
  • The recorder carries the customer, and the callers pass the row's own customer.
  • SessionTools.Upsert refuses a session the customer does not have.
  • No triggers fire on the new DO UPDATE.
  • generated.sh is in step.
  • 38 mutants checked: 34 killed, 4 survived (R1.1). The call-site mutants in session.go, manager.go, records.go, recordings.go and campaign.go are all killed.
  • Siblings #2181, #2182, #2183, #2189, #2190 and #2195 overlap only in CHANGELOG.md.

Verdict: request changes, for R1.1 (untested rules) and R1.2 (the ticket would close with its fix incomplete).

Comment thread acceleration/internal/store/response.go Resolved
@thesyncim
thesyncim force-pushed the fix/ai-3480-session-writes-name-owner branch from 648c9ce to 579508a Compare November 23, 2026 13:41
@thesyncim

Copy link
Copy Markdown
Member Author

On R1.2: you're right, step 2 isn't here. It needs the refused-upsert error from #2177, which is still open. I changed the last line to Part of AI-3480 and the body now says what is left: end a session whose opening write was refused (or reserve its id), with a test that two apps opening one id leave one session running. That comes after #2177.

On R1.3: left as is. Whichever of this and #2177 lands second folds ErrIDTaken and ErrSessionTaken into one sentinel.

@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
Each write that found a session, response, call or other row by id alone
now also takes the customer and changes only that customer's row: session
close, tool timeout, video and voice, session tools, response and call
finish, call model changes, recordings, a scheduled task run finished by
hand, a connection's OAuth client and validation, a simulation run and a
campaign dial. The session recorder carries the customer with each write.

A response, branch switch or conversation document inserted under an id
another customer's row has is refused with ErrIDTaken instead of being
reported as written; a retry of the same customer's row is still not an
error.
…ites

Recordings.Start and Finish, ScheduledTaskRuns.FinishManual and CampaignAttempts.End refuse an empty customer and id as the other writers do, and SessionTools.Upsert checks its customer first. The changelog entry gets its blank line.
…d turn's items

A finish by another customer of an app's own response leaves the app's
session clock alone, a same-customer retry of each insert is not an
error, and every owner-scoped writer refuses an empty customer, each now
with a test. A branch switch refused because its id is another
customer's is answered with a 409, and the recorder writes no items for
a turn whose row was refused.
@thesyncim
thesyncim force-pushed the fix/ai-3480-session-writes-name-owner branch from 579508a to 3849e38 Compare November 23, 2026 13:54
@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 19da294 into main Nov 23, 2026
13 checks passed
@thesyncim
thesyncim deleted the fix/ai-3480-session-writes-name-owner branch November 23, 2026 14:06
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