Commit 1ced0b6
authored
fix(knowledge): stop the stuck-document sweep reclaiming still-queued documents (#6921)
* fix(knowledge): stop the stuck-document sweep reclaiming still-queued documents
The sweep gave 'processing' a staleness cutoff but gave 'pending' and 'failed'
none, and the only cross-run guard was uploadedAt < syncStartedAt, which scopes
within a sync but not across them.
That was harmless while document processing awaited inline: documents were
terminal by the time a sync ended. Since dispatch became asynchronous they sit
'pending' until a worker picks them up, so the next sync deleted their
embeddings, reset them, and re-dispatched while the original runs were still
executing — and each re-dispatch mints a fresh pass id, so it billed again.
Queued documents now get a grace period derived from queue drain time rather
than run duration: the processing queue's concurrency is 20 and global across
workspaces, so a 2,600-document corpus takes roughly two hours to drain. The
existing 45-minute threshold bounds a run, not a wait, and would still reclaim
live documents on any corpus over about 900.
No column records dispatch time, so the signal is processingStartedAt falling
back to uploadedAt. The sweep now stamps that column when it re-dispatches, and
the user-triggered retry stamps it instead of clearing it — without both, only a
document's first dispatch was protected and the sweep churned once per sync
forever on anything that kept waiting.
The grace narrows the duplicate-billing window but cannot close it: a
re-dispatch mints a new request id and the Trigger idempotency key is scoped per
dispatch by design. Closing it durably needs a document-scoped key or a
dispatch-generation column, recorded in TSDoc and deliberately not built here.
* fix(knowledge): record dispatch time in its own column instead of overloading the start time
Two review findings on this PR traced to the same root: the queue grace was
reading processingStartedAt, a column that means something else.
Externally, a pending row carrying a dispatch timestamp reported a processing
start time for work that had not started. Internally, updateDocument sets a
document pending and refreshes uploadedAt while leaving the previous run's
processingStartedAt in place, and completion never clears it — so the sweep aged
a re-dispatched document from a leftover stamp rather than its actual dispatch,
and could reclaim it inside the grace window while the earlier queue entry was
still live. That reopens the duplicate-billing race this PR closes.
processing_queued_at is written on every re-dispatch and read by the sweep;
processingStartedAt goes back to meaning what its name says, so its external
contract is byte-identical to before this PR.
Not stamped on first dispatch: the row is created and dispatched inside the same
sync run, so uploadedAt is already accurate there and a guarded write per upload
would buy no behavior.
The internal document route returns a full table row through a passthrough
schema, so the new column would have shipped as an undeclared raw Date. Declared
and serialized explicitly instead.
* fix(knowledge): stamp the queue time in the dispatch funnel, not at each call site
Giving dispatch its own column was not enough. The connector content-update path
updates a row in place with pending status and a fresh uploadedAt while leaving
every prior-run processing column intact, so a document dispatched once already
carried a stale queue stamp, the refreshed uploadedAt was never consulted, and
the row aged from a dead run's timestamp — the same bug one level down.
"Written on dispatch and only on dispatch" is now structural rather than a
convention three call sites remember: the stamp lives in processDocumentsWithQueue,
which every one of the nine dispatch paths already goes through, so none can
forget it. The sweep's and the retry's own stamps are kept because they land
inside the same transaction as their reset, so a document keeps its grace even
if the dispatch that follows throws.
The funnel also clears processingStartedAt, guarded on the row still being
pending so it cannot disturb a worker's compare-and-set. That closes the
API-facing half at its source: a pending row can no longer report a start time
from any path, including the content-update path no serializer would have seen.1 parent 2074afe commit 1ced0b6
11 files changed
Lines changed: 20513 additions & 21 deletions
File tree
- apps/sim/lib
- api/contracts/knowledge
- knowledge
- api
- connectors
- documents
- packages
- db
- migrations
- meta
- testing/src/mocks
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
267 | 267 | | |
268 | 268 | | |
269 | 269 | | |
| 270 | + | |
| 271 | + | |
270 | 272 | | |
271 | 273 | | |
272 | 274 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
| |||
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
| 73 | + | |
72 | 74 | | |
73 | 75 | | |
74 | 76 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
702 | 703 | | |
703 | 704 | | |
704 | 705 | | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
61 | 79 | | |
62 | 80 | | |
63 | 81 | | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
64 | 146 | | |
65 | 147 | | |
66 | 148 | | |
| |||
1368 | 1450 | | |
1369 | 1451 | | |
1370 | 1452 | | |
1371 | | - | |
1372 | | - | |
1373 | | - | |
1374 | | - | |
1375 | | - | |
1376 | | - | |
1377 | | - | |
1378 | | - | |
| 1453 | + | |
| 1454 | + | |
| 1455 | + | |
| 1456 | + | |
| 1457 | + | |
| 1458 | + | |
| 1459 | + | |
| 1460 | + | |
| 1461 | + | |
| 1462 | + | |
| 1463 | + | |
| 1464 | + | |
| 1465 | + | |
| 1466 | + | |
1379 | 1467 | | |
1380 | 1468 | | |
1381 | 1469 | | |
1382 | 1470 | | |
1383 | 1471 | | |
1384 | 1472 | | |
| 1473 | + | |
| 1474 | + | |
| 1475 | + | |
| 1476 | + | |
1385 | 1477 | | |
1386 | 1478 | | |
1387 | 1479 | | |
1388 | 1480 | | |
1389 | 1481 | | |
1390 | | - | |
1391 | | - | |
1392 | | - | |
1393 | | - | |
1394 | | - | |
1395 | | - | |
1396 | | - | |
1397 | | - | |
1398 | | - | |
1399 | | - | |
| 1482 | + | |
1400 | 1483 | | |
1401 | 1484 | | |
1402 | 1485 | | |
1403 | | - | |
1404 | | - | |
1405 | 1486 | | |
1406 | 1487 | | |
1407 | 1488 | | |
1408 | 1489 | | |
1409 | 1490 | | |
| 1491 | + | |
| 1492 | + | |
| 1493 | + | |
1410 | 1494 | | |
1411 | 1495 | | |
1412 | 1496 | | |
| |||
1451 | 1535 | | |
1452 | 1536 | | |
1453 | 1537 | | |
| 1538 | + | |
| 1539 | + | |
| 1540 | + | |
| 1541 | + | |
| 1542 | + | |
| 1543 | + | |
1454 | 1544 | | |
1455 | 1545 | | |
1456 | 1546 | | |
| |||
0 commit comments