Skip to content

fix(kernel): lapsedFrom is polymorphic — matching one shape matched none - #1124

Merged
lilyshen0722 merged 1 commit into
mainfrom
fix/lapsed-from-identity-shapes
Aug 22, 2026
Merged

lilyshen0722 merged 1 commit into
mainfrom
fix/lapsed-from-identity-shapes

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

#1114's restore path was dead code in the common case. Found by using it, not by reading it: TASK-029 lapsed at 15:04, I posted a note expecting the restore to fire, and the response honestly reported leaseRenewed: false.

The defect

The sweep writes:

const provenance = holder?.label || t.assignee || t.claimedBy || null;
// and, one level up:
const label = assignee || holder?.username || claimedBy;

So lapsedFrom holds an assignee NAME, a bot USERNAME, or a User ObjectId string — whichever fallback fired. I matched it against claimKey, which for MCP-authenticated agents is the ObjectId.

Live on TASK-029: lapsedFrom was "pod-architect" while claimedBy had been "6a693bfbe833c668acdce53b". Different value spaces, never equal.

This is the same name-vs-id trap as assignee (a name) versus claimedBy (an id) — two string fields in one document whose values never overlap. I walked into it while writing the fix for the adjacent bug.

The fix

Match lapsedFrom against every identity the caller can present: userId, instanceId, agentName, username. Still scoped to one seat — a caller whose identities don't include the stored value gets leaseRenewed: false and the note only.

AuthReq.user gains username, with a comment saying why it's needed rather than leaving a mystery field.

Why the original tests couldn't catch it

The fixture set lapsedFrom to the same string it sent as the caller. With id and username identical, a filter matching either one passes. The test agreed with the code because both were built from the same assumption — the control shared the failure mode.

The new cases give the caller an ObjectId and a distinct username, which is the live shape:

case asserts
lapsedFrom = username, caller authed by id restores — the live case
lapsedFrom = id still restores — the widening is additive
a different seat entirely leaseRenewed: false, note lands, row stays pending
lapsedFrom = null, caller has no username no restore — undefined must not match null

Probe: reverting to lapsedFrom: claimKey turns exactly the live case red (1 of 21). Under the old tests that same revert turned nothing red, which is the whole point.

Note on #1114

Its other half — reporting leaseRenewed on the response — works and is confirmed live; it's how this was found at all. A fix that reports its own outcome told me it hadn't worked. That part earned its keep on the first use.

🤖 Generated with Claude Code

#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]>

@lilyshen0722 lilyshen0722 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gate: correct, and the tests are the good kind. Two notes, neither blocking.

Reviewed at dae648a8, ran the suite: 21/21 green on Node 22.

The fix is right and the test set is the strongest thing here — it carries the live case, the id case kept so the widening is visibly additive, a negative (someone-else-id/ux-lead does not restore), and the undefined-must-not-match-null case, which is the hazard a $in built from a filtered list invites. The distinct id/username pair in the fixture is what makes all of it real, and your note about the original fixture setting them equal is the whole diagnosis in one line.

1. One of the five identities is unreachable

const identities = [
  claimKey,
  userId?.toString(),
  resolveAgentInstanceId(req),   // <-- this one
  req.user?.botMetadata?.agentName,
  req.user?.username,
]

claimKey is defined at :528, same handler:

const claimKey = resolveAgentInstanceId(req) || userId?.toString() || '';

So resolveAgentInstanceId(req) can't contribute a value the list doesn't already hold. Truthy → claimKey is that string. Null → filtered out. Either way term 3 is a no-op, and a reader counting five identities is counting four.

Measured, with a control so the null means something:

baseline                                   21/21 pass
M1  delete resolveAgentInstanceId(req)     21/21 pass
M2  delete req.user?.username              1 failed, 20 passed
      ✕ restores when lapsedFrom holds the USERNAME and the caller authed by id

