Skip to content

appendRunTurn

const appendRunTurn: (request, placeholder, alongside) => Promise<AppendedRunTurn | undefined> = sqliteRepo.appendRunTurn

Defined in: src/lib/repositories/chat-history.ts:45

Write a request, the assistant row that will report on it, and the durable job that answers it — in one commit.

Separate writes gave every failure between them its own broken shape: a run with no card, a card pointing at a run that was never created, a reload that lost the task, and a retry from the UI that started the work twice. One commit has none of those states because it has no between.

alongside is handed the two message ids and returns the statement that files the job against them. It is a statement rather than a callback into another repository because this module owns the chat tables and nothing else: the job’s own repository composes its INSERT, and neither module has to know what the other stores.

A session that is not there resolves undefined rather than throwing, so the decision is the caller’s and is made once, inside the transaction. A caller that checked first and then called had a window: a chat deleted between the two turned “start this run without linkage” into a failed start.

The request hangs off the session’s current leaf, read in the same transaction. Inserted with no parent it was a second root: the assistant row became the active leaf, the conversation so far was no longer an ancestor of it, and loading the chat showed the run with everything before it gone.

Omit<StoredMessage, "id">

(requestMessageId) => Omit<StoredMessage, "id">

(ids) => RunTurnStatement

Promise<AppendedRunTurn | undefined>