Skip to content

Infra: fix stale skills snapshot after gateway restart - #48500

Closed
fif911 wants to merge 2 commits into
openclaw:mainfrom
fif911:fix/stale-skills-snapshot-on-restart
Closed

fif911 wants to merge 2 commits into
openclaw:mainfrom
fif911:fix/stale-skills-snapshot-on-restart

Conversation

@fif911

@fif911 fif911 commented Mar 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: Skills installed while the gateway is stopped are invisible to agents after restart. Existing sessions keep their stale skills snapshot indefinitely.
  • Why it matters: Users must manually delete session entries from sessions.json to pick up new skills — a confusing workaround that shouldn't be needed.
  • What changed: globalVersion in src/agents/skills/refresh.ts initialized to Date.now() instead of 0, plus a regression test.
  • What did NOT change (scope boundary): No changes to session storage format, watcher logic, or version comparison semantics.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

  • Related: none found

User-visible / Behavior Changes

After a gateway restart, agents in existing sessions now correctly see newly installed skills without requiring manual deletion of session entries from sessions.json.

Security Impact (required)

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? No
  • Command/tool execution surface changed? No
  • Data access scope changed? No

Repro + Verification

Environment

  • OS: macOS (reproducible on any platform)
  • Runtime/container: Node 22+, dev source mode
  • Model/provider: any
  • Integration/channel: any

Steps

  1. Start gateway, send a message to an agent (creates session with skillsSnapshot.version: 0)
  2. Stop gateway
  3. Install a new skill (add SKILL.md to workspace skills dir)
  4. Restart gateway
  5. Send another message in the same session

Expected

  • Agent sees and can use the newly installed skill

Actual

  • Agent uses stale snapshot, new skill is invisible. The refresh guard snapshotVersion > 0 evaluates to false because globalVersion starts at 0.

Evidence

  • Failing test/log before + passing after

The new regression test (getSkillsSnapshotVersion > initial globalVersion is > 0) fails with the old let globalVersion = 0 and passes with Date.now().

Root cause trace:

  • session-updates.ts:185: snapshotVersion > 0 && (nextEntry?.skillsSnapshot?.version ?? 0) < snapshotVersion
  • getSkillsSnapshotVersion() returns globalVersion (= 0 before fix)
  • 0 > 0 → false → shouldRefreshSnapshot = false → stale snapshot reused

Human Verification (required)

  • Verified scenarios: Traced the full code path from session-updates.ts:182-228 confirming that existing sessions (isFirstTurnInSession = false) rely entirely on shouldRefreshSnapshot to rebuild skill snapshots.
  • Edge cases checked: bumpVersion() already produces Date.now()-scale numbers, so Date.now() initialization is consistent with the version space. workspaceVersions map entries default to 0 via ?? 0 but are always combined with Math.max(globalVersion, local), so the fix covers workspace-scoped versions too.
  • What I did not verify: Live end-to-end test with a running gateway (code analysis only).

Review Conversations

  • I replied to or resolved every bot review conversation I addressed in this PR.
  • I left unresolved only the conversations that still need reviewer or maintainer judgment.

Compatibility / Migration

  • Backward compatible? Yes
  • Config/env changes? No
  • Migration needed? No

Failure Recovery (if this breaks)

  • How to disable/revert this change quickly: Revert the single line back to let globalVersion = 0
  • Files/config to restore: src/agents/skills/refresh.ts
  • Known bad symptoms reviewers should watch for: Excessive skill snapshot rebuilds on every turn (unlikely — the version is persisted to the session, so after one rebuild the versions match again)

