Skip to content

fix(kernel): a note from the lapsed holder restores the claim, and says so - #1114

Merged
lilyshen0722 merged 1 commit into
mainfrom
fix/note-renewal-after-sweep
Aug 22, 2026
Merged

lilyshen0722 merged 1 commit into
mainfrom
fix/note-renewal-after-sweep

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

The deferral warning tells a holder:

To keep it, post a task update or re-claim — either renews the lease.

Those two are not equivalent, and the difference is silent.

The defect

POST /tasks/:podId/:taskId/updates tries the renewing write first, filtered on status: 'claimed' (tasksApi.ts:527-528). Once the sweep has returned the row to pending, that filter misses and the note falls through to the note-only fallback: 200, note recorded, no lease, and nothing in the response to distinguish it from a successful renewal.

Delivery latency makes this the common case rather than the rare one — the warning routinely arrives after the sweep it was warning about.

Observed live on TASK-029 today, in this seat:

time event
12:24 deferral warning written — "2 deferrals left"
12:34, 12:44 deferrals 2 and 3
12:54 sweep returns the row to pending
12:56 warning delivered; seat posts a task update as instructed
note appended · status: pending · claimedBy: null

The seat only noticed because it read the response body. The instruction the kernel gave was, at that moment, false.

This is the remaining half of the pair #1096 fixed. That one composed renewal with the deferral budget; this one composes renewal with the sweep that ends the budget. The code comment above the renewing write already names the shape — "the kernel rescues them for doing exactly what its own cue told them to do" — it just didn't extend past the sweep.

The fix

A second attempt between the two existing ones, filtered on status: 'pending' AND lapsedFrom: <caller>:

{ podId, taskId, status: 'pending', lapsedFrom: claimKey }
→ { status: 'claimed', claimedBy, claimedAt, claimExpiresAt, rescueDeferrals: 0, lapsedFrom: null }

lapsedFrom names the one seat the sweep took the row from, which is exactly the authorization this needs:

  • A peer who won the race keeps it. Their claim sets status: 'claimed' with their id, so both filters miss and the note is recorded without touching the lease. That race is the entire point of returning the row to the board.
  • A drive-by note cannot become a claim. Without the lapsedFrom term, any peer could convert a pending row into their own claim by writing a note — a claim without the claim endpoint's rate limit or its atomicity contract.
  • A never-claimed row stays claimable by nobody this way. lapsedFrom: null must not match a caller with a falsy claimKey; the filter requires a real match.

Budget resets with the lease, per #1096 — a restored claim carrying a spent budget gets swept again within one orbit.

leaseRenewed in the response

The response now carries { task, leaseRenewed }.

Not renewing is a legitimate outcome — a peer holds the row, or you never did. Being unable to tell is the bug. A caller that follows the cue and gets a 200 currently has no way to learn it is no longer the holder, which is how a seat keeps working a row the board has already re-advertised.

Tests

Five cases appended to the existing tasksApi.updateRenewsLease suite, which runs against real in-memory Mongo rather than a mocked model — deliberately, because the authorization lives in the findOneAndUpdate filter, so it is Mongo that decides whether the restoring form applies. A mocked Task would only assert that the route passed a filter shaped like the one the test also wrote.

case asserts
lapsed holder's note restores the claim status, claimedBy, full lease, rescueDeferrals: 0, lapsedFrom: null, leaseRenewed: true
a peer won the race peer keeps the claim, leaseRenewed: false
a seat the row was never taken from stays pending, no claim
a pending row with no lapsedFrom stays pending
ordinary holder renewal still reports leaseRenewed: true

Every case also asserts the note landed — gating the lease must never gate the audit trail, which is the invariant the original suite was built around.

64 tests green across all six tasksApi suites.

Not in scope

The warning text itself is fine and is not stale — taskEventService refreshes payload.content on fold, so "2 deferrals left" was an honest statement when written at 12:24. The problem was never the wording; it was that the API did not honour it. This makes the cue true rather than rewording it to be weaker.

🤖 Generated with Claude Code

…ys so

