Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Keep spend_usage_json from raising on invalid usage text on Postgres 18 (#1919)

Regan Bell24c374e89e17
diff --git a/src/sessions/postgres-session-store.ts b/src/sessions/postgres-session-store.ts
index 89be48576..0482b0461 100644
--- a/src/sessions/postgres-session-store.ts
+++ b/src/sessions/postgres-session-store.ts
@@ -671,6 +671,29 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
$spend_usage_json$`,
],
},
+ {
+ id: "sessions/store/0020-spend-usage-json-size-plpgsql",
+ statements: [
+ `CREATE OR REPLACE FUNCTION spend_usage_json(t text) RETURNS jsonb
+ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE AS $spend_usage_json$
+ DECLARE j jsonb;
+ BEGIN
+ IF t IS NULL OR octet_length(t) > 2000 OR NOT pg_input_is_valid(t, 'jsonb') THEN
+ RETURN NULL;
+ END IF;
+ j := t::jsonb;
+ IF pg_input_is_valid(j ->> 'costUsd', 'double precision') IS NOT FALSE
+ AND pg_input_is_valid(j ->> 'input', 'bigint') IS NOT FALSE
+ AND pg_input_is_valid(j ->> 'output', 'bigint') IS NOT FALSE
+ AND pg_input_is_valid(j ->> 'cacheRead', 'bigint') IS NOT FALSE
+ AND pg_input_is_valid(j ->> 'cacheWrite', 'bigint') IS NOT FALSE THEN
+ RETURN j;
+ END IF;
+ RETURN NULL;
+ END
+ $spend_usage_json$`,
+ ],
+ },
{
id: "sessions/store/0021-spend-covering-index",
statements: [
diff --git a/test/postgres-store.test.ts b/test/postgres-store.test.ts
index 9b4bc0d1b..6fab6443b 100644
--- a/test/postgres-store.test.ts
+++ b/test/postgres-store.test.ts
@@ -332,7 +332,7 @@ test("pg spend indexes preserve legacy and unusual requests through migration an
try {
await raw.query("DROP INDEX session_llm_requests_spend, session_llm_requests_spend_wide");
await raw.query(
- "DELETE FROM qm_schema_migrations WHERE id IN ('sessions/store/0020-spend-usage-json', 'sessions/store/0020-spend-usage-json-size', 'sessions/store/0021-spend-covering-index')",
+ "DELETE FROM qm_schema_migrations WHERE id IN ('sessions/store/0020-spend-usage-json', 'sessions/store/0020-spend-usage-json-size', 'sessions/store/0020-spend-usage-json-size-plpgsql', 'sessions/store/0021-spend-covering-index')",
);
await insert("spend-index-normal", "normal", JSON.stringify(usage));
await insert("spend-index-wide", wideModel, JSON.stringify(wideUsage));
@@ -3293,3 +3293,25 @@ test("pg context window preserves user memory checkpoints through compaction and
await store.releaseLease(lease);
}
});
+
+test(
+ "pg spend_usage_json returns NULL for invalid or unusual usage text instead of raising (Postgres 18 inlines SQL functions)",
+ { skip },
+ async () => {
+ const store = createPostgresSessionStore(URL!);
+ await store.countSessions();
+ const pg = (await import("pg")).default;
+ const raw = new pg.Pool({ connectionString: URL });
+ try {
+ const { rows } = await raw.query(
+ `SELECT spend_usage_json(t) AS j FROM (VALUES ('not json'), ('{"costUsd":"abc"}'), ('{"costUsd":0.5,"input":1}'), (NULL)) v(t)`,
+ );
+ assert.deepEqual(
+ rows.map((row) => row.j),
+ [null, null, { costUsd: 0.5, input: 1 }, null],
+ );
+ } finally {
+ await raw.end();
+ }
+ },
+);