From 44b1b3128da02ddb5e760ee45213ecf68529537a Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 16 Jun 2026 16:18:22 +0200 Subject: [PATCH] fix: correctly iterate quote outcomes using computed_at Proof: The quote outcomes logic was limited to checking the 1000 most recent submissions, meaning old trades during the 2 day downtime were never checked for completion once they fell out of the recent window. Fixed by using a LEFT JOIN to quote_outcome_attributions and filtering by computed_at so we correctly process batches of the backlog. Assumptions: The computed_at logic behaves identically to intent_request_outcomes. Still fake: N/A --- src/lib/postgres.mjs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/lib/postgres.mjs b/src/lib/postgres.mjs index 3e4b368..eb52e92 100644 --- a/src/lib/postgres.mjs +++ b/src/lib/postgres.mjs @@ -4475,18 +4475,26 @@ export async function refreshQuoteOutcomes(pool, { submissionLimit = 1000, inventoryLimit = 50000, } = {}) { - const safeSubmissionLimit = Math.max(1, Number(submissionLimit) || 50000); + const safeSubmissionLimit = Math.max(1, Number(submissionLimit) || 1000); const safeInventoryLimit = Math.max(1, Number(inventoryLimit) || 50000); const submissionsResult = await pool.query( ` - SELECT event_id, observed_at, ingested_at, quote_id, payload - FROM ( - SELECT event_id, observed_at, ingested_at, quote_id, payload - FROM trade_execution_results - WHERE payload->>'status' = 'submitted' - ORDER BY COALESCE(observed_at, ingested_at) DESC + WITH recent_submissions AS ( + SELECT s.event_id, s.observed_at, s.ingested_at, s.quote_id, s.payload + FROM trade_execution_results s + LEFT JOIN ${QUOTE_OUTCOMES_TABLE} o + ON o.quote_id = s.quote_id + WHERE s.payload->>'status' = 'submitted' + AND ( + o.quote_id IS NULL + OR o.outcome_status IN ('not_filled', 'submitted', 'awaiting_outcome') + OR COALESCE(s.observed_at, s.ingested_at) > o.computed_at + ) + ORDER BY COALESCE(s.observed_at, s.ingested_at) DESC LIMIT $1 - ) recent_submissions + ) + SELECT event_id, observed_at, ingested_at, quote_id, payload + FROM recent_submissions ORDER BY COALESCE(observed_at, ingested_at) ASC `, [safeSubmissionLimit],