Risks and Mitigations

  • Risk: First agent turn after every gateway restart will always rebuild the skills snapshot (even if skills haven't changed).
    • Mitigation: This is a cold-path operation that's already expected on new sessions. The rebuild is lightweight (directory scan + prompt assembly). Subsequent turns in the same session skip it because the persisted version matches.

`globalVersion` starts at `0`, which matches the default session snapshot
version. The refresh guard (`snapshotVersion > 0`) therefore always evaluates
to `false` after a gateway restart, so sessions created before the restart
keep their stale skills snapshot indefinitely.

Initialize `globalVersion` to `Date.now()` so that any pre-existing session
snapshot (version `0` or from a previous boot) is correctly detected as stale
and rebuilt on the next agent turn.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Mar 16, 2026
@greptile-apps

greptile-apps Bot commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a one-line bug where globalVersion = 0 in src/agents/skills/refresh.ts caused the staleness guard snapshotVersion > 0 in session-updates.ts to always short-circuit to false after a gateway restart, preventing existing sessions from ever seeing skills that were installed while the gateway was stopped.

The fix (let globalVersion = Date.now()) is minimal, correct, and consistent with the bumpVersion() helper which already produces Date.now()-scale numbers.

  • Root cause is well-understood and the fix is precise — changing the initial value from 0 to Date.now() ensures any previously-persisted session snapshot (which stores version: 0 by default) is always detected as stale on the first agent turn after a restart.
  • No existing tests are broken — refresh.test.ts covers ensureSkillsWatcher behaviour but does not test getSkillsSnapshotVersion / globalVersion initialisation directly, so the change passes without modification.
  • No regression test added — The PR description's test plan marks manual verification as unchecked, and no automated test was added to assert that getSkillsSnapshotVersion() returns a value > 0 on cold module load (and specifically > any session-persisted version). Adding such a test would prevent this class of regression from going unnoticed in the future.

Confidence Score: 4/5

  • This PR is safe to merge — the single-line change is well-reasoned and the risk of regression is low.
  • The fix is minimal and directly addresses a clearly described bug. Initialising globalVersion to Date.now() is consistent with the existing bumpVersion() pattern and makes the staleness check work correctly after a restart. The only deduction is the absence of an automated regression test to guard against re-introducing the same bug.
  • No files require special attention.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/agents/skills/refresh.ts
Line: 27

Comment:
**Consider adding a regression test for this initialization**

The fix is correct, but `refresh.test.ts` currently has no coverage for `getSkillsSnapshotVersion` or the initial value of `globalVersion`. Without a test, the same bug (e.g. someone resetting it back to `0` or adding an early-return path that re-initialises it) could silently regress.

A lightweight test in `refresh.test.ts` could assert that the version is `> 0` immediately after module load and that it is strictly greater than a session snapshot version of `0`:

```ts
it("initial globalVersion is > 0 so stale session snapshots are always detected", async () => {
  const mod = await import("./refresh.js");
  const version = mod.getSkillsSnapshotVersion();
  expect(version).toBeGreaterThan(0);
  // Simulates a persisted session with default version 0
  expect(version).toBeGreaterThan(0); // snapshotVersion > 0 guard passes
});
```

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

Last reviewed commit: cf6ab7b

const workspaceVersions = new Map<string, number>();
const watchers = new Map<string, SkillsWatchState>();
let globalVersion = 0;
let globalVersion = Date.now();

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.

Consider adding a regression test for this initialization

The fix is correct, but refresh.test.ts currently has no coverage for getSkillsSnapshotVersion or the initial value of globalVersion. Without a test, the same bug (e.g. someone resetting it back to 0 or adding an early-return path that re-initialises it) could silently regress.

A lightweight test in refresh.test.ts could assert that the version is > 0 immediately after module load and that it is strictly greater than a session snapshot version of 0:

it("initial globalVersion is > 0 so stale session snapshots are always detected", async () => {
  const mod = await import("./refresh.js");
  const version = mod.getSkillsSnapshotVersion();
  expect(version).toBeGreaterThan(0);
  // Simulates a persisted session with default version 0
  expect(version).toBeGreaterThan(0); // snapshotVersion > 0 guard passes
});
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/skills/refresh.ts
Line: 27

Comment:
**Consider adding a regression test for this initialization**

The fix is correct, but `refresh.test.ts` currently has no coverage for `getSkillsSnapshotVersion` or the initial value of `globalVersion`. Without a test, the same bug (e.g. someone resetting it back to `0` or adding an early-return path that re-initialises it) could silently regress.

A lightweight test in `refresh.test.ts` could assert that the version is `> 0` immediately after module load and that it is strictly greater than a session snapshot version of `0`:

```ts
it("initial globalVersion is > 0 so stale session snapshots are always detected", async () => {
  const mod = await import("./refresh.js");
  const version = mod.getSkillsSnapshotVersion();
  expect(version).toBeGreaterThan(0);
  // Simulates a persisted session with default version 0
  expect(version).toBeGreaterThan(0); // snapshotVersion > 0 guard passes
});
```

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

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@clawsweeper

clawsweeper Bot commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

Closing this as duplicate or superseded after Codex automated review.

PR #48500 describes a real stale skills snapshot restart bug that still exists in the checked-out main, but the remaining work is now superseded by canonical maintainer PR #71497. That PR is open/draft, labeled maintainer and dedupe:parent, and applies the same root-cause fix in the current refresh-state.ts layer with focused regression coverage and a changelog entry.

Best possible solution:

Close #48500 as superseded by canonical maintainer PR #71497, and keep the implementation, regression test, and changelog entry concentrated in that current-file-layout fix.

What I checked:

So I’m closing this here and keeping the remaining discussion on the canonical linked item.

Codex Review notes: model gpt-5.5, reasoning high; reviewed against 62869c85025b.

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

Labels

agents Agent runtime and tooling size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant