Adopt Kolibri's supervisor ownership and reconciliation API for WorkManager-dispatched jobs
❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.
Overview
Kolibri is gaining a supervisor ownership and reconciliation mechanism for the task runner (supervisor registry, supervisor_id ownership on jobs, and reconcile_stalled_jobs). On Android, jobs are dispatched by WorkManager rather than Kolibri's in-process worker pool, so a Kolibri job row can be left stuck in RUNNING when Android kills the worker process. Kolibri now exposes an API for an external dispatcher to take ownership of jobs and to reconcile against an externally supplied live set; this task adopts it in the Android task layer.
Complexity: Low Target branch: main
Context
Kolibri-side API (in the kolibri repo, PR link to follow):
execute_job(job_id, ..., supervisor_id=None)— when given, the job is marked RUNNING and owned bysupervisor_idbefore execution, with ownership preserved through the bare re-mark insideJob.execute(). If the job is already owned by a different supervisor the claim fails andexecute_jobreturns without running it, so a job must be reconciled (its dead owner cleared) before it can be re-dispatched. A job with no owner (NULL) is claimable, so rows left by the pre-update taskworker still run.Storage.reconcile_stalled_jobs(supervisor_stale_threshold=None, live_supervisor_ids=None)— exactly one kwarg. Withlive_supervisor_ids, SELECTED and RUNNING jobs whose owner is not in the set (or that have no owner) are requeued to QUEUED, and such CANCELING jobs are finalized to CANCELED. The registry is not used as a liveness source in this mode; reconcile clears any registry rows it finds, and Android's is always empty.- Requeues and finalizations run through registered job hooks'
update(state=...); initial scheduling runsschedule. The Android hook keys offupdate(state=QUEUED)to re-enqueue (see The Change).
Reconciliation is at-least-once: a job wrongly judged dead may still finish in parallel with its requeued run, so tasks are expected to be idempotent.
The Change
taskworker.execute_jobtakes ownership of the job using the WorkManager request id as the supervisor identity. The request id survives WorkManager's own retries, so a retried request keeps owning its job rather than triggering a false requeue.AndroidJobHookenqueues to WorkManager on the transition into QUEUED, not only on schedule.schedule()'sTask.enqueueOncebody moves into a shared helper that bothschedule()andupdate()call;update()invokes it whenstate == State.QUEUED. This fires only on a genuine transition — core's identical-re-mark guard suppresses the hook for a no-op QUEUED→QUEUED update, so an already-queued row is not re-enqueued. A reconcile requeue therefore re-enters WorkManager immediately, exactly as a freshly scheduled job does.task_reconcilerrunsreconcile_stalled_jobsas the first step of_do_reconciliation, passing the active WorkManager request ids as the live set. Clearing the dead owner first is required, or the fencedexecute_jobwould refuse to re-run the job; the requeue then re-enqueues via the hook above.- The existing WorkManager↔queue sync is retained as the queue-membership reconciler — a different axis from
reconcile_stalled_jobs, which only handles ownership/liveness of supervised states. Kolibri's DB and WorkManager are two stores with no shared transaction, so they drift: it re-enqueues QUEUED jobs that never reached WorkManager (a failed or lostenqueueOnce, or WM eviction) and cancels WorkManager work for jobs Kolibri no longer has. On Android this drift is routine (force-stop, app update, doze, WM dropping work), so the sync stays load-bearing, not optional.
Out of Scope
- Narrowing the membership sync's "missing" check to QUEUED now that
reconcile_stalled_jobshandles the supervised states first — an optional tidy, not done here. - Supervisor registry rows and heartbeats — Android never uses the registry as a liveness source (WorkManager is the authority); reconcile simply clears any rows it finds.
Acceptance Criteria
- [ ] A job executed via WorkManager is owned by its request id for the duration of execution (
supervisor_idset while RUNNING, cleared on completion). - [ ] A RUNNING job whose WorkManager work no longer exists is requeued by reconciliation and re-enqueued into WorkManager via the job hook's QUEUED handling.
- [ ] A CANCELING job whose WorkManager work no longer exists is finalized to CANCELED by reconciliation rather than left stuck.
- [ ] A job whose WorkManager request is retried by WorkManager is not requeued while the retry is pending.
- [ ] RUNNING rows with no owner (left by the pre-update taskworker) are requeued on first reconciliation.
- [ ] Verified on-device: kill the app mid-task and confirm the task resumes after reconciliation runs.
References
kolibri/core/tasks/worker.pyandkolibri/core/tasks/storage.pyin the kolibri repo (supervisor ownership branch; link the PR once open).
AI usage
Drafted with Claude Code from a design discussion during review of the Kolibri-side changes, and revised once that review settled the final API; the API description and integration points were verified against both codebases and edited by hand.
The bot has been waiting for a response for 2 hour(s). It will resume automatically when someone replies.
The bot has been waiting for a response for 2 hour(s). It will resume automatically when someone replies.
The bot has been waiting for a response for 2 hour(s). It will resume automatically when someone replies.
This was fixed in #15051