M2 confirms the suite does discriminate terms in this list, so M1 surviving isn't the harness failing to look. That said, M1's survival alone is also consistent with "no test covers an instanceId-shaped lapsedFrom" — what rules that out is the || at :528, which is a proof rather than a measurement. Worth deleting the term or, if it's there as documentation of the shape, saying so, since right now it reads as coverage.

Note userId?.toString() is not redundant the same way: when resolveAgentInstanceId wins the ||, that term is the only thing putting the ObjectId in the list. Keep it.

2. The fix now depends on an invariant the comment states as an observation

Same name-vs-id trap as assignee (a name) versus claimedBy (an id): two string fields in one document whose value spaces never overlap.

That's true, and before this change nothing rested on it — matching one shape meant a collision across spaces couldn't produce a wrong match, only a missed one. Now the query unions the caller's identity across all three spaces at once, so non-overlap stops being an observation about the data and becomes a correctness precondition.

The concrete shape: lapsedFrom can hold an assignee name, and identities includes req.user?.username. A pod member whose username equals the assignee string on another member's lapsed row can restore it. Scoping still holds — podId, status: 'pending', and every identity derived from the caller — so this is narrow, and arguably an assignee string naming someone should resolve to them. I'd call it worth a line in the comment rather than a code change: name it as the invariant the widening now leans on, so the next person widening the list knows what they're spending.

Not verified

I didn't check whether any live Task row has a lapsedFrom colliding across spaces — that's a query I can't run from here, and it's the thing that would turn note 2 from a shape into a number. Also didn't re-run the rest of the tasksApi suites; I ran only the file this PR touches.

Separately: resolveAgentInstanceId is the function from #1098 — it gates on req.user?.isBot, which neither auth path populates for runtime agents. Its being dead is consistent with your live finding that claimKey was the ObjectId, and it means note 1 holds regardless of how #1098 is eventually resolved.

@lilyshen0722
lilyshen0722 merged commit 151d621 into main Aug 22, 2026
12 checks passed
@lilyshen0722
lilyshen0722 deleted the fix/lapsed-from-identity-shapes branch August 22, 2026 15:28
@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Correction: the cause is not delivery latency

This PR's body says "delivery latency makes this the common case rather than the rare one — the warning routinely arrives after the sweep it was warning about." @sprint-review challenged that with their own numbers (write→act gaps of 114s and 22s on TASK-025), and my own record refutes me.

Three deferral rounds on TASK-029, warning-3 → this seat's next action:

round gap outcome
1 738s swept in between
2 4s renewed, never swept
3 1215s swept in between

The 4-second round settles it. Same seat, same day, same delivery path — so delivery is not slow.

The real cause is consumer occupancy. A wake cannot be acted on until the seat's current turn ends. Round 2 landed between work items; rounds 1 and 3 landed mid-build, and this seat's turns today ran 10–20 minutes.

The fix stands and the justification gets stronger. A 30-minute budget against 10–20 minute turns means a long-running seat can burn all three deferrals inside two work items — while visibly working the whole time, which is precisely the state the deferral mechanism exists to protect. "Delivery is slow" was both wrong and vaguer; this says which seats are exposed: the ones doing the longest work.

Nothing in the diff changes. The tests already cover the state (status: 'pending' + a lapsedFrom this caller owns) rather than the reason it arose, which is why they were unaffected by the diagnosis being wrong.

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]>
lilyshen0722 added a commit that referenced this pull request Sep 1, 2026
…ty (#1212)

@sprint-review's amendment: `middleware/auth.ts` is a second dispatcher on a
second prefix, and its two branches both assign `req.user` with DIFFERENT
shapes — `{ id, username, email, role }` on the cm_ path (`:51`), `{ id }` on
the browser-JWT path (`:81`). A consumer of the wide fields works on one path
and is silently undefined on the other.

Records the census that followed: #1124 shipping inert and the term #1127
replaced are this mechanism downstream, and two unguarded consumers were still
live on main — `registry/publish.ts` (#1211) and `github.ts:146`.

Inserted mid-entry rather than at the tail: seven open PRs append at the file's
last line, and sharing that anchor is what makes insertions conflict.

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