Repository navigation
Add import-job lifecycle: cancel in-progress and delete finished jobs - #8975
Conversation
There was a problem hiding this comment.
1 issue found across 4 files
Confidence score: 2/5
- In
backend/routers/imports.py(cancel endpoint viaupdate_import_job), there’s a race where a cancel request can overwrite a job that already finished because the Firestore write is unconditional; merging as-is could leave users with incorrect final job states and potentially trigger wrong downstream behavior. Add a transaction or compare-and-set precondition on the status update (and reject stale cancels) before merging.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="backend/routers/imports.py">
<violation number="1" location="backend/routers/imports.py:183">
P1: The cancel endpoint can overwrite a job that finished between the status check and the update. Because `update_import_job` does a raw Firestore write with no transaction or CAS precondition, a background worker that transitions the job to `completed`/`failed` after the `if` check but before the update will have its result silently lost. The user sees `cancelled` even though the import actually succeeded, which breaks the intended state machine and can lead to unnecessary re-imports. Consider wrapping the check-and-update in a Firestore transaction so the cancellation only applies when the status is still `pending` or `processing` at the moment of the write.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| if job.get('status') not in (ImportJobStatus.pending.value, ImportJobStatus.processing.value): | ||
| raise HTTPException(status_code=409, detail="Only a pending or processing import can be cancelled") | ||
|
|
||
| import_jobs_db.update_import_job(job_id, {'status': ImportJobStatus.cancelled.value, 'error': 'Cancelled by user'}) |
There was a problem hiding this comment.
P1: The cancel endpoint can overwrite a job that finished between the status check and the update. Because update_import_job does a raw Firestore write with no transaction or CAS precondition, a background worker that transitions the job to completed/failed after the if check but before the update will have its result silently lost. The user sees cancelled even though the import actually succeeded, which breaks the intended state machine and can lead to unnecessary re-imports. Consider wrapping the check-and-update in a Firestore transaction so the cancellation only applies when the status is still pending or processing at the moment of the write.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At backend/routers/imports.py, line 183:
<comment>The cancel endpoint can overwrite a job that finished between the status check and the update. Because `update_import_job` does a raw Firestore write with no transaction or CAS precondition, a background worker that transitions the job to `completed`/`failed` after the `if` check but before the update will have its result silently lost. The user sees `cancelled` even though the import actually succeeded, which breaks the intended state machine and can lead to unnecessary re-imports. Consider wrapping the check-and-update in a Firestore transaction so the cancellation only applies when the status is still `pending` or `processing` at the moment of the write.</comment>
<file context>
@@ -169,6 +169,44 @@ def get_import_job_status(
+ if job.get('status') not in (ImportJobStatus.pending.value, ImportJobStatus.processing.value):
+ raise HTTPException(status_code=409, detail="Only a pending or processing import can be cancelled")
+
+ import_jobs_db.update_import_job(job_id, {'status': ImportJobStatus.cancelled.value, 'error': 'Cancelled by user'})
+ return ImportJobResponse(
+ job_id=job['id'],
</file context>
|
Thanks @ZachL111 — this is a well-structured, purely-additive change that fills a real gap in the import-job lifecycle, and the test coverage is thorough (14 cases, all green). A few notes for a maintainer:
Leaving this for human maintainer review since it touches user-data persistence. |
Users could start, list, and check import jobs but never cancel a stuck or running import or
remove a finished one, so failed jobs accumulated with no recourse. This wires up the two missing
verbs (the delete_import_job db helper already existed but was never called by a router).
- models/import_job.py: add a cancelled status.
- routers/imports.py: POST /v1/import/jobs/{job_id}/cancel (pending/processing -> cancelled, 409
otherwise) and DELETE /v1/import/jobs/{job_id} (terminal states only, 409 while in progress).
Both mirror the existing 404/403 ownership checks.
- utils/imports/limitless.py: the worker re-reads the job before its final status write and skips
it if the job was cancelled, so a mid-run cancel is not overwritten by completed/failed.
f6ba752 to
7ff8a2c
Compare
kodjima33
left a comment
There was a problem hiding this comment.
Reviewed: scoped backend addition, approve-only per policy (features/new endpoints are Nik's merge call).
|
Thanks for this, @ZachL111 — clean implementation. A few observations from automated review: Strengths
Minor notes for a human maintainer
This touches user-data persistence and auth surfaces, so formal approval is intentionally withheld — human maintainer review is needed before merge. The implementation itself looks solid. Automated review by Omi PR maintainer monitor (glm-5.2). AI-agent review — please factor that into your judgment. |
What
Users can start (
POST /v1/import/limitless), list (GET /v1/import/jobs), and check the status of an import job, but there was no way to cancel a stuck or running import or remove a finished/failed one, so old jobs pile up in the list with no recourse. This adds the two missing lifecycle verbs. It sits in the actively-built ChatGPT/Claude/Limitless memory-bank import area (#4862, #8764), and it wires up thedelete_import_jobdb helper that already shipped but was never called by any router.Purely additive: no existing route or behavior changes.
Changes
models/import_job.py— add acancelledstatus toImportJobStatus.routers/imports.pyPOST /v1/import/jobs/{job_id}/cancel— cancels apendingorprocessingjob (setscancelled+ an error note); 409 if the job is already terminal.DELETE /v1/import/jobs/{job_id}— deletes a terminal job (completed/failed/cancelled); 409 while it is still in progress (cancel first).get_import_job_statusownership checks (404 unknown, 403 wrong owner). The two endpoints form a clean state machine (cancel for in-progress, delete for terminal), so the delete path never races the background worker.utils/imports/limitless.py— the worker re-reads the job right before its final status write and bails if it was cancelled, so a mid-run cancel is not overwritten bycompleted/failed.Tests
tests/routers/test_imports.py(14 cases): cancel of pending/processing setscancelledand callsupdate_import_job; cancel of a terminal job is 409; delete of completed/failed/cancelled callsdelete_import_job; delete of an in-progress job is 409; and both 404 on a missing job and 403 on a wrong-owner job. The module is imported normally and the db helpers are patched (nosys.modulesmutation, no network).