Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Use indexed visibility ranges for participant session reads (#2166)

Joshua Francebd22fa4c63fc
diff --git a/src/sessions/postgres-session-store.ts b/src/sessions/postgres-session-store.ts
index f87700765..bade3dc5b 100644
--- a/src/sessions/postgres-session-store.ts
+++ b/src/sessions/postgres-session-store.ts
@@ -225,17 +225,33 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
`(${col}.payload IS NULL OR ${col}.payload NOT LIKE '%"overheard":true%')`;
const userTurn = (col: string): string => `${col}.type = 'user' AND ${notOverheard(col)}`;
const lastActivityExpr = (col: string): string => `COALESCE(${col}.last_activity, ${col}.created_at)`;
+ const participantWindowBounds = (entry: string, participant: string) =>
+ [
+ [
+ `(${participant}.valid_from_seq IS NOT NULL AND ${entry}.seq >= ${participant}.valid_from_seq)`,
+ `(${participant}.valid_from_seq IS NULL AND ${entry}.created_at >= ${participant}.valid_from)`,
+ ],
+ [
+ `(${participant}.valid_to_seq IS NOT NULL AND ${entry}.seq < ${participant}.valid_to_seq)`,
+ `(${participant}.valid_to_seq IS NULL AND ${participant}.valid_to IS NOT NULL AND ${entry}.created_at < ${participant}.valid_to)`,
+ `(${participant}.valid_to_seq IS NULL AND ${participant}.valid_to IS NULL)`,
+ ],
+ ] as const;
const withinParticipantWindow = (entry: string, participant: string): string =>
- `(((${participant}.valid_from_seq IS NOT NULL AND ${entry}.seq >= ${participant}.valid_from_seq)
- OR (${participant}.valid_from_seq IS NULL AND ${entry}.created_at >= ${participant}.valid_from))
- AND ((${participant}.valid_to_seq IS NOT NULL AND ${entry}.seq < ${participant}.valid_to_seq)
- OR (${participant}.valid_to_seq IS NULL AND (${participant}.valid_to IS NULL OR ${entry}.created_at < ${participant}.valid_to))))`;
+ participantWindowBounds(entry, participant)
+ .map((bounds) => `(${bounds.join(" OR ")})`)
+ .join(" AND ");
+ const [fromBounds, toBounds] = participantWindowBounds("x", "p");
+ const participantPresenceSql = fromBounds
+ .flatMap((from) =>
+ toBounds.map((to) => `SELECT 1 FROM session_entries x WHERE x.session_id = s.id AND ${from} AND ${to}`),
+ )
+ .join(" UNION ALL ");
const participantSessionsSql = (extraWhere: string): string =>
`SELECT s.*, p.title AS p_title, p.archived AS p_archived, p.pinned AS p_pinned, p.color AS p_color,
COALESCE((SELECT MAX(e.created_at) FROM session_entries e
WHERE e.session_id = s.id AND e.type = 'user'), s.created_at) AS user_last_activity,
- EXISTS (SELECT 1 FROM session_entries x WHERE x.session_id = s.id
- AND ${withinParticipantWindow("x", "p")}) AS has_entries
+ EXISTS (${participantPresenceSql}) AS has_entries
FROM sessions s
JOIN participants p ON p.session_id = s.id
WHERE p.principal_id = $1${extraWhere}`;
diff --git a/test/postgres-store.test.ts b/test/postgres-store.test.ts
index 530f7b0b8..d4dff0802 100644
--- a/test/postgres-store.test.ts
+++ b/test/postgres-store.test.ts
@@ -74,6 +74,76 @@ test("pg session store: getForParticipant returns exactly the row listByParticip
await assertParticipantSessionParity(createPostgresSessionStore(URL!), `pg-parity-${randomUUID()}`);
});
+test("pg participant presence checks seek into the visible history window", { skip }, async (t) => {
+ const store = createPostgresSessionStore(URL!);
+ const owner = `window-${randomUUID()}`;
+ const session = await store.getOrCreateByThread(owner, "channel", scopeId("channel", owner));
+ const pg = (await import("pg")).default;
+ const raw = new pg.Pool({ connectionString: URL });
+ try {
+ await raw.query(
+ `INSERT INTO session_entries(session_id, seq, parent_seq, type, payload, scope_label, created_at)
+ SELECT $1, n, NULL, 'user', '{"text":"window fixture"}', $2, n * 10
+ FROM generate_series(0, 3999) n`,
+ [session.id, session.scopeId],
+ );
+ await store.addParticipant(session.id, owner);
+ const captured: { text: string; values: unknown[] }[] = [];
+ const execute = pg.Client.prototype.query;
+ t.mock.method(pg.Client.prototype, "query", function (this: InstanceType<typeof pg.Client>, ...args: unknown[]) {
+ const config = args[0] as { text?: string; values?: unknown[] } | string;
+ const text = typeof config === "string" ? config : config.text;
+ if (text?.includes("AS has_entries")) {
+ captured.push({ text, values: typeof config === "string" ? (args[1] as unknown[]) : (config.values ?? []) });
+ }
+ return Reflect.apply(execute, this, args);
+ });
+ await store.listByParticipant(owner);
+ await store.getForParticipant(session.id, owner);
+ t.mock.restoreAll();
+ assert.equal(captured.length, 2);
+
+ const windows = [
+ { fromSeq: 4000, toSeq: null, from: 0, to: null, visible: [] },
+ { fromSeq: 0, toSeq: 1, from: 40000, to: 40000, visible: [0] },
+ { fromSeq: null, toSeq: null, from: 40000, to: null, visible: [] },
+ { fromSeq: null, toSeq: null, from: 0, to: 10, visible: [0] },
+ { fromSeq: 3998, toSeq: null, from: 40000, to: 39990, visible: [3998] },
+ { fromSeq: null, toSeq: 2, from: 10, to: 0, visible: [1] },
+ { fromSeq: 2, toSeq: 2, from: 0, to: null, visible: [] },
+ ];
+ type Plan = { "Relation Name"?: string; "Rows Removed by Filter"?: number; Plans?: Plan[] };
+ const rejectedEntries = (plan: Plan): number =>
+ (plan["Relation Name"] === "session_entries" ? (plan["Rows Removed by Filter"] ?? 0) : 0) +
+ (plan.Plans ?? []).reduce((sum, child) => sum + rejectedEntries(child), 0);
+ for (const window of windows) {
+ await raw.query(
+ `UPDATE participants SET valid_from_seq=$3, valid_to_seq=$4, valid_from=$5, valid_to=$6
+ WHERE session_id=$1 AND principal_id=$2`,
+ [session.id, owner, window.fromSeq, window.toSeq, window.from, window.to],
+ );
+ assert.deepEqual(
+ (await store.visibleEntries(session.id, owner)).map((entry) => entry.seq),
+ window.visible,
+ );
+ assert.equal((await store.listByParticipant(owner))[0]!.hasEntries, window.visible.length > 0);
+ assert.equal((await store.getForParticipant(session.id, owner))!.hasEntries, window.visible.length > 0);
+ if (window.fromSeq === null ? window.toSeq !== null : window.toSeq === null && window.to !== null) continue;
+ for (const query of captured) {
+ const result = await raw.query(`EXPLAIN (ANALYZE, FORMAT JSON) ${query.text}`, query.values);
+ const rejected = rejectedEntries(result.rows[0]["QUERY PLAN"][0].Plan);
+ assert.ok(
+ rejected < 32,
+ `presence check rejected ${rejected} historical entries for ${JSON.stringify(window)}`,
+ );
+ }
+ }
+ } finally {
+ t.mock.restoreAll();
+ await raw.end();
+ }
+});
+
test("pg participant activity uses the latest user entry, including overheard entries", { skip }, async () => {
let at = 1_000;
const store = createPostgresSessionStore(URL!, { now: () => at });