Skip to content

fix(gateway): pass the session liveness predicate on every worktree gc path - #104146

Closed
ObliviateRickLin wants to merge 1 commit into
openclaw:mainfrom
ObliviateRickLin:fix/worktrees-gc-rpc-liveness-104108
Closed

ObliviateRickLin wants to merge 1 commit into
openclaw:mainfrom
ObliviateRickLin:fix/worktrees-gc-rpc-liveness-104108

Conversation

@ObliviateRickLin

Copy link
Copy Markdown
Contributor

What Problem This Solves

#104108 (P0): the worktrees.gc RPC handler called service.gc() with no parameters, and the CLI openclaw worktrees gc did the same — neither passed the isOwnerActive liveness callback that the scheduled maintenance sweep passes. Session-owned worktrees deliberately take no git worktree lock (registry activity substitutes; only service.acquire() locks, and no session run path calls it), so inside gc() the owner-active guard short-circuits to false whenever the callback is absent. Result: a manual/admin worktrees.gc RPC or CLI run force-removes an idle-expired worktree while its owning session is still live, destroying any uncommitted work — the scheduled sweep, given identical inputs, retains it.

Why This Change Was Made

Per the triage's best-solution shape ("one canonical session-owner activity predicate for scheduled and RPC-triggered GC … and resolve the CLI sibling without creating a second liveness policy"):

  • isManagedWorktreeOwnerActive moves verbatim from server-maintenance.ts into src/gateway/worktree-owner-activity.ts — one canonical implementation (it stays in the gateway layer because it depends on the gateway session-key resolution in session-utils.loadSessionEntry, including legacy-key handling; re-deriving it elsewhere would create the second policy the triage warned about).
  • All three gc() callers now pass it: scheduled maintenance (unchanged behavior, now importing the shared module), the worktrees.gc RPC handler, and the CLI worktrees gc command.
  • Net prod diff is small (predicate moved, two call sites gain the argument); server-maintenance.ts shrinks by the moved lines.

User Impact

An admin running worktrees.gc (RPC or CLI) can no longer delete the working tree — including uncommitted changes — of a session that is actively in use. Workboard/manual worktrees are unaffected (they rely on the git-lock fallback on every path already).

Evidence

Executed real-git before/after (real ManagedWorktreeService + real git worktree + real SQLite registry + real handler from createWorktreesHandlers; session store shows the owner interacted now, registry idle-expired via injected clock, uncommitted wip.txt inside the worktree):

worktrees.gc RPC result uncommitted wip.txt
current main removed: ["c52a19ce-…"] destroyed
this PR removed: [] survives

Regressions (all green):

  • worktree-owner-activity.test.ts (new): live session owner → active; idle-expired session → inactive; non-session owners → false (git-lock fallback owns them); unknown session → false. Forged real session store + runtime config snapshot.
  • server-methods/worktrees.test.ts: the gc handler now must call service.gc({ isOwnerActive: isManagedWorktreeOwnerActive }) (asserted by identity) — fails on prior head where gc() was called bare.
  • agents/worktrees/service.test.ts 27/27 (existing owner-activity semantics unchanged).

Validation: handler + predicate suites 14/14; tsgo:core 0; oxlint/oxfmt clean; session-accessor boundary guard passed.

Scope: #103263 (gc-internals leasing rework) is complementary — it re-checks isOwnerActive at removal time but still depends on callers supplying it, and does not touch the RPC handler or CLI; this PR closes exactly that supply gap, as the issue's sibling analysis notes.

Fixes #104108

🤖 Generated with Claude Code

https://claude.ai/code/session_01AJogTw4aGDiacwqju1uod6

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime cli CLI command changes size: S labels Jul 11, 2026
@harjothkhara

Copy link
Copy Markdown
Contributor

Source review: this is the best current fix for #104108, and it is complementary to #103263 rather than duplicating it.

I checked the full GC caller set on head 67a6b8580d5b: the scheduled maintenance path, worktrees.gc RPC handler, and openclaw worktrees gc CLI are the only production managedWorktrees.gc() callers, and this head passes the same isManagedWorktreeOwnerActive predicate at all three. The extracted predicate is behavior-preserving relative to current scheduled GC, while the submitted real handler + ManagedWorktreeService + SQLite + Git worktree proof exercises the destructive boundary that matters. #103263 improves lease/removal-time behavior inside gc(), but it still depends on callers supplying isOwnerActive, so it does not supersede this caller fix.

No blocking code finding from this pass. The one remaining focused coverage gap is the CLI wiring: the RPC caller is identity-asserted and the predicate has active/stale/non-session/missing-owner tests, but there is no command-level regression proving worktrees gc supplies the callback. A small CLI registration/action test would make the “every path” invariant durable. I also would not call the PR land-ready until the current exact-head CI finishes and the two QA smoke build failures can be inspected; their logs were not yet available while the run remained in progress.

Best-fix verdict: best. Alternatives considered: changing gc() to discover Gateway session state would invert service ownership; relying on #103263 alone leaves the original missing-callback path intact; fixing only RPC would leave the equivalent CLI deletion path open.

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P0 Emergency: data loss, security bypass, crash loop, or unusable core runtime. labels Jul 11, 2026
@clawsweeper

clawsweeper Bot commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 11, 2026, 1:55 AM ET / 05:55 UTC.

Summary
The branch extracts the existing gateway session-owner activity predicate into a shared helper and passes it to scheduled, RPC, and CLI managed-worktree garbage collection.

PR surface: Source +8, Tests +56. Total +64 across 6 files.

Reproducibility: yes. Current source exposes the missing-callback path, and the submitted real-system comparison demonstrates the active session checkout and uncommitted file being removed on current main.

Review metrics: none identified.

Stored data model
Persistent data-model change detected: serialized state: src/gateway/worktree-owner-activity.test.ts. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #104108
Summary: This PR is the direct candidate fix for the canonical missing-callback issue; the leasing PR addresses a complementary active-run removal race rather than superseding this work.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🦞 diamond lobster
Proof: 🦞 diamond lobster
Patch quality: 🦞 diamond lobster
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Next step before merge

  • No automated repair is needed; the remaining action is ordinary maintainer landing review of the clean exact-head patch.

Security
Cleared: The patch changes internal TypeScript caller wiring and tests only, with no dependency, workflow, permission, secret, downloaded-artifact, or supply-chain surface.

Review details

Best possible solution:

Merge the shared-predicate fix after ordinary landing review, preserving one gateway-owned session-liveness policy across every production GC entry point while reviewing the complementary active-run leasing work separately.

Do we have a high-confidence way to reproduce the issue?

Yes. Current source exposes the missing-callback path, and the submitted real-system comparison demonstrates the active session checkout and uncommitted file being removed on current main.

Is this the best way to solve the issue?

Yes. Reusing the established gateway-owned predicate at all callers is narrower and more maintainable than duplicating session-key logic or making the generic worktree service discover gateway session state.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 82abf7244d98.

Label changes

Label justifications:

  • P0: Without this fix, an administrative RPC or CLI cleanup can destroy an actively used session checkout and its uncommitted work.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR provides convincing after-fix live output from the real handler, service, Git worktree, SQLite registry, and session store, including survival of an uncommitted file that current main destroys.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR provides convincing after-fix live output from the real handler, service, Git worktree, SQLite registry, and session store, including survival of an uncommitted file that current main destroys.
Evidence reviewed

PR surface:

Source +8, Tests +56. Total +64 across 6 files.

View PR surface stats
Area Files Added Removed Net
Source 4 33 25 +8
Tests 2 56 0 +56
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 89 25 +64

What I checked:

  • Current-main defect: The gateway RPC handler still calls service.gc() without the session-owner activity callback, so the service cannot retain an idle-expired worktree based on its live session. (src/gateway/server-methods/worktrees.ts:146, 82abf7244d98)
  • CLI sibling defect: The current CLI cleanup action likewise calls bare managedWorktrees.gc(), leaving the same destructive behavior reachable through openclaw worktrees gc. (src/cli/worktrees-cli.ts:118, 82abf7244d98)
  • Service contract: ManagedWorktreeService.gc() only skips idle removal when the optional isOwnerActive callback returns true; absent that callback it proceeds through lock inspection to idle-gc removal. (src/agents/worktrees/service.ts:767, 82abf7244d98)
  • Complete caller fix: The exact PR head has three production GC callers—scheduled maintenance, gateway RPC, and CLI—and all three pass the same isManagedWorktreeOwnerActive predicate. (src/gateway/worktree-owner-activity.ts:8, 60d80017ab41)
  • Focused regression coverage: The branch tests live, expired, non-session, and unknown owners and identity-checks the predicate supplied by the RPC handler. (src/gateway/worktree-owner-activity.test.ts:15, 60d80017ab41)
  • Real behavior proof: The PR body records a before/after run using the real gateway handler, worktree service, Git worktree, SQLite registry, session store, and an uncommitted file: current main removes it, while this head retains it. (60d80017ab41)

