Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8228,6 +8228,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
(
SELECT substr(json_extract(message.payload_json, '$.text'), 1, ${ATTENTION_MENTION_TEXT_LIMIT})
FROM orchestration_v2_projection_messages message
INDEXED BY orchestration_v2_projection_messages_thread_updated_idx
WHERE message.thread_id = t.thread_id
AND NOT ${isIdleCompactionMessage("message")}
ORDER BY message.updated_at DESC, message.message_id DESC
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/persistence/Migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ import Migration0096 from "./Migrations/096_RepairStaleTitleRegeneration.ts";
import Migration0097 from "./Migrations/097_AuthSessionFounder.ts";
import Migration0098 from "./Migrations/098_FounderEnrollmentBindings.ts";
import Migration0099 from "./Migrations/099_FounderEnrollmentCorrections.ts";
import Migration0100 from "./Migrations/100_MessagesLatestVisibleIndex.ts";

const migrationEntries = [
[1, "OrchestrationEvents", Migration0001],
Expand Down Expand Up @@ -209,6 +210,7 @@ const migrationEntries = [
[97, "AuthSessionFounder", Migration0097],
[98, "FounderEnrollmentBindings", Migration0098],
[99, "FounderEnrollmentCorrections", Migration0099],
[100, "MessagesLatestVisibleIndex", Migration0100],
] as const;

export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { assert, it } from "@effect/vitest";
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/sql/SqlClient";

import { runMigrations } from "../Migrations.ts";

const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" }));

layer("100_MessagesLatestVisibleIndex", (it) => {
it.effect("serves a thread's newest message of any role in index order, with no sort", () =>
Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* runMigrations();
const plan = (yield* sql.unsafe<{ readonly detail: string }>(`
EXPLAIN QUERY PLAN
SELECT message.message_id
FROM orchestration_v2_projection_messages message
INDEXED BY orchestration_v2_projection_messages_thread_updated_idx
WHERE message.thread_id = 'thread' AND NOT (message.role = 'user' AND message.streaming = 2)
ORDER BY message.updated_at DESC, message.message_id DESC
LIMIT 1
`))
.map((row) => row.detail)
.join("\n");
assert.include(plan, "USING INDEX orchestration_v2_projection_messages_thread_updated_idx");
assert.notInclude(plan, "TEMP B-TREE");
}),
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/sql/SqlClient";

/**
* A thread shell reads its newest visible message by `updated_at`. Only user messages had a
* matching index, so every other read sorted the thread's whole history and parsed each user
* message's JSON to filter it.
*/
export default Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;

yield* sql`
CREATE INDEX IF NOT EXISTS orchestration_v2_projection_messages_thread_updated_idx
ON orchestration_v2_projection_messages(thread_id, updated_at DESC, message_id DESC)
`;
});
Loading