cccc icon indicating copy to clipboard operation
cccc copied to clipboard

Task claim/lease with epoch fencing

Open chriscoveries opened this issue 1 month ago • 2 comments

Problem

A board's tasks can be worked by several actors, but the only record of who holds a task is the assignee and handoff_to fields, and the only guard on a peer's write is that the caller is one of those two. That check is "are you the recorded owner", not "do you still hold the work", and the two fields are independent, so a task can name two writable actors at once: while a task is handed from one actor to another, the outgoing assignee and the incoming handoff target are both authorized, and both writes succeed -- absent an explicit group-wide if_version, the last one wins -- with no per-task ordering or fencing between them. The same gap leaves a stale actor writable: a task whose handoff_to still names an actor who has moved on keeps accepting that actor's writes, and if that actor re-acquires the task later, an old in-flight request from the earlier hold is indistinguishable from a current one. What is missing is exclusion (one holder at a time), visibility (who holds what, and since when) and fencing (a write from a previous holder is rejected, not merged).

What cccc does today

  • Task state is a free-form field map, not a typed struct, so a new field is additive -- crates/cccc-core/src/context/model.rs:19.
  • task.update writes a fixed allow-list that includes assignee and handoff_to but no claim, lease or epoch field -- crates/cccc-core/src/context/task_apply.rs:73-90.
  • task.create copies every field of the request onto the new task, so a caller-supplied claim or claim_epoch would land on the task unless the daemon strips it -- crates/cccc-core/src/context/task_apply.rs:24-28.
  • A peer's task.update/task.move is allowed if the caller matches either assignee or handoff_to, so one task can be writable by two actors at once; a caller matching neither is refused with permission_denied -- crates/cccc-daemon/src/ops/context.rs:200-209.
  • task.move takes only task_id and status, so "I am taking this" cannot be recorded atomically with the write that follows -- crates/cccc-core/src/context/task_apply.rs:110-124.
  • Concurrency is group-wide and all-or-nothing: if_version / ctxv:<revision>, version_conflict on mismatch -- crates/cccc-core/src/context/store.rs:38, :72-75.

Proposal

Two daemon-owned task fields and four new context_sync ops, so a claim and the write it guards are one atomic batch:

claim_epoch: u64            // daemon-owned; monotonic; bumped on every holder change
claim: { holder, state: "reserved"|"active", by, since, expires_at?, meta? }
task.reserve { task_id, actor, ttl }   // take a hold
task.claim   { task_id }               // reserved -> active
task.renew   { task_id, ttl }          // extend expires_at
task.release { task_id, reason }       // give it up

claim and claim_epoch are daemon-owned: task.create ignores any caller-supplied claim or claim_epoch, and the daemon initialises claim_epoch to 0 on a new task, so the first holder change takes it to 1. Each op takes an optional if_claim_epoch; a stale epoch fails with claim_conflict naming the current epoch and holder. Any change of holder bumps the epoch; reserved -> active and renew do not. When the epoch is required: a write to a task that currently has a live claim -- any task.update or task.move on it, by the holder or anyone else -- MUST carry if_claim_epoch equal to the task's current claim_epoch, and is refused with claim_conflict if it is absent or does not match. That is what distinguishes an old in-flight request from a current one: an actor that released the task and later re-acquires it gets a new epoch, so its earlier request, carrying the old epoch, is refused rather than landing. The claim ops themselves take the epoch optionally, as an optimistic check on the state they are replacing. While a claim is live, peer task.update and task.move are refused for everyone but the holder; with no live claim the existing assignee/handoff check applies unchanged. Reserve is foreman or user; task.claim takes no actor parameter and only the current reservation holder may activate its own reservation, so it is the holder and nobody else; renew is the holder; release is the holder or, after expiry, the assignee, foreman or user. One ledger event per transition, plus an attention=claim_expired filter -- the daemon reports expiry but never acts on it. One batch: task.reserve { task_id, actor, ttl } then a task.update carrying if_claim_epoch: 7; if another actor reserved in between, the batch is refused with claim_conflict and nothing is written.

Alternatives. if_version alone (group-wide, says nothing about ownership); assignee or handoff_to as the claim (both are responsibility fields that outlive the actor, and they can name two writable actors at once); lease expiry without an epoch (a paused holder's late write is indistinguishable from a current one); a lock file per task (state outside the context and the ledger, not batchable with the write it guards).

Out of scope. Who should hold what and for how long; automatic reassignment, release or notification on expiry; claiming anything but a task; per-task revision tokens; any change to how assignee or handoff_to is set, read or filtered.

We run this today in our own board tooling on top of cccc, and would send a PR if the direction suits.

chriscoveries avatar Oct 22 '26 06:10 chriscoveries

Thanks for spelling out the distinction between task responsibility and an exclusive hold. That distinction is useful.

Current main now supports per-task rev / if_task_rev, so stale writes no longer require a whole-context version check. We also verified that an incoming peer can accept a handoff in one guarded update: assign the task to itself and clear handoff_to. The outgoing peer then loses write permission, and an old guarded request is rejected even if that peer later reacquires the task.

These mechanisms protect task records; they do not provide exclusive execution or fence filesystem and deployment effects.

Before introducing reserve/claim/renew/release, could you describe a concrete workflow that still needs a daemon-owned lease after using task revisions and explicit ownership transfer? In particular, which write needs stronger fencing?

Please hold off on implementing the lease operations for now. I’d like to establish that remaining requirement before adding another lifecycle to the core.

ChesterRa avatar Oct 23 '26 07:10 ChesterRa

Thanks for asking us to ground this in a concrete workflow. We reviewed the incidents we could verify. The reservation case was a state and authority defect; the dispatch case was a failure to follow an explicit handoff target. The deployment evidence describes partial version rollout across hosts. None of these records shows concurrent writes to the same task or a former holder continuing filesystem or deployment effects after losing ownership. Those cases need corrections at the reservation, dispatch, or deployment boundary.

We do not have a concrete workflow that still needs a daemon-owned lease after task revisions and guarded ownership transfer. We agree this request should be closed with thanks. If a future integration identifies a specific external write that needs fencing, we can bring that bounded case separately.

chriscoveries avatar Oct 24 '26 21:10 chriscoveries