Skip to content

Commit 232281f

Browse files
authored
perf(skills): reuse unchanged sandbox skill snapshots (#121379)
Gate reuse on the watcher-owned snapshot version and persisted entry manifest, while keeping safe paths process-owned and reconciling only changed skills. Cache config fingerprints by object identity and reset them at the applied-config lifecycle owner.
1 parent 592bf90 commit 232281f

13 files changed

Lines changed: 541 additions & 46 deletions

‎docs/.generated/plugin-sdk-api-baseline.sha256‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
71522995185b956a0cc4927a472cc8d1153e5e998874bfd9a750513175174713 module/account-id
44
46e3736461cd8e238604ba5ad92023c00b1dc28358a27b4349fe5e16cd320719 module/account-resolution
55
4fbb1c87e99399f842a20d75d5e35a4b7064a1b7f02115c23f9a2a7cdcfb57ee module/agent-config-primitives
6-
f6a1af86f4f05b21c9c2015a32db51c0ecd896fbb5e9422d1e91cabbdf1201ef module/agent-harness
7-
f071c6f7a1512df242675c1c6f04ecedebb8bcc2816a52227c4e19c0fcdb9e3b module/agent-harness-runtime
6+
8e78e6ab3e3e76fbf4e5a0c56b38b9ec936aab54b1c09a41c5a3bf9c93da5475 module/agent-harness
7+
8bfacea25437c95d2bd63d0468b10aeabff6249ca7a9495b3227692b7ac4226b module/agent-harness-runtime
88
d6097cfa1b410f4b5267a56a7bd19c2a33fbaf6642683dae6aec68e998e48f6c module/agent-media-payload
9-
ccc96ba5d51d199eab044b1129678f18e073a225e43692cbbe7d1b764c23fe6f module/agent-runtime
9+
c36bfc7abe97c176e888a3bb9fac0c90c96d3070eb9a85663563567e3ddea87e module/agent-runtime
1010
b57a3cb274a9977c48c5387772df7707e50dde9a6132e532e7367a27225dc142 module/agent-scope-runtime
1111
8fecb210e22bce4532b6ab649b09465f0bd2c857a44abf40db7d683d6491e6da module/allow-from
1212
32e3efc34e4ca59cfb8bf21bcaa50da234821493f51ca41dc681ec39e127aa98 module/allowlist-config-edit

‎src/agents/embedded-agent-runner/run/attempt-setup.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ import type { ProviderRuntimePluginHandle } from "../../../plugins/provider-hook
55
import type { EmbeddedRunAttemptParams } from "./types.js";
66

77
const resolveProviderRuntimePluginHandle = vi.hoisted(() => vi.fn());
8+
const resolveSandboxContext = vi.hoisted(() => vi.fn(async () => null));
89

910
vi.mock("../../../plugins/provider-hook-runtime.js", async (importOriginal) => ({
1011
...(await importOriginal<typeof import("../../../plugins/provider-hook-runtime.js")>()),
1112
resolveProviderRuntimePluginHandle,
1213
}));
1314

15+
vi.mock("../../sandbox.js", () => ({ resolveSandboxContext }));
16+
1417
import { prepareEmbeddedAttemptSetup } from "./attempt-setup.js";
1518

1619
describe("prepareEmbeddedAttemptSetup", () => {
1720
beforeEach(() => {
1821
resolveProviderRuntimePluginHandle.mockReset();
22+
resolveSandboxContext.mockClear();
1923
});
2024

2125
it("prepares the default and session agent identities together", async () => {
@@ -39,6 +43,30 @@ describe("prepareEmbeddedAttemptSetup", () => {
3943
expect(setup.sessionAgentId).toBe("marketing");
4044
});
4145

46+
it("passes the resolved skill snapshot into sandbox synchronization", async () => {
47+
const skillsSnapshot = {
48+
prompt: "skills",
49+
skills: [{ name: "alpha" }],
50+
resolvedSkills: [],
51+
version: 42,
52+
};
53+
54+
await prepareEmbeddedAttemptSetup({
55+
config: {},
56+
modelId: "gpt-5.4",
57+
provider: "openai",
58+
runId: "run-sandbox-skills",
59+
sessionId: "session-sandbox-skills",
60+
sessionKey: "agent:main:main",
61+
skillsSnapshot,
62+
thinkLevel: "high",
63+
timeoutMs: 30_000,
64+
workspaceDir: path.join(os.tmpdir(), "openclaw-attempt-setup-sandbox-skills"),
65+
} as unknown as EmbeddedRunAttemptParams);
66+
67+
expect(resolveSandboxContext).toHaveBeenCalledWith(expect.objectContaining({ skillsSnapshot }));
68+
});
69+
4270
it("reuses lifecycle metadata and the provider handle from the runtime plan", async () => {
4371
const metadataSnapshot = { plugins: [] } as never;
4472
const workspaceDir = path.join(os.tmpdir(), "openclaw-attempt-setup-prepared");

‎src/agents/embedded-agent-runner/run/attempt-setup.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type AttemptWorkspaceParams = Pick<
3737
| "sandboxSessionKey"
3838
| "sessionId"
3939
| "sessionKey"
40+
| "skillsSnapshot"
4041
| "workspaceDir"
4142
>;
4243

@@ -50,6 +51,7 @@ export async function resolveAttemptWorkspaceSandbox(params: AttemptWorkspacePar
5051
config: params.config,
5152
execOverrides: params.execOverrides,
5253
sessionKey: sandboxSessionKey,
54+
skillsSnapshot: params.skillsSnapshot,
5355
workspaceDir: resolvedWorkspace,
5456
});
5557
const effectiveWorkspace =

‎src/agents/sandbox.resolveSandboxContext.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ describe("resolveSandboxContext", () => {
219219
}, 15_000);
220220

221221
it("resolves a registered non-docker backend", async () => {
222+
syncSkillsToWorkspaceMock.mockClear();
222223
resolveNodeExecEligibilityMock.mockClear();
223224
readRegisteredSandboxRuntimeIdsMock.mockResolvedValue(["registered-runtime"]);
224225
const backendFactory = vi.fn(async () => ({
@@ -255,11 +256,17 @@ describe("resolveSandboxContext", () => {
255256
},
256257
},
257258
};
259+
const skillsSnapshot = {
260+
prompt: "skills",
261+
skills: [{ name: "alpha" }],
262+
version: 42,
263+
};
258264

259265
const result = await resolveSandboxContext({
260266
config: cfg,
261267
execOverrides: { host: "node", node: "build-node", security: "allowlist" },
262268
sessionKey: "agent:worker:task",
269+
skillsSnapshot,
263270
workspaceDir: "/tmp/openclaw-test",
264271
});
265272

@@ -277,6 +284,9 @@ describe("resolveSandboxContext", () => {
277284
execOverrides: { host: "node", node: "build-node", security: "allowlist" },
278285
}),
279286
);
287+
expect(syncSkillsToWorkspaceMock).toHaveBeenCalledWith(
288+
expect.objectContaining({ skillsSnapshot }),
289+
);
280290

281291
const workspace = await ensureSandboxWorkspaceForSession({
282292
config: cfg,

‎src/agents/sandbox/context.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
resolveBrowserConfig,
1515
} from "../../plugin-sdk/browser-profiles.js";
1616
import { defaultRuntime } from "../../runtime.js";
17-
import type { SkillEligibilityContext, SkillUsagePath } from "../../skills/types.js";
17+
import type { SkillEligibilityContext, SkillSnapshot, SkillUsagePath } from "../../skills/types.js";
1818
import type { ExecPolicyOverrides } from "../exec-defaults.js";
1919
import { getSandboxBackendWorkdirResolver, requireSandboxBackendFactory } from "./backend.js";
2020
import { ensureSandboxBrowser } from "./browser.js";
@@ -36,6 +36,7 @@ async function syncSandboxSkillsToWorkspace(params: {
3636
agentId: string;
3737
rawSessionKey: string;
3838
execOverrides?: ExecPolicyOverrides;
39+
skillsSnapshot?: SkillSnapshot;
3940
}): Promise<{ eligibility?: SkillEligibilityContext; skillUsagePaths?: SkillUsagePath[] }> {
4041
try {
4142
const [
@@ -65,6 +66,7 @@ async function syncSandboxSkillsToWorkspace(params: {
6566
config: params.config,
6667
agentId: params.agentId,
6768
eligibility,
69+
skillsSnapshot: params.skillsSnapshot,
6870
});
6971
return { eligibility, skillUsagePaths };
7072
} catch (error) {
@@ -80,6 +82,7 @@ async function ensureSandboxWorkspaceLayout(params: {
8082
rawSessionKey: string;
8183
config?: OpenClawConfig;
8284
execOverrides?: ExecPolicyOverrides;
85+
skillsSnapshot?: SkillSnapshot;
8386
workspaceDir?: string;
8487
}): Promise<{
8588
agentWorkspaceDir: string;
@@ -113,6 +116,7 @@ async function ensureSandboxWorkspaceLayout(params: {
113116
agentId: params.agentId,
114117
rawSessionKey,
115118
execOverrides: params.execOverrides,
119+
skillsSnapshot: params.skillsSnapshot,
116120
});
117121
} else {
118122
await fs.mkdir(workspaceDir, { recursive: true });
@@ -123,6 +127,7 @@ async function ensureSandboxWorkspaceLayout(params: {
123127
agentId: params.agentId,
124128
rawSessionKey,
125129
execOverrides: params.execOverrides,
130+
skillsSnapshot: params.skillsSnapshot,
126131
});
127132
}
128133

@@ -193,6 +198,7 @@ type ResolveSandboxContextParams = {
193198
execOverrides?: ExecPolicyOverrides;
194199
requireCurrentConfig?: boolean;
195200
sessionKey?: string;
201+
skillsSnapshot?: SkillSnapshot;
196202
workspaceDir?: string;
197203
};
198204

@@ -221,6 +227,7 @@ async function resolveProvisionedSandboxContext(
221227
rawSessionKey,
222228
config: params.config,
223229
execOverrides: params.execOverrides,
230+
skillsSnapshot: params.skillsSnapshot,
224231
workspaceDir: params.workspaceDir,
225232
});
226233

@@ -333,6 +340,7 @@ export async function resolveSandboxContext(params: {
333340
execOverrides?: ExecPolicyOverrides;
334341
requireCurrentConfig?: boolean;
335342
sessionKey?: string;
343+
skillsSnapshot?: SkillSnapshot;
336344
workspaceDir?: string;
337345
}): Promise<SandboxContext | null> {
338346
const resolved = resolveSandboxSession(params);

‎src/gateway/server-reload-handlers.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ const hoisted = vi.hoisted(() => ({
227227
activeEmbeddedRunSessionKeys: [] as string[],
228228
markRestartAbortedMainSessions: vi.fn(async (_params: unknown) => ({ marked: 1, skipped: 0 })),
229229
runtimeConfig: { value: { session: { store: "/tmp/active-sessions.json" } } as OpenClawConfig },
230+
resetSkillSnapshotConfigFingerprintCache: vi.fn(),
230231
reloadEvents: [] as string[],
231232
loadModelCatalog: vi.fn(async (_params: { config: OpenClawConfig }) => []),
232233
resetModelCatalogCache: vi.fn(() => {}),
@@ -312,6 +313,11 @@ vi.mock("../config/config.js", async () => {
312313
};
313314
});
314315

316+
vi.mock("../skills/runtime/snapshot-config-fingerprint.js", async (importOriginal) => ({
317+
...(await importOriginal<typeof import("../skills/runtime/snapshot-config-fingerprint.js")>()),
318+
resetSkillSnapshotConfigFingerprintCache: hoisted.resetSkillSnapshotConfigFingerprintCache,
319+
}));
320+
315321
vi.mock("../agents/model-catalog.js", () => ({
316322
loadModelCatalog: (params: { config: OpenClawConfig }) => {
317323
hoisted.reloadEvents.push("load-model-catalog");
@@ -855,6 +861,7 @@ beforeEach(() => {
855861
resetProcessRegistryForTests();
856862
delete process.env.OPENCLAW_SKIP_CHANNELS;
857863
delete process.env.OPENCLAW_SKIP_PROVIDERS;
864+
hoisted.resetSkillSnapshotConfigFingerprintCache.mockClear();
858865
});
859866

860867
afterEach(() => {
@@ -972,6 +979,7 @@ describe("managed reload transaction ownership", () => {
972979
expect(result.acceptTerminalConfig).toHaveBeenCalledOnce();
973980
expect(result.prepareTerminalConfig).toHaveBeenCalledOnce();
974981
expect(result.reconcileTerminalSessions).toHaveBeenCalledOnce();
982+
expect(hoisted.resetSkillSnapshotConfigFingerprintCache).toHaveBeenCalledOnce();
975983
expect(getActiveSecretsRuntimeSnapshot()?.sourceConfig).toEqual(result.configA);
976984
});
977985

@@ -3840,6 +3848,7 @@ describe("gateway Gmail hot reload handlers", () => {
38403848
refKeys: ["env:default:TTS_FOURTH"],
38413849
},
38423850
]);
3851+
expect(hoisted.resetSkillSnapshotConfigFingerprintCache).not.toHaveBeenCalled();
38433852
} finally {
38443853
await reloader.stop();
38453854
}

‎src/gateway/server-reload-managed.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { OpenClawConfig } from "../config/types.openclaw.js";
22
import { runWithGatewayIndependentRootWorkAdmission } from "../process/gateway-work-admission.js";
33
import { getActiveSecretsRuntimeSnapshotRevision } from "../secrets/runtime-state.js";
4+
import { resetSkillSnapshotConfigFingerprintCache } from "../skills/runtime/snapshot-config-fingerprint.js";
45
import { invalidateConfigGetResponseCache } from "./config-get-response.js";
56
import {
67
startGatewayConfigReloader,
@@ -438,7 +439,12 @@ export function startManagedGatewayConfigReloader(
438439
throw error;
439440
}
440441
},
441-
onConfigApplied: (_plan, nextConfig) => params.commitTerminalConfig(nextConfig),
442+
onConfigApplied: (_plan, nextConfig) => {
443+
// Applied runtime identity owns config-derived process memos; accepted
444+
// source-only changes must not evict caches for the still-active config.
445+
resetSkillSnapshotConfigFingerprintCache();
446+
params.commitTerminalConfig(nextConfig);
447+
},
442448
onConfigRevisionApplied: publishAppliedConfigHash,
443449
onEffectiveConfigUnchanged,
444450
onNoopConfigCommit,

0 commit comments

Comments
 (0)