Clear session links no carry-over made, at boot

Sessions from v0.2.8 and earlier were linked to the one before them by
every rollover and New session, so production threads offered "Show the
previous session" (and prompts read summaries) for sessions nothing was
carried from. A link is kept only when the session opens with a message
carried from that session.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-26 06:21:51 +01:00
co-authored by Claude Opus 5.5
parent c7e5ed401b
commit 56f219b322
5 changed files with 67 additions and 1 deletions
@@ -0,0 +1,32 @@
import { Database } from "bun:sqlite";
import { expect, test } from "bun:test";
import clearImplicitSessionLinksSql from "./clear-implicit-session-links-sql";
/**
* Only a carry-over keeps its link to the session before it. Run against
* real SQLite.
*/
test("clears links without a carried-over message", () => {
const db = new Database(":memory:");
db.run(`CREATE TABLE bot_sessions (id INTEGER PRIMARY KEY, previous_session_id INTEGER)`);
db.run(`CREATE TABLE bot_messages (id INTEGER PRIMARY KEY, session_id INTEGER, carried_from_session_id INTEGER)`);
db.run(`INSERT INTO bot_sessions VALUES (1, NULL), (2, 1), (3, 2), (4, 3)`);
db.run(`INSERT INTO bot_messages (id, session_id, carried_from_session_id) VALUES
(1, 2, 1),
(2, 3, NULL),
(3, 4, 1)`);
const { sql, values } = clearImplicitSessionLinksSql();
db.query(sql).run(...values);
const links = db.query(`SELECT id, previous_session_id FROM bot_sessions ORDER BY id`).all();
expect(links).toEqual([
{ id: 1, previous_session_id: null },
{ id: 2, previous_session_id: 1 },
{ id: 3, previous_session_id: null },
{ id: 4, previous_session_id: null },
]);
});
@@ -0,0 +1,20 @@
/**
* Clears `previous_session_id` on every session that wasn't carried over:
* before v0.3.0 a rollover and New session linked each session to the
* one before it, so threads offered "Show the previous session" and
* prompts read summaries of sessions nothing was carried from. A carry
* over always opens its session with a message naming the one it came
* from (`carried_from_session_id`); a link without one is cleared.
*/
export default function clearImplicitSessionLinksSql() {
return {
sql: `UPDATE bot_sessions SET previous_session_id = NULL
WHERE previous_session_id IS NOT NULL
AND NOT EXISTS (
SELECT 1 FROM bot_messages m
WHERE m.session_id = bot_sessions.id
AND m.carried_from_session_id = bot_sessions.previous_session_id
)`,
values: [] as number[],
};
}
@@ -0,0 +1,10 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import clearImplicitSessionLinksSql from "./clear-implicit-session-links-sql";
/**
* Drops session links from v0.2.8 and earlier that no carry-over made,
* safe to run on every start (`clearImplicitSessionLinksSql`).
*/
export default async function clearImplicitSessionLinks() {
await BunSQLite.sql(clearImplicitSessionLinksSql());
}
+4
View File
@@ -10,6 +10,7 @@ import fileIngest from "./file-ingest";
import liveRelay from "./live-relay";
import enableForeignKeys from "./functions/backend/db/enable-foreign-keys";
import migrateBotTypes from "./functions/backend/db/bot-types/migrate-bot-types";
import clearImplicitSessionLinks from "./functions/backend/db/bot-sessions/clear-implicit-session-links";
import handleComputerProxy from "./functions/server/computers/handle-computer-proxy";
import handlePwaFile from "./functions/server/pwa/handle-pwa-file";
import handleInstallIconFile from "./functions/server/pwa/handle-install-icon-file";
@@ -34,6 +35,9 @@ await enableForeignKeys();
await migrateBotTypes().catch((error) =>
logError({ title: "Could not migrate bot types", error }).catch(() => {}),
);
await clearImplicitSessionLinks().catch((error) =>
logError({ title: "Could not clear implicit session links", error }).catch(() => {}),
);
await bunext.bunextInit();
/**