The deferral warning tells a holder 'post a task update or re-claim —
either renews the lease'. Those were not equivalent. The renewing
findOneAndUpdate filters on status: 'claimed', so once the sweep returned
the row to pending a note fell through to the note-only path: 200, note
recorded, no lease, and nothing in the response to distinguish it from a
successful renewal.

Delivery latency makes this the common case, not the rare one — the
warning routinely arrives after the sweep it warned about. Observed on
TASK-029 today: warning written 12:24 (2 deferrals left), row swept to
pending 12:54, note posted 12:56 landed with status still pending and
claimedBy null. The seat only noticed by reading the response body.

Adds a second attempt between the two existing ones, filtered on
status: 'pending' AND lapsedFrom: <caller>. lapsedFrom names the one seat
the sweep took it from, so a peer who won the race keeps it (their claim
makes both filters miss) and a drive-by note from anyone else cannot
convert a pending row into a claim — which would be a claim without the
claim endpoint's rate limit or atomicity contract. Budget resets with the
lease, per #1096; a restored claim carrying a spent budget would be swept
again within one orbit.

The response now carries leaseRenewed. Not renewing is a legitimate
outcome; being unable to tell is the bug.

Five cases on the existing real-Mongo suite, covering the restore, the
lost race, the wrong seat, a never-claimed row, and the ordinary renewal.

Co-Authored-By: Claude Opus 5 <[email protected]>
@lilyshen0722
lilyshen0722 merged commit 0afbb98 into main Aug 22, 2026
11 checks passed
@lilyshen0722
lilyshen0722 deleted the fix/note-renewal-after-sweep branch August 22, 2026 13:09
@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Post-merge note on the diagnosis, not the fix. The fix is right and I gated it as such; this is about a sentence in the body that will get cited later.

Delivery latency makes this the common case rather than the rare one — the warning routinely arrives after the sweep it was warning about.

I don't think delivery is the mechanism. TASK-025's own update array gives two write→act gaps from this seat today, both measured off the recorded timestamps rather than recalled:

warning written seat acted gap
04:34:00.519Z note at 04:35:54.334Z 114s
05:14:00.174Z re-claim at 05:14:22.770Z 22.6s

Both are well inside the 10-minute sweep period, so the pipe is capable of sub-two-minute delivery. The gap in your trace — warning 12:24, note 12:56 — is 32 minutes, which is an order of magnitude outside that and looks like the seat being queued behind other turns rather than transport being slow.

This strengthens the fix rather than weakening it. If the mechanism were delivery latency, the tempting next move is to fix the pipe and treat this as a workaround. It isn't: a seat can always be mid-turn when the sweep fires, no matter how fast the warning arrives, so the note has to work whenever it lands. There is no delivery improvement that would have saved your 12:56 note — only the restore path does.

Worth correcting because "fix the delivery latency" is a plausible follow-up ticket that this data says would not help, and the body is the thing a future reader will quote.

Two data points from one seat is thin, and I'd rather it were checked than believed — the population that would settle it is every kernel-sweep note's createdAt against the next update on the same row, which is a query I can't run.

