Title: fix(knowledge): stop the stuck-document sweep reclaiming still-queued documents by waleedlatif1 · Pull Request #6921 · simstudioai/sim · GitHub
Open Graph Title: fix(knowledge): stop the stuck-document sweep reclaiming still-queued documents by waleedlatif1 · Pull Request #6921 · simstudioai/sim
X Title: fix(knowledge): stop the stuck-document sweep reclaiming still-queued documents by waleedlatif1 · Pull Request #6921 · simstudioai/sim
Description: Live on staging now. Introduced by the interaction between #6920 and a pre-existing gap in the stuck-document sweep — each is fine alone. The bug The sweep gives 'processing' a staleness cutoff but gives 'pending' and 'failed' none at all. The only cross-run guard is uploadedAt < syncStartedAt, which scopes within a sync but not across syncs. That was harmless while processDocumentsWithQueue awaited every document inline — documents were terminal by the time a sync ended. Since #6920 made dispatch asynchronous they sit 'pending' until a worker picks them up, and processingStatus only flips to 'processing' when the worker starts. So the next sync sees merely-queued documents, deletes their embeddings, resets them, and re-dispatches while the original runs may still be executing. Each re-dispatch mints a fresh pass id, so it bills again — directly undercutting #6911. Very reachable: document-processing-queue has a global concurrency of 20, so a large connector drains slowly, and syncIntervalMinutes is validated at min(0). The signal No column records dispatch time — document has only uploadedAt, processingStartedAt, processingCompletedAt. So queued age is processingStartedAt ?? uploadedAt: First dispatch: uploadedAt is the proxy. Its error is bounded and one-directional — dispatch happens later in the same sync — so it over-estimates the wait by at most the 1h sync ceiling. Re-dispatch: uploadedAt is arbitrarily old and useless, so the sweep now stamps processingStartedAt when it re-dispatches. Without this only a document's first dispatch was protected and the sweep churned once per sync forever on anything that kept waiting — the same bug, one layer down. User-triggered retry (retryDocumentProcessing) previously set the column to null and dispatched, leaving a connector document with an old uploadedAt sweepable by the very next sync. It now stamps the requeue time. The overload is safe: every reader of processingStartedAt gates on processingStatus === 'processing' first, and processDocumentAsync claims with an unconditional update — it never requires the column to be null — so a stamped pending row is claimed exactly as a nulled one was. processing-outbox-handler.ts deliberately unchanged: the outbox is the upload path only, so those documents have connectorId = null and the connector-scoped sweep can never see them. Its null write is also load-bearing there as the CAS token invalidating a dead worker. The grace period: 240 minutes Derived from queue drain time, not run duration — the existing STALE_PROCESSING_MINUTES = 45 bounds a run (600s × 3 attempts), which is a different quantity. Concurrency is 20, global across all workspaces (verified on the task definition; no concurrencyKey exists anywhere in the document dispatch path). The largest documented corpus — 2,600 documents — is 130 waves, roughly two hours to drain, longer when other tenants hold slots. 240 is ~2× that and still well under the 1,440-minute default syncIntervalMinutes, so a default-configured connector gains zero recovery latency. 45 would still reclaim live documents on any corpus over ~900. Recovery is preserved — nothing was removed from the sweep, only delayed. A dispatch lost to a crash is swept by the first sync starting more than 240 minutes after it was queued; RETRY_WINDOW_DAYS = 7 still bounds it from above. A 'processing' document abandoned mid-run is unaffected at 45 minutes, and a user can retry a 'failed' document immediately. 'failed' deliberately gets no grace: it is terminal, so re-dispatch cannot duplicate live work, and it is the state the user-facing retry operates from. Structure The SQL now selects a coarse, index-friendly superset and the age decision moves to isStuckDocumentSweepEligible, a pure exported function. That matters for testability — this file's tests mock drizzle-orm to bare vi.fn()s, so a SQL-shape assertion would prove nothing. Verification type-check clean · check:audits 32/32 · 567 tests across lib/knowledge. Nine mutations, all red. The flagship — making 'pending' unconditionally sweepable, i.e. exactly today's staging behavior — turns four tests red, including the requested "dispatched by the previous sync and still queued". The new retry-processing-grace.test.ts calls the real retryDocumentProcessing, captures its actual .set() payload, and feeds it into the real predicate, so the linkage between the two files is executed rather than asserted by review. Known limitations, stated plainly The grace narrows the duplicate-billing window; it cannot close it. A re-dispatch mints a new request id and the Trigger idempotency key is doc-process-{documentId}-{requestId} — scoped per dispatch by design, so cross-sync duplicates are never deduped. Closing it durably needs a document-scoped key or a dispatch-generation column the worker checks before billing. Recorded in TSDoc, deliberately not built here. Two load-bearing pieces have no test coverage, by choice: the processingStartedAt stamp in the sweep's own reset, and the widened SQL predicate. Both live inside executeSync, for which no harness exists; building one means standing up the whole db/transaction/registry stack. The retry-path equivalent of that stamp is covered, which is the closest available proxy — same column, same fallback, same predicate. If connector documents are ever routed through the outbox, the assumption above breaks and the same stamp is needed in processing-claim.ts.
Open Graph Description: Live on staging now. Introduced by the interaction between #6920 and a pre-existing gap in the stuck-document sweep — each is fine alone. The bug The sweep gives 'processing' a staleness cu...
X Description: Live on staging now. Introduced by the interaction between #6920 and a pre-existing gap in the stuck-document sweep — each is fine alone. The bug The sweep gives 'processing' a stal...
Opengraph URL: https://github.com/simstudioai/sim/pull/6921
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:814689a4-8691-9fae-5504-1a63da3071ef |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | A478:2E4CE1:7250E8:A01AB0:6AFB67FC |
| html-safe-nonce | f4f85fa84ae63027d1b6d803a9a689289aeb154766ceba7ac71c5f8b45891553 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNDc4OjJFNENFMTo3MjUwRTg6QTAxQUIwOjZBRkI2N0ZDIiwidmlzaXRvcl9pZCI6IjMyMDI3MjYxMzQ4MjI5NTUwMDQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 455bcbd1ad012835031a57368f5a2ef87434cfcd467037b5e64c4e3d2c9affc4 |
| hovercard-subject-tag | pull_request:4327894537 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/simstudioai/sim/pull/6921/files |
| twitter:image | https://avatars.githubusercontent.com/u/40672544?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/40672544?s=400&v=4 |
| og:image:alt | Live on staging now. Introduced by the interaction between #6920 and a pre-existing gap in the stuck-document sweep — each is fine alone. The bug The sweep gives 'processing' a staleness cu... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | ea33d13c96d0ff66bd8fa906b193d2213536b78cb06b0c50a4632c58b1418f94 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/simstudioai/sim git https://github.com/simstudioai/sim.git |
| octolytics-dimension-user_id | 199344406 |
| octolytics-dimension-user_login | simstudioai |
| octolytics-dimension-repository_id | 912559512 |
| octolytics-dimension-repository_nwo | simstudioai/sim |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 912559512 |
| octolytics-dimension-repository_network_root_nwo | simstudioai/sim |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 59ef97164c52cf59bd10558ddec9c4f482763988 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width