Skip to content

Commit 820d167

Browse files
authored
fix: allow plugin state values up to 1 MiB (#130387)
* fix: allow plugin state values up to 1 MiB * test: type-check plugin state write rejection cases * fix(ci): keep tar archive paths local on Windows
1 parent fa87acd commit 820d167

5 files changed

Lines changed: 66 additions & 36 deletions

File tree

‎docs/plugins/sdk-runtime.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ snapshots; OpenClaw owns all persistence and lifecycle coordination.
991991
const blob = await blobs.lookup("artifact-1");
992992
```
993993

994-
Keyed stores survive restarts and are isolated by the runtime-bound plugin id. Use `registerIfAbsent(...)` for atomic dedupe claims: it returns `true` when the key was missing or expired and registered, or `false` when a live value already exists without overwriting its value, creation time, or TTL. Use `deleteIf(...)` when cleanup must remove only the value previously observed; its synchronous predicate and deletion run in one SQLite transaction. Limits: `maxEntries` per namespace, 50,000 live rows per plugin, JSON values under 64KB, and optional TTL expiry. By default, a write at either row limit sheds the oldest live rows from the namespace being written; sibling namespaces are not evicted for that write, and the write still fails if the namespace cannot free enough rows. Set `overflowPolicy: "reject-new"` for durable ownership records that must never be evicted: new keys fail at either limit, while existing keys remain updateable.
994+
Keyed stores survive restarts and are isolated by the runtime-bound plugin id. Use `registerIfAbsent(...)` for atomic dedupe claims: it returns `true` when the key was missing or expired and registered, or `false` when a live value already exists without overwriting its value, creation time, or TTL. Use `deleteIf(...)` when cleanup must remove only the value previously observed; its synchronous predicate and deletion run in one SQLite transaction. Limits: `maxEntries` per namespace, 50,000 live rows per plugin, JSON values up to 1 MiB of UTF-8 encoded JSON, and optional TTL expiry. By default, a write at either row limit sheds the oldest live rows from the namespace being written; sibling namespaces are not evicted for that write, and the write still fails if the namespace cannot free enough rows. Set `overflowPolicy: "reject-new"` for durable ownership records that must never be evicted: new keys fail at either limit, while existing keys remain updateable.
995995

996996
`openSyncKeyedStore<T>(...)` returns the same store shape with synchronous methods (`register`, `registerIfAbsent`, `deleteIf`, `lookup`, `consume`, `clear` all return values directly instead of promises) for callers that cannot await.
997997

‎scripts/package-openclaw-for-docker.mts‎

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -641,12 +641,18 @@ export async function prepareBundledAiRuntimePackage(
641641
((tarballPath: string, destination: string) =>
642642
// Source-ref validation runs this trusted harness outside the candidate's dependency tree.
643643
// Keep extraction on the system tar contract so only the candidate checkout needs install.
644-
run("tar", ["-xzf", tarballPath, "-C", destination, "--strip-components=1"], destination, {
645-
timeoutMs: resolveTimeoutMs(
646-
"OPENCLAW_DOCKER_PACKAGE_PACK_TIMEOUT_MS",
647-
DEFAULT_PACKAGE_PACK_TIMEOUT_MS,
648-
),
649-
}));
644+
// Use an archive basename so GNU tar cannot treat a Windows drive as a remote host.
645+
run(
646+
"tar",
647+
["-xzf", path.basename(tarballPath), "-C", destination, "--strip-components=1"],
648+
path.dirname(tarballPath),
649+
{
650+
timeoutMs: resolveTimeoutMs(
651+
"OPENCLAW_DOCKER_PACKAGE_PACK_TIMEOUT_MS",
652+
DEFAULT_PACKAGE_PACK_TIMEOUT_MS,
653+
),
654+
},
655+
));
650656
const prepareManifest = packageOptions.prepareManifest ?? (async () => false);
651657
const restoreManifest = packageOptions.restoreManifest ?? (async () => false);
652658
const originalPackageJson = await fs.readFile(packageJsonPath, "utf8");
@@ -831,7 +837,12 @@ async function normalizeOpenClawTarballModes(tarballPath: string) {
831837
);
832838
const stageDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-package-modes-"));
833839
try {
834-
await run("tar", ["-xzf", tarballPath, "-C", stageDir], stageDir, { timeoutMs });
840+
await run(
841+
"tar",
842+
["-xzf", path.basename(tarballPath), "-C", stageDir],
843+
path.dirname(tarballPath),
844+
{ timeoutMs },
845+
);
835846
let stagedFileCount = 0;
836847
const normalizeStagedModes = async (dir: string): Promise<void> => {
837848
for (const entry of await fs.readdir(dir, { withFileTypes: true })) {
@@ -855,11 +866,16 @@ async function normalizeOpenClawTarballModes(tarballPath: string) {
855866
const stageRootEntries = await fs.readdir(stageDir);
856867
const normalizedPath = `${tarballPath}.modes-tmp`;
857868
await fs.rm(normalizedPath, { force: true });
858-
await run("tar", ["-czf", normalizedPath, "-C", stageDir, ...stageRootEntries], stageDir, {
859-
// macOS bsdtar must not add AppleDouble (._*) sidecar entries.
860-
env: { ...process.env, COPYFILE_DISABLE: "1" },
861-
timeoutMs,
862-
});
869+
await run(
870+
"tar",
871+
["-czf", path.basename(normalizedPath), "-C", stageDir, ...stageRootEntries],
872+
path.dirname(normalizedPath),
873+
{
874+
// macOS bsdtar must not add AppleDouble (._*) sidecar entries.
875+
env: { ...process.env, COPYFILE_DISABLE: "1" },
876+
timeoutMs,
877+
},
878+
);
863879
await fs.rename(normalizedPath, tarballPath);
864880
} finally {
865881
await fs.rm(stageDir, { force: true, recursive: true });

‎src/plugin-state/plugin-state-store.e2e.test.ts‎

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { withOpenClawTestState } from "../test-utils/openclaw-test-state.js";
66
import {
77
closePluginStateDatabase,
88
createPluginStateKeyedStore,
9+
createPluginStateSyncKeyedStore,
910
resetPluginStateStoreForTests,
1011
sweepExpiredPluginStateEntries,
1112
} from "./plugin-state-store.js";
@@ -149,31 +150,41 @@ describe("isolation", () => {
149150
// Limits
150151
// ---------------------------------------------------------------------------
151152
describe("limits", () => {
152-
it("accepts a value at the 64 KB boundary", async () => {
153-
await withOpenClawTestState({ label: "e2e-limit-accept" }, async () => {
154-
const store = createPluginStateKeyedStore<string>("fixture-plugin", {
153+
it.each(["async", "sync"])("enforces the 1 MiB boundary across %s writes", async (mode) => {
154+
await withOpenClawTestState({ label: "e2e-limit" }, async () => {
155+
const createStore =
156+
mode === "async"
157+
? createPluginStateKeyedStore<string>
158+
: createPluginStateSyncKeyedStore<string>;
159+
const store = createStore("fixture-plugin", {
155160
namespace: "size",
156161
maxEntries: 10,
157162
});
158163
// JSON.stringify wraps a string in quotes (+2 bytes).
159-
// 65 534 chars → 65 536 bytes of JSON → exactly at limit.
160-
const boundary = "x".repeat(65_534);
161-
await expect(store.register("big", boundary)).resolves.toBeUndefined();
162-
await expect(store.lookup("big")).resolves.toBe(boundary);
163-
});
164-
});
165-
166-
it("rejects a value one byte over 64 KB", async () => {
167-
await withOpenClawTestState({ label: "e2e-limit-reject" }, async () => {
168-
const store = createPluginStateKeyedStore<string>("fixture-plugin", {
169-
namespace: "size",
170-
maxEntries: 10,
171-
});
172-
// 65 535 chars → 65 537 bytes of JSON → over limit.
173-
const oversize = "x".repeat(65_535);
174-
await expect(store.register("big", oversize)).rejects.toMatchObject({
175-
code: "PLUGIN_STATE_LIMIT_EXCEEDED",
176-
});
164+
const boundary = "x".repeat(1_048_574);
165+
const oversize = `${boundary}x`;
166+
const update = expectDefined(store.update, "keyed store update support");
167+
await store.register("registered", boundary);
168+
expect(await store.registerIfAbsent("claimed", boundary)).toBe(true);
169+
await store.register("updated", "before");
170+
expect(await update("updated", () => boundary)).toBe(true);
171+
172+
for (const write of [
173+
() => store.register("registered", oversize),
174+
() => store.registerIfAbsent("rejected", oversize),
175+
() => update("updated", () => oversize),
176+
]) {
177+
await expect(async () => {
178+
await write();
179+
}).rejects.toMatchObject({
180+
code: "PLUGIN_STATE_LIMIT_EXCEEDED",
181+
});
182+
}
183+
resetPluginStateStoreForTests();
184+
for (const key of ["registered", "claimed", "updated"]) {
185+
expect(await store.lookup(key)).toBe(boundary);
186+
}
187+
expect(await store.lookup("rejected")).toBeUndefined();
177188
});
178189
});
179190
});

‎src/plugin-state/plugin-state-store.sqlite.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
} from "./plugin-state-store.types.js";
3131

3232
// Plugin-wide fuse only; namespace maxEntries still owns normal cache eviction.
33-
export const MAX_PLUGIN_STATE_VALUE_BYTES = 65_536;
33+
export const MAX_PLUGIN_STATE_VALUE_BYTES = 1_048_576;
3434
export const MAX_PLUGIN_STATE_ENTRIES_PER_PLUGIN = 50_000;
3535
const PLUGIN_STATE_EXPIRY_BATCH_ROWS = 1_024;
3636
let maxPluginStateEntriesPerPluginForTests: number | undefined;

‎src/plugin-state/plugin-state-store.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,10 @@ describe("plugin state keyed store", () => {
810810
await expect(store.register("non-enumerable", nonEnumerable)).rejects.toThrow(
811811
PluginStateStoreError,
812812
);
813-
await expectPluginStateStoreError(store.register("big", "x".repeat(65_537)), {
813+
// UTF-8 bytes, including JSON quotes, determine the 1 MiB boundary.
814+
const boundary = "é".repeat(524_287);
815+
await expect(store.register("large", boundary)).resolves.toBeUndefined();
816+
await expectPluginStateStoreError(store.register("big", `${boundary}x`), {
814817
code: "PLUGIN_STATE_LIMIT_EXCEEDED",
815818
});
816819

0 commit comments

Comments
 (0)