Skip to content

Commit 0e781ea

Browse files
authored
fix(skills): reuse plugin metadata for execution workspaces (#126720)
* fix(skills): reuse metadata for execution workspaces * test(skills): make upload lease heartbeat deterministic
1 parent 2006049 commit 0e781ea

3 files changed

Lines changed: 75 additions & 37 deletions

File tree

‎src/skills/lifecycle/upload-store.test.ts‎

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,11 @@ describe("skill upload store", () => {
668668
});
669669

670670
it("renews the install lease and preserves an expired leased upload", async () => {
671+
let now = 1000;
671672
const { databasePath, store } = await makeStore({
672673
installLeaseHeartbeatMs: 10,
673674
installLeaseMs: 100,
675+
now: () => now,
674676
});
675677
const archive = Buffer.from("abc");
676678
const committed = await store.begin({
@@ -687,47 +689,53 @@ describe("skill upload store", () => {
687689

688690
const entered = deferred();
689691
const release = deferred();
692+
vi.useFakeTimers();
690693
const pinned = store.withCommittedUpload(committed.uploadId, async () => {
691694
entered.resolve();
692695
await release.promise;
693696
});
694-
await entered.promise;
695-
const db = stateDatabase(databasePath);
696-
const initialHeartbeat = (
697-
db
698-
.prepare(
699-
"SELECT heartbeat_at FROM state_leases WHERE scope = 'skill-upload-install' AND lease_key = ?",
700-
)
701-
.get(committed.uploadId) as { heartbeat_at: number }
702-
).heartbeat_at;
703-
await new Promise<void>((resolve) => {
704-
setTimeout(resolve, 40);
705-
});
706-
const renewedHeartbeat = (
707-
db
708-
.prepare(
709-
"SELECT heartbeat_at FROM state_leases WHERE scope = 'skill-upload-install' AND lease_key = ?",
710-
)
711-
.get(committed.uploadId) as { heartbeat_at: number }
712-
).heartbeat_at;
713-
expect(renewedHeartbeat).toBeGreaterThan(initialHeartbeat);
714-
715-
db.prepare("UPDATE skill_uploads SET expires_at = ? WHERE upload_id = ?").run(
716-
Date.now() - 1,
717-
committed.uploadId,
718-
);
719-
expect(
720-
deleteExpiredSkillUploadUnlessLeased({
721-
uploadId: committed.uploadId,
722-
nowMs: Date.now(),
723-
options: { path: databasePath },
724-
}),
725-
).toBe("leased");
726-
expect(uploadCount(databasePath)).toBe(1);
727-
expect(installLeaseCount(databasePath, committed.uploadId)).toBe(1);
728-
729-
release.resolve();
730-
await pinned;
697+
try {
698+
await entered.promise;
699+
const db = stateDatabase(databasePath);
700+
const initialHeartbeat = (
701+
db
702+
.prepare(
703+
"SELECT heartbeat_at FROM state_leases WHERE scope = 'skill-upload-install' AND lease_key = ?",
704+
)
705+
.get(committed.uploadId) as { heartbeat_at: number }
706+
).heartbeat_at;
707+
now += 10;
708+
await vi.advanceTimersByTimeAsync(10);
709+
const renewedHeartbeat = (
710+
db
711+
.prepare(
712+
"SELECT heartbeat_at FROM state_leases WHERE scope = 'skill-upload-install' AND lease_key = ?",
713+
)
714+
.get(committed.uploadId) as { heartbeat_at: number }
715+
).heartbeat_at;
716+
expect(renewedHeartbeat).toBeGreaterThan(initialHeartbeat);
717+
718+
db.prepare("UPDATE skill_uploads SET expires_at = ? WHERE upload_id = ?").run(
719+
now - 1,
720+
committed.uploadId,
721+
);
722+
expect(
723+
deleteExpiredSkillUploadUnlessLeased({
724+
uploadId: committed.uploadId,
725+
nowMs: now,
726+
options: { path: databasePath },
727+
}),
728+
).toBe("leased");
729+
expect(uploadCount(databasePath)).toBe(1);
730+
expect(installLeaseCount(databasePath, committed.uploadId)).toBe(1);
731+
} finally {
732+
release.resolve();
733+
try {
734+
await pinned;
735+
} finally {
736+
vi.useRealTimers();
737+
}
738+
}
731739
expect(installLeaseCount(databasePath, committed.uploadId)).toBe(0);
732740
});
733741

‎src/skills/runtime/session-snapshot.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { expectDefined } from "@openclaw/normalization-core";
33
import { beforeEach, describe, expect, it, vi } from "vitest";
44
import type { OpenClawConfig } from "../../config/config.js";
5+
import type { PluginMetadataSnapshot } from "../../plugins/plugin-metadata-snapshot.types.js";
56
import { WORKSPACE_SKILLS_PROMPT_FORMAT_VERSION } from "../types.js";
67
import type { SkillSnapshot } from "../types.js";
78

@@ -20,6 +21,7 @@ const {
2021
buildWorkspaceSkillSnapshotMock,
2122
ensureSkillsWatcherMock,
2223
getSkillsSnapshotVersionMock,
24+
loadMergedWorkspaceSkillsMock,
2325
shouldRefreshSnapshotForVersionMock,
2426
} = vi.hoisted(() => ({
2527
buildWorkspaceSkillSnapshotMock: vi.fn((..._args: unknown[]) => ({
@@ -29,11 +31,22 @@ const {
2931
})),
3032
ensureSkillsWatcherMock: vi.fn(),
3133
getSkillsSnapshotVersionMock: vi.fn(() => 1),
34+
loadMergedWorkspaceSkillsMock: vi.fn(
35+
(_params: { pluginMetadataSnapshot?: PluginMetadataSnapshot }) => [],
36+
),
3237
shouldRefreshSnapshotForVersionMock: vi.fn((cached = 0, next = 0) =>
3338
next === 0 ? cached > 0 : cached < next,
3439
),
3540
}));
3641

42+
vi.mock("../loading/workspace-skill-loader.js", () => ({
43+
loadMergedWorkspaceSkills: loadMergedWorkspaceSkillsMock,
44+
normalizeWorkspaceSkillRoots: (roots: {
45+
agentWorkspaceDir: string;
46+
executionSkillsDir?: string;
47+
}) => roots,
48+
}));
49+
3750
vi.mock("../loading/workspace-skill-prompt.js", () => ({
3851
buildSkillSnapshot: buildWorkspaceSkillSnapshotMock,
3952
}));
@@ -62,6 +75,22 @@ describe("resolveReusableWorkspaceSkillSnapshot", () => {
6275
);
6376
});
6477

78+
it("reuses prepared plugin metadata when loading execution-workspace skills", () => {
79+
const pluginMetadataSnapshot = { policyHash: "prepared" } as PluginMetadataSnapshot;
80+
81+
resolveReusableWorkspaceSkillSnapshot({
82+
workspaceDir: TEST_WORKSPACE_DIR,
83+
executionSkillsDir: "/tmp/execution/skills",
84+
config: {},
85+
pluginMetadataSnapshot,
86+
});
87+
88+
expect(loadMergedWorkspaceSkillsMock).toHaveBeenCalledOnce();
89+
expect(loadMergedWorkspaceSkillsMock.mock.calls[0]?.[0].pluginMetadataSnapshot).toBe(
90+
pluginMetadataSnapshot,
91+
);
92+
});
93+
6594
it("reuses cached resolvedSkills across calls with the same workspace, version, and filter", () => {
6695
const snapshot = strippedSnapshot();
6796

‎src/skills/runtime/session-snapshot.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export function resolveReusableWorkspaceSkillSnapshot(
101101
skillFilter: params.skillFilter,
102102
skillOverrides: params.skillOverrides,
103103
eligibility: params.eligibility,
104+
pluginMetadataSnapshot: params.pluginMetadataSnapshot,
104105
})
105106
: undefined;
106107
const snapshot = buildSkillSnapshot(params.workspaceDir, {

0 commit comments

Comments
 (0)