unrip/check-matches-small.mjs
philipp fb547f24d9
All checks were successful
deploy / deploy (push) Successful in 48s
fix: scope inventory fetch by submission time window to prevent OOM
Instead of fetching up to 50k inventory snapshots (~442MB), scope the
query to only the time range of the current submission batch with a
15-minute buffer. For a typical 1-hour batch this drops from 50k rows
to ~300 rows, well within the 1280Mi pod memory limit.

The coalesced_at_idx on intent_inventory_snapshots covers the BETWEEN
clause so this remains efficient.

Proof: history-writer OOM kills from refreshQuoteOutcomes inventory fetch
Assumptions: 15min buffer covers the attribution window for all submissions
Still fake: heuristic gap outcomes may attribute trades imprecisely
2026-06-16 17:17:19 +02:00

54 lines
1.9 KiB
JavaScript

import { createPostgresPool } from './src/lib/postgres.mjs';
import { deriveQuoteOutcomeRecords } from './src/core/quote-outcomes.mjs';
async function main() {
const pool = createPostgresPool({ connectionString: process.env.POSTGRES_URL });
// Wait a bit for connection
await new Promise(r => setTimeout(r, 1000));
console.log("Fetching submissions...");
const expectedDeltasRows = await pool.query(`
SELECT
quote_id,
COALESCE(observed_at, ingested_at) as submitted_at,
payload->>'expected_inventory_delta' as expected_inventory_delta
FROM trade_execution_results
WHERE payload->>'status' = 'submitted'
ORDER BY COALESCE(observed_at, ingested_at) DESC
LIMIT 2000
`);
const submissions = expectedDeltasRows.rows.map(r => ({
quote_id: r.quote_id,
submitted_at: r.submitted_at,
expected_inventory_delta: typeof r.expected_inventory_delta === 'string' && r.expected_inventory_delta ? JSON.parse(r.expected_inventory_delta) : r.expected_inventory_delta
}));
console.log("Fetching inventory...", submissions.length);
const inventoryRows = await pool.query(`
SELECT
event_id as inventory_id,
observed_at,
payload
FROM intent_inventory_snapshots
ORDER BY COALESCE(observed_at, ingested_at) DESC
LIMIT 5000
`);
const inventorySnapshots = inventoryRows.rows.map(r => ({
inventory_id: r.inventory_id,
observed_at: r.observed_at,
asset_balances: r.payload?.asset_balances || {},
}));
console.log("Checking for matches...", submissions.length, inventorySnapshots.length);
const records = deriveQuoteOutcomeRecords({
submissions,
inventorySnapshots,
});
const completed = records.filter(r => r.outcome_status === 'completed');
console.log("Found", completed.length, "completed trades out of", records.length);
process.exit(0);
}
main().catch(console.error);