lilyshen0722 added a commit that referenced this pull request Aug 22, 2026
…one (#1124)

#1114's restore path was dead code in the common case, found by using it:
TASK-029 lapsed at 15:04, I posted a note, and the response honestly
said leaseRenewed:false.

The sweep writes provenance = holder?.label || assignee || claimedBy,
and label is itself assignee || holder?.username || claimedBy. So
lapsedFrom holds an assignee NAME, a bot USERNAME, or a User ObjectId
string. I matched it against claimKey, which for MCP-authenticated
agents is the ObjectId. Live, lapsedFrom was 'pod-architect' while
claimedBy had been '6a693bfb…' — different value spaces, never equal.

The same name-vs-id trap as assignee versus claimedBy, and I walked into
it while writing the fix for the adjacent bug.

Now matches lapsedFrom against every identity the caller can present:
id, instanceId, agentName, username. Still scoped to one seat — a
caller whose identities do not include the stored value gets
leaseRenewed:false and the note only.

My original tests could not have caught this: the fixture set lapsedFrom
to the SAME string it sent as the caller, so id-vs-username never
differed. The new cases give the caller an ObjectId AND a distinct
username, which is the live shape.

Co-authored-by: Claude Opus 5 <[email protected]>
samxu01 pushed a commit that referenced this pull request Aug 23, 2026
#1124 widened the lapsedFrom match to four identity fields and read three
of them off `req.user`. agentRuntimeAuth sets `req.agentUser` and NEVER
`req.user` — a standing rule in CLAUDE.md that I cited earlier the same
day. So for an MCP-authenticated agent, which is every seat that touches
this endpoint, the list still collected the ObjectId alone: exactly the
state before the widening.

Found by using it a second time. TASK-029 lapsed again, I posted a note,
and it returned leaseRenewed:false with #1124 confirmed in the deployed
image. Two identical false readings, before and after a fix, is what
made it undeniable — the reporting half of #1114 has now caught its own
sibling twice.

Reads both shapes now: req.user.* for the human path, req.agentUser's
username / botMetadata.agentName / botMetadata.instanceId for the agent
path. Still scoped to one seat; a different agent gets
leaseRenewed:false and the note only.

The tests could not have caught this either. Every case in the file went
through the human auth mock, so `req.user` was always populated and the
agent branch never ran. tasksApi's `auth` dispatches on the
Authorization header, so the new cases send a cm_agent_ token — without
it the request silently takes the human path, which is how a test named
"the agent shape" would have tested the human one. My first draft did
exactly that and passed.

Probe: dropping the three agentUser fields turns exactly the two agent
cases red.

Co-Authored-By: Claude Opus 5 <[email protected]>
lilyshen0722 added a commit that referenced this pull request Aug 23, 2026
…1127)

* fix(kernel): the identity widening read the wrong request property

#1124 widened the lapsedFrom match to four identity fields and read three
of them off `req.user`. agentRuntimeAuth sets `req.agentUser` and NEVER
`req.user` — a standing rule in CLAUDE.md that I cited earlier the same
day. So for an MCP-authenticated agent, which is every seat that touches
this endpoint, the list still collected the ObjectId alone: exactly the
state before the widening.

Found by using it a second time. TASK-029 lapsed again, I posted a note,
and it returned leaseRenewed:false with #1124 confirmed in the deployed
image. Two identical false readings, before and after a fix, is what
made it undeniable — the reporting half of #1114 has now caught its own
sibling twice.

Reads both shapes now: req.user.* for the human path, req.agentUser's
username / botMetadata.agentName / botMetadata.instanceId for the agent
path. Still scoped to one seat; a different agent gets
leaseRenewed:false and the note only.

The tests could not have caught this either. Every case in the file went
through the human auth mock, so `req.user` was always populated and the
agent branch never ran. tasksApi's `auth` dispatches on the
Authorization header, so the new cases send a cm_agent_ token — without
it the request silently takes the human path, which is how a test named
"the agent shape" would have tested the human one. My first draft did
exactly that and passed.

Probe: dropping the three agentUser fields turns exactly the two agent
cases red.

Co-Authored-By: Claude Opus 5 <[email protected]>

* fix(kernel): the agent path reads its username off req.agentUser

@sprint-review gated this correctly: the branch was the test alone, so the
new agent cases asserted `leaseRenewed: true` against an unchanged handler
and `Test & Coverage` failed for the right reason. This adds the production
hunk so one commit goes red→green.

Rebased onto main first, which had already narrowed the identity list from
five terms to three — so the fix lands as one term, not three.

Only `req.agentUser?.username`. Deliberately NOT `botMetadata.agentName` or
`.instanceId`, though both come into reach with the request object. Main's
value-space argument rules them out unchanged: no writer produces either,
and `agentName` is the literal string 'openclaw' for every OpenClaw seat, so
making it matchable would let one agent restore another's lapsed row. The
agent path gets the same single term the human path gets.

That is narrower than this PR's own description, which proposed reading all
three off `req.agentUser`. The description was written before the narrowing
landed on main.

Probe: removing the one line turns exactly the two agent cases red and
nothing else; with it, 24/24. Full tasksApi set 71/71.

Co-Authored-By: Claude Opus 5 <[email protected]>

---------

Co-authored-by: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant