Skip to content

fix(skills): refresh stale session skill snapshots after restart - #69716

Closed
JinluZhang wants to merge 1 commit into
openclaw:mainfrom
JinluZhang:fix/refresh-stale-skill-snapshot
Closed

JinluZhang wants to merge 1 commit into
openclaw:mainfrom
JinluZhang:fix/refresh-stale-skill-snapshot

Conversation

@JinluZhang

Copy link
Copy Markdown

Summary

Fixes a stale-session edge case where newly added skills can stay invisible after a gateway restart.

Fixes #69715.

What Changed

  • seed a non-zero skills snapshot version when the first workspace watcher is attached
  • read snapshotVersion after ensureSkillsWatcher() in ensureSkillSnapshot()
  • add regression coverage for both behaviors

Why

A persisted session snapshot with version = 0 could compare equal to the fresh process state after restart, so the next turn would skip rebuilding the snapshot. In practice that let existing sessions miss newly added workspace skills until the session was reset or otherwise refreshed.

Testing

  • git diff --check
  • Added focused unit tests in:
    • src/agents/skills/refresh.test.ts
    • src/auto-reply/reply/session-updates.test.ts
  • Not run locally: this checkout does not have node_modules, vitest, or pnpm installed, so node scripts/run-vitest.mjs ... fails with Cannot find module 'vitest/package.json'.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Apr 21, 2026
@greptile-apps

greptile-apps Bot commented Apr 21, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a stale-session edge case by (1) bumping the per-workspace skills snapshot version to a non-zero value the first time a watcher is attached and (2) reading that version after ensureSkillsWatcher returns. The production logic is correct, but the regression test for the ordering fix mocks getSkillsSnapshotVersion under ../../agents/skills/refresh.js while session-updates.ts actually imports it from ../../agents/skills/refresh-state.js, so the snapshotVersion: 123 assertion will fail when the suite is run.

Confidence Score: 4/5

Production fix is sound but the new regression test needs a mock correction before the suite will pass.

The core logic change (ordering + initial version seeding) is correct and well-reasoned. The P1 finding is confined to the test: getSkillsSnapshotVersionMock is wired to the wrong module, so the key assertion will fail when tests are actually run. Fixing the mock target is a small change but necessary for the test to serve its purpose.

src/auto-reply/reply/session-updates.test.ts — mock for getSkillsSnapshotVersion targets refresh.js instead of refresh-state.js

Comments Outside Diff (2)

  1. src/auto-reply/reply/session-updates.test.ts, line 36-40 (link)

    P1 Mock targets wrong module — test won't verify the fix

    session-updates.ts imports getSkillsSnapshotVersion (and shouldRefreshSnapshotForVersion) from ../../agents/skills/refresh-state.js, but the mock here only stubs the refresh.js namespace. When ensureSkillsWatcherMock fires and calls getSkillsSnapshotVersionMock.mockReturnValue(123), it changes the mock on the wrong module; the real getSkillsSnapshotVersion from refresh-state.js is still called and returns 0. The assertion on line 129 (snapshotVersion: 123) will therefore fail at runtime.

    The fix is to also mock ../../agents/skills/refresh-state.js:

    vi.mock("../../agents/skills/refresh-state.js", () => ({
      getSkillsSnapshotVersion: getSkillsSnapshotVersionMock,
      shouldRefreshSnapshotForVersion: shouldRefreshSnapshotForVersionMock,
    }));

    and remove the two stubs from the refresh.js mock factory (or keep them for tree-shake reasons but rely on the refresh-state.js mock for the actual calls).

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: src/auto-reply/reply/session-updates.test.ts
    Line: 36-40
    
    Comment:
    **Mock targets wrong module — test won't verify the fix**
    
    `session-updates.ts` imports `getSkillsSnapshotVersion` (and `shouldRefreshSnapshotForVersion`) from `../../agents/skills/refresh-state.js`, but the mock here only stubs the `refresh.js` namespace. When `ensureSkillsWatcherMock` fires and calls `getSkillsSnapshotVersionMock.mockReturnValue(123)`, it changes the mock on the wrong module; the real `getSkillsSnapshotVersion` from `refresh-state.js` is still called and returns `0`. The assertion on line 129 (`snapshotVersion: 123`) will therefore fail at runtime.
    
    The fix is to also mock `../../agents/skills/refresh-state.js`:
    
    ```typescript
    vi.mock("../../agents/skills/refresh-state.js", () => ({
      getSkillsSnapshotVersion: getSkillsSnapshotVersionMock,
      shouldRefreshSnapshotForVersion: shouldRefreshSnapshotForVersionMock,
    }));
    ```
    
    and remove the two stubs from the `refresh.js` mock factory (or keep them for tree-shake reasons but rely on the `refresh-state.js` mock for the actual calls).
    
    How can I resolve this? If you propose a fix, please make it concise.
  2. src/agents/skills/refresh.ts, line 108-117 (link)

    P2 Version not seeded when watch is disabled

    When watchEnabled is false and no existing watcher is present, the function returns early before the version-seeding block. A session with a persisted version: 0 snapshot that connects to a gateway with skills.load.watch: false will still skip the stale-snapshot rebuild, since getSkillsSnapshotVersion remains 0 and shouldRefreshSnapshotForVersion(0, 0) returns false.

    If the intent is to cover the watch-disabled path too, a minimal seeding call could be placed before the early return:

    if (!watchEnabled) {
      if (existing) { /* close existing */ }
      // Still seed so restarted sessions with version 0 rebuild their snapshot.
      if (getSkillsSnapshotVersion(workspaceDir) === 0) {
        bumpSkillsSnapshotVersion({ workspaceDir, reason: "manual" });
      }
      return;
    }
    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: src/agents/skills/refresh.ts
    Line: 108-117
    
    Comment:
    **Version not seeded when watch is disabled**
    
    When `watchEnabled` is `false` and no existing watcher is present, the function returns early before the version-seeding block. A session with a persisted `version: 0` snapshot that connects to a gateway with `skills.load.watch: false` will still skip the stale-snapshot rebuild, since `getSkillsSnapshotVersion` remains `0` and `shouldRefreshSnapshotForVersion(0, 0)` returns `false`.
    
    If the intent is to cover the watch-disabled path too, a minimal seeding call could be placed before the early return:
    
    ```typescript
    if (!watchEnabled) {
      if (existing) { /* close existing */ }
      // Still seed so restarted sessions with version 0 rebuild their snapshot.
      if (getSkillsSnapshotVersion(workspaceDir) === 0) {
        bumpSkillsSnapshotVersion({ workspaceDir, reason: "manual" });
      }
      return;
    }
    ```
    
    How can I resolve this? If you propose a fix, please make it concise.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/auto-reply/reply/session-updates.test.ts
Line: 36-40

Comment:
**Mock targets wrong module — test won't verify the fix**

`session-updates.ts` imports `getSkillsSnapshotVersion` (and `shouldRefreshSnapshotForVersion`) from `../../agents/skills/refresh-state.js`, but the mock here only stubs the `refresh.js` namespace. When `ensureSkillsWatcherMock` fires and calls `getSkillsSnapshotVersionMock.mockReturnValue(123)`, it changes the mock on the wrong module; the real `getSkillsSnapshotVersion` from `refresh-state.js` is still called and returns `0`. The assertion on line 129 (`snapshotVersion: 123`) will therefore fail at runtime.

The fix is to also mock `../../agents/skills/refresh-state.js`:

```typescript
vi.mock("../../agents/skills/refresh-state.js", () => ({
  getSkillsSnapshotVersion: getSkillsSnapshotVersionMock,
  shouldRefreshSnapshotForVersion: shouldRefreshSnapshotForVersionMock,
}));
```

and remove the two stubs from the `refresh.js` mock factory (or keep them for tree-shake reasons but rely on the `refresh-state.js` mock for the actual calls).

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/agents/skills/refresh.ts
Line: 108-117

Comment:
**Version not seeded when watch is disabled**

When `watchEnabled` is `false` and no existing watcher is present, the function returns early before the version-seeding block. A session with a persisted `version: 0` snapshot that connects to a gateway with `skills.load.watch: false` will still skip the stale-snapshot rebuild, since `getSkillsSnapshotVersion` remains `0` and `shouldRefreshSnapshotForVersion(0, 0)` returns `false`.

If the intent is to cover the watch-disabled path too, a minimal seeding call could be placed before the early return:

```typescript
if (!watchEnabled) {
  if (existing) { /* close existing */ }
  // Still seed so restarted sessions with version 0 rebuild their snapshot.
  if (getSkillsSnapshotVersion(workspaceDir) === 0) {
    bumpSkillsSnapshotVersion({ workspaceDir, reason: "manual" });
  }
  return;
}
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "fix(skills): refresh stale session skill..." | Re-trigger Greptile

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3b60d3f59d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +111 to +113
ensureSkillsWatcherMock.mockImplementation(() => {
getSkillsSnapshotVersionMock.mockReturnValue(123);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Badge Mock refresh-state for snapshot-version assertion

ensureSkillSnapshot reads getSkillsSnapshotVersion from src/agents/skills/refresh-state.js, so changing getSkillsSnapshotVersionMock inside ensureSkillsWatcherMock here does not affect the value used by production code. In this test path (OPENCLAW_TEST_FAST=0), the snapshot version stays at the real default (0), so the new expectation for snapshotVersion: 123 fails deterministically instead of validating the intended ordering behavior.

Useful? React with 👍 / 👎.

@vincentkoc

Copy link
Copy Markdown
Member

Thanks @JinluZhang. This found the right failure mode: persisted skills snapshots can stay at version: 0 across a restart and hide newly added skills.

I opened the canonical maintainer PR at #71497. It keeps the same root-cause fix, but moves the version seed into the shared skills refresh-state layer so both the gateway reply path and the CLI agent path refresh once after process restart.

Closing this as superseded/duplicate so the cluster has one merge target. Your contribution is referenced from the canonical PR.

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

Labels

agents Agent runtime and tooling close:duplicate Closed as duplicate dedupe:child Duplicate issue/PR child in dedupe cluster size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: stale skillsSnapshot can hide newly added skills after restart

2 participants