Likely related people:

  • steipete: Current-main blame and merged-PR metadata tie the managed-worktree service, GC predicate, gateway method, and CLI surfaces to commit b7156ce6…. (role: introduced current behavior and merger; confidence: high; commits: b7156ce68884; files: src/agents/worktrees/service.ts, src/gateway/server-maintenance.ts, src/gateway/server-methods/worktrees.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.
Review history (1 earlier review cycle)
  • reviewed 2026-07-11T04:14:35.480Z sha 67a6b8580d5b01985a9fdf894090b469b2f7f96f :: needs maintainer review before merge. :: none

…c path

The worktrees.gc RPC handler and the CLI gc command called service.gc() with
no isOwnerActive callback, while the scheduled maintenance sweep passes one.
Session-owned worktrees take no git worktree lock (registry activity
substitutes), so the callback is their only protection: the RPC and CLI paths
force-removed an idle-expired worktree while its owning session was still
live, destroying uncommitted work. The predicate now lives in one canonical
module used by all three callers, with regressions for active/inactive
session owners and the handler wiring.

Fixes openclaw#104108
@ObliviateRickLin
ObliviateRickLin force-pushed the fix/worktrees-gc-rpc-liveness-104108 branch from 67a6b85 to 60d8001 Compare July 11, 2026 05:44
@ObliviateRickLin

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (which now includes 6e9e64bd30 "fix: current-tree package checks support unreleased versions") — this should clear the two QA Smoke CI packing-preflight failures that were caused by the mid-release-train window. No code changes; focused suite green on the refreshed head.

@ObliviateRickLin

Copy link
Copy Markdown
Contributor Author

Re the Persistent data-model change detected: serialized state flag — false positive; no persisted format changes, nothing to migrate.

The new module worktree-owner-activity.ts is a read-only GC liveness predicate: isManagedWorktreeOwnerActive(ownerId) calls loadSessionEntry(ownerId, { clone: false }) and inspects the existing entry to decide whether a managed worktree's owner is still active. It writes nothing and defines no new persisted shape — it consumes the session store that already exists. The RPC/CLI callers just gate worktrees.gc on this predicate.

The fs.writeFileSync(JSON.stringify(...)) in worktree-owner-activity.test.ts is a fixture that stages an owner session in the existing format. No migration needed; existing session/worktree state stays valid.

@steipete steipete self-assigned this Jul 15, 2026
@ObliviateRickLin

Copy link
Copy Markdown
Contributor Author

Closing as superseded by #106224 (df3fe6a4f26, feat(worktrees): configurable cleanup limits). That PR added src/gateway/worktree-owner-activity.ts (isManagedWorktreeOwnerActive) and now wires the liveness predicate into every gc path on current main:

So #104108's reported behavior — a manual worktrees.gc removing a live session's worktree because the RPC handler omitted isOwnerActive — is already fixed on main, with gc/liveness coverage in server-methods/worktrees.test.ts. This PR now conflicts (add/add on worktree-owner-activity.ts) and adds nothing on top, so closing it. Thanks!

@steipete

Copy link
Copy Markdown
Contributor

Follow-up: the regression coverage from this report landed in #107989 as 56ff612606cdf156c4030db20600993f36d4b9a5 after exact-head CI passed: https://github.com/openclaw/openclaw/actions/runs/29390057935

The landed commit preserves RickLin's co-author credit. Thanks @ObliviateRickLin for identifying and patching the liveness gap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes gateway Gateway runtime P0 Emergency: data loss, security bypass, crash loop, or unusable core runtime. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

worktrees.gc RPC handler omits the isOwnerActive liveness callback, so a manual gc removes a live session's worktree

3 participants