fix(memory): reduce constrained Gateway startup pressure (#197801)

* fix(memory): reclaim constrained process headroom earlier

* docs(memory): clarify constrained process retirement threshold

* fix(gateway): narrow reply runtime prewarm imports

* fix(memory): retain early reclamation with narrow startup imports

* fix(memory): defer unused Gateway reply and CLI runtimes

* test(gateway): preserve reply helpers in startup mocks

* fix(memory): serialize startup publication reads

* test(gateway): remove redundant shared reply mocks

* fix(memory): defer Gateway secret providers until needed

* test(gateway): document provider import isolation

* fix(memory): defer disk scan ownership imports until needed

* fix(memory): defer agent runtime imports until first use

* fix(memory): keep CLI inspection and cleanup demand-loaded

* fix(cli): preserve lightweight service inspection boundaries

* fix(gateway): defer optional work under memory pressure

* test(gateway): report unexpected startup cleanup failures

* fix(gateway): load skill archive runtimes on demand

* fix(gateway): defer cron agent execution runtimes

* fix(gateway): defer exec reviewer completion runtime

* fix(ci): include identity protocol in PR wrapper closure

* fix(build): compact runtime source without losing module origins

* fix(build): skip declaration chunks when compacting module labels

* fix(workers): collect small heaps after first idle period

* revert: drop unproven first-idle worker collection

* perf(skills): defer status readers during discovery

* fix: load cron readers only for cron state queries

* fix: load the OpenAI SDK only for model requests
This commit is contained in:
Vincent Koc 2026-11-13 12:00:18 +08:00 • committed by GitHub
parent f747fed366
commit d8eca04bb3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
87 changed files with 962 additions and 450 deletions

View file

@ -4891,7 +4891,6 @@
["src/gateway/server-request-context.test.ts","./server/health-state.js","4825118d8a74fcd3beae251fbdf728e718b324b75c5fac96a598af537e6debfd",1]
["src/gateway/server-request-entry.test.ts","./server/ws-connection/authenticated-request-dispatch.server-methods.runtime.js","f7b9d183a30f3c792db2d84b6bed3ebcdc1a2f1162342a1c101493ddb008508f",1]
["src/gateway/server-restart-sentinel-notice.test.ts","../utils/sleep.js","fe5511515fb7118ab09d5ca626173670f4f3f91fb876c0b70f1b71889ea0a2c1",1]
["src/gateway/server-restart-sentinel.state-context.test.ts","../auto-reply/reply/dispatch-from-config.runtime-loaders.js","f2d49ad7aad728ea0602313be6dde7d23aa4d1a566921000f68dacdf5644f5c6",1]
["src/gateway/server-restart-sentinel.state-context.test.ts","../channels/message/runtime.js","044b945c01befbf7f5b3835b9ba18832561a6fbfbc5acc97954eb82e43412ca2",1]
["src/gateway/server-restart-sentinel.state-context.test.ts","../channels/turn/lifecycle.js","9a0ead5a0e926a9e79fdb4fde9341973d545554eef81bd15a021dffef87bf7fb",1]
["src/gateway/server-restart-sentinel.state-context.test.ts","../hooks/internal-hooks.js","7806ee74d3dd149bdc5b66051e0766a11bfc5c2d770d79372aa8fad456a7bc95",1]

View file

@ -923,8 +923,10 @@ include that delay; coverage and heap counters describe acquisition. These field
are not an atomic process snapshot.
Critical memory pressure retires idle workers through their existing cleanup owners,
including when diagnostic event collection is disabled. Warnings do not retire
workers. Retirement requests are asynchronous, skip active work, and keep native
including when diagnostic event collection is disabled. On constrained Node/Linux
hosts, process memory usage at 80% of the limit also triggers retirement without
changing diagnostic severity. Other warnings do not retire workers.
Retirement requests are asynchronous, skip active work, and keep native
close/checkpoint operations off the main thread. Active operations keep their
custody and the usual 30-minute database retention window resumes after use.
No stored data, database schema, or update procedure changes.

View file

@ -1,10 +1,14 @@
import OpenAI from "openai";
import OpenAI, { AzureOpenAI } from "openai";
import { getAiTransportHost } from "../host.js";
import { buildGuardedModelFetch } from "../transports/host-policy.js";
import { resolveAzureOpenAIApiVersion } from "../transports/openai-responses-replay-internal.js";
import { buildOpenAISdkClientOptions } from "../transports/openai-transport-params.js";
import {
resolveOpenAIClientBaseUrl,
withOpenAIErrorResponseHook,
} from "../transports/openai-transport-shared.js";
import type { Model, ModelRequestHeaders, StreamOptions } from "../types.js";
import { isOpenAICompatibleAzureResponsesBaseUrl } from "./azure-openai-responses-client-compat.js";
import { isCloudflareProvider, resolveCloudflareBaseUrl } from "./cloudflare.js";
export function createOpenAIProviderClient(
@ -45,3 +49,45 @@ export function createOpenAIProviderClient(
),
});
}
export function createOpenAIResponsesClient(
model: Model,
apiKey: string,
defaultHeaders: ModelRequestHeaders,
fetchOverride?: typeof globalThis.fetch,
) {
return new OpenAI({
apiKey,
baseURL: resolveOpenAIClientBaseUrl(model),
dangerouslyAllowBrowser: true,
defaultHeaders,
fetch: fetchOverride ?? buildGuardedModelFetch(model),
...buildOpenAISdkClientOptions(model),
});
}
export function createAzureOpenAIClient(
model: Model,
apiKey: string,
defaultHeaders: ModelRequestHeaders,
fetchOverride?: typeof globalThis.fetch,
) {
const baseURL = model.baseUrl.replace(/\/+$/, "");
const clientOptions = {
apiKey,
dangerouslyAllowBrowser: true,
defaultHeaders,
baseURL,
fetch: fetchOverride ?? buildGuardedModelFetch(model),
...buildOpenAISdkClientOptions(model),
};
if (isOpenAICompatibleAzureResponsesBaseUrl(baseURL)) {
return new OpenAI(clientOptions);
}
return new AzureOpenAI({
...clientOptions,
apiVersion: resolveAzureOpenAIApiVersion(),
});
}

View file

@ -74,7 +74,7 @@ const reasoningReplayIdentity = { sessionId: "session-a", authProfileId: "profil
type LifecycleParams = Parameters<typeof runResponsesStreamLifecycle>[0];
type RequestOptions = Parameters<
ReturnType<LifecycleParams["createClient"]>["responses"]["create"]
Awaited<ReturnType<LifecycleParams["createClient"]>>["responses"]["create"]
>[1];
function startLifecycle(

View file

@ -150,6 +150,20 @@ export function applyCommonResponsesParams<TApi extends Api>(
}
}
type ResponsesClient = {
responses: {
create(
params: ResponseCreateParamsStreaming,
options: ResponsesRequestOptions,
): {
withResponse(): Promise<{
data: AsyncIterable<ResponseStreamEvent>;
response: Response;
}>;
};
};
};
export async function runResponsesStreamLifecycle<TApi extends Api>(params: {
stream: AssistantMessageEventStream;
model: Model<TApi>;
@ -165,19 +179,7 @@ export async function runResponsesStreamLifecycle<TApi extends Api>(params: {
> &
FirstStreamEventInternalOptions;
resolveRequestModel?: (model: Model<TApi>) => Model<TApi>;
createClient: (model: Model<TApi>) => {
responses: {
create(
params: ResponseCreateParamsStreaming,
options: ResponsesRequestOptions,
): {
withResponse(): Promise<{
data: AsyncIterable<ResponseStreamEvent>;
response: Response;
}>;
};
};
};
createClient: (model: Model<TApi>) => ResponsesClient | Promise<ResponsesClient>;
buildParams: (
model: Model<TApi>,
replayMode: OpenAIResponsesReplayMode,
@ -189,7 +191,7 @@ export async function runResponsesStreamLifecycle<TApi extends Api>(params: {
let firstEventAbort: ReturnType<typeof createFirstStreamEventAbortController> | undefined;
try {
const model = params.resolveRequestModel?.(params.model) ?? params.model;
const client = params.createClient(model);
const client = await params.createClient(model);
const encodeBody = prepareModelRequestBody(options);
const buildRequest = async (replayMode: OpenAIResponsesReplayMode) => {
let request = params.buildParams(model, replayMode);

View file

@ -26,9 +26,9 @@ vi.mock("openai", () => ({
},
}));
import { createOpenAIResponsesClient } from "../transports/openai-responses-client.js";
import type { OpenAIResponsesOptions as Options } from "../transports/openai-responses-contracts.js";
import { buildOpenAIResponsesParams } from "../transports/openai-responses-params-internal.js";
import { createOpenAIResponsesClient } from "./openai-provider-client.js";
import { makeOpenAIProviderModel } from "./openai-provider.test-support.js";
import { streamOpenAIResponses, streamSimpleOpenAIResponses } from "./openai-responses.js";

View file

@ -13,7 +13,6 @@ import {
clampOpenAIPromptCacheKey,
resolveOpenAIPromptCacheParams,
} from "./openai-prompt-cache.js";
import { createOpenAIProviderClient } from "./openai-provider-client.js";
import {
resolveOpenAISimpleReasoningEffort,
type OpenAIRequestReasoningEffort,
@ -65,7 +64,7 @@ export const streamOpenAIResponses: StreamFunction<"openai-responses", OpenAIRes
output,
context,
options,
createClient: () => {
createClient: async () => {
const apiKey = options?.apiKey || getEnvApiKey(model.provider) || "";
const cacheRetention = resolveCacheRetention(options?.cacheRetention);
const cacheSessionId = cacheRetention === "none" ? undefined : options?.sessionId;
@ -86,6 +85,7 @@ export const streamOpenAIResponses: StreamFunction<"openai-responses", OpenAIRes
headers["x-client-request-id"] = cacheSessionId;
}
const { createOpenAIProviderClient } = await import("./openai-provider-client.js");
return createOpenAIProviderClient(
model,
apiKey,

View file

@ -9,15 +9,18 @@ vi.mock("@anthropic-ai/sdk", () => {
throw new Error("Shared Anthropic helpers must not initialize the Anthropic SDK");
});
vi.mock("openai", () => {
throw new Error("Generic transport helpers must not initialize the OpenAI SDK");
});
// mock-isolation: importing the real partitioner would conceal an eager dependency.
vi.mock("./utils/reasoning-tag-text-partitioner.js", () => {
throw new Error("Generic runtime helpers must not initialize provider reasoning parsing");
});
it("loads generic transports without initializing a provider SDK", async () => {
await expect(import("./transports.js")).resolves.toHaveProperty(
"createTransportAwareStreamFnForModel",
expect.any(Function),
expect((await import("./transports.js")).createTransportAwareStreamFnForModel).toBeTypeOf(
"function",
);
});
@ -29,8 +32,7 @@ it("loads generic runtime helpers without initializing provider reasoning parsin
});
it("defers the Anthropic client SDK and contains a first-use import failure", async () => {
const { isDirectAnthropicModel, streamAnthropic } = await import("./internal/anthropic.js");
expect(isDirectAnthropicModel).toEqual(expect.any(Function));
const { streamAnthropic } = await import("./internal/anthropic.js");
const onPayload = vi.fn();
const stream = streamAnthropic(
makeProviderModel("anthropic-messages", "anthropic", "synthetic"),

View file

@ -6,7 +6,7 @@ import type {
SimpleStreamOptions,
StreamFn,
} from "@openclaw/llm-core";
import OpenAI from "openai";
import type OpenAI from "openai";
import type { ChatCompletion, ChatCompletionChunk } from "openai/resources/chat/completions.js";
import { getEnvApiKey } from "../env-api-keys.js";
import { getAiTransportHost } from "../host.js";
@ -19,7 +19,6 @@ import { resolveProviderContext } from "../provider-types.js";
import { resolveCacheRetention } from "../providers/cache-retention.js";
import { buildCopilotDynamicHeaders } from "../providers/github-copilot-headers.js";
import { finalizeOpenAICompletionsToolCalls } from "../providers/openai-completions-tool-calls.js";
import { createOpenAIProviderClient } from "../providers/openai-provider-client.js";
import { mapOpenAIStopReason } from "../providers/openai-stop-reason.js";
import { toOpenAIResponsesToolChoice } from "../providers/openai-tool-projection.js";
import { notifyLlmRequestActivity } from "../utils/llm-request-activity.js";
@ -275,6 +274,8 @@ export function streamOpenAICompletionsRequest(
let firstEventAbort: ReturnType<typeof createFirstStreamEventAbortController> | undefined;
try {
const requestContext = await resolveProviderContext(context, options);
const { default: OpenAI } = await import("openai");
const { createOpenAIProviderClient } = await import("../providers/openai-provider-client.js");
const apiKey = options?.apiKey || getEnvApiKey(model.provider) || "";
const cacheRetention = resolveCacheRetention(options?.cacheRetention);
const policy =

View file

@ -1,11 +1,10 @@
import { randomUUID } from "node:crypto";
import type { AssistantMessage, Model, ModelRequestHeaders, StreamFn } from "@openclaw/llm-core";
import OpenAI, { AzureOpenAI } from "openai";
import type OpenAI from "openai";
import { getEnvApiKey } from "../env-api-keys.js";
import { getAiTransportHost } from "../host.js";
import { codeModeToolSurfaceObserver } from "../provider-options.js";
import { resolveAzureDeploymentNameFromMap } from "../providers/azure-deployment-map.js";
import { isOpenAICompatibleAzureResponsesBaseUrl } from "../providers/azure-openai-responses-client-compat.js";
import { applyResponsesServiceTierPricing } from "../providers/openai-responses-shared.js";
import { notifyLlmRequestActivity } from "../utils/llm-request-activity.js";
import {
@ -58,7 +57,6 @@ import { supportsResponsesReasoningUpdate } from "./openai-responses-reasoning-u
import {
createOpenAIResponsesAssistantOutput,
createResponsesStreamWithRecovery,
resolveAzureOpenAIApiVersion,
} from "./openai-responses-replay-internal.js";
import { createResponsesRequestFetch } from "./openai-responses-request-fetch.js";
import {
@ -83,7 +81,6 @@ import {
import {
assertCodeModeResponsesToolSurface,
buildOpenAIClientHeaders,
buildOpenAISdkClientOptions,
buildOpenAISdkRequestOptions,
enforceCodeModeResponsesToolSurface,
resolveCodeModeResponsesVisibleToolNames,
@ -92,7 +89,6 @@ import {
createResponseModelTracker,
createOpenAIProviderAcceptanceHook,
log,
resolveOpenAIClientBaseUrl,
withOpenAIErrorResponseHook,
} from "./openai-transport-shared.js";
import {
@ -110,22 +106,6 @@ import {
} from "./transport-stream-shared.js";
import { redactIdentifier } from "./transport-utils.js";
export function createOpenAIResponsesClient(
model: Model,
apiKey: string,
defaultHeaders: ModelRequestHeaders,
fetchOverride?: typeof globalThis.fetch,
) {
return new OpenAI({
apiKey,
baseURL: resolveOpenAIClientBaseUrl(model),
dangerouslyAllowBrowser: true,
defaultHeaders,
fetch: fetchOverride ?? buildGuardedModelFetch(model),
...buildOpenAISdkClientOptions(model),
});
}
type ResponsesStreamParams = Parameters<typeof createResponsesStreamWithRecovery>[0] & {
requestOptions: ReturnType<typeof buildOpenAISdkRequestOptions>;
};
@ -135,7 +115,12 @@ function createResponsesTransportExecutor(config: {
firstEventTimeoutMs?: number;
streamRequest?: boolean;
httpContinuation?: boolean;
createClient: typeof createOpenAIResponsesClient;
createClient: (
model: Model,
apiKey: string,
defaultHeaders: ModelRequestHeaders,
fetchOverride?: typeof globalThis.fetch,
) => Promise<OpenAI>;
buildRequest: typeof buildOpenAIResponsesParams;
pricingOptions?: (
options: OpenAIResponsesOptions | undefined,
@ -220,7 +205,7 @@ function createResponsesTransportExecutor(config: {
options?.onResponse,
model,
);
const client = config.createClient(model, apiKey, httpHeaders, fetchOverride);
const client = await config.createClient(model, apiKey, httpHeaders, fetchOverride);
const nativeAstra =
model.id === "gpt-6-astra" && supportsNativeOpenAIResponsesEndpoint(model);
const asyncToolExecutionEligible =
@ -655,7 +640,11 @@ export function createOpenAIResponsesTransportStreamFn(): StreamFn {
return createResponsesTransportExecutor({
streamRequest: true,
httpContinuation: true,
createClient: createOpenAIResponsesClient,
createClient: async (...args) => {
const { createOpenAIResponsesClient } =
await import("../providers/openai-provider-client.js");
return createOpenAIResponsesClient(...args);
},
buildRequest: buildOpenAIResponsesParams,
pricingOptions: (options, model) => ({
serviceTier: options?.serviceTier,
@ -671,7 +660,10 @@ export function createAzureOpenAIResponsesTransportStreamFn(): StreamFn {
return createResponsesTransportExecutor({
outputApi: "azure-openai-responses",
firstEventTimeoutMs: AZURE_RESPONSES_FIRST_EVENT_TIMEOUT_MS,
createClient: createAzureOpenAIClient,
createClient: async (...args) => {
const { createAzureOpenAIClient } = await import("../providers/openai-provider-client.js");
return createAzureOpenAIClient(...args);
},
buildRequest: (model, context, options, metadata, replayMode) => {
const deploymentName = resolveAzureDeploymentNameFromMap({
modelId: model.id,
@ -684,29 +676,3 @@ export function createAzureOpenAIResponsesTransportStreamFn(): StreamFn {
},
});
}
function createAzureOpenAIClient(
model: Model,
apiKey: string,
defaultHeaders: ModelRequestHeaders,
fetchOverride?: typeof globalThis.fetch,
) {
const baseURL = model.baseUrl.replace(/\/+$/, "");
const clientOptions = {
apiKey,
dangerouslyAllowBrowser: true,
defaultHeaders,
baseURL,
fetch: fetchOverride ?? buildGuardedModelFetch(model),
...buildOpenAISdkClientOptions(model),
};
if (isOpenAICompatibleAzureResponsesBaseUrl(baseURL)) {
return new OpenAI(clientOptions);
}
return new AzureOpenAI({
...clientOptions,
apiVersion: resolveAzureOpenAIApiVersion(),
});
}

View file

@ -1,12 +1,12 @@
import { observeAsyncIterator, type Model } from "@openclaw/llm-core";
import { isRecord } from "@openclaw/normalization-core/record-coerce";
import { APIError } from "openai";
import { APIError } from "openai/core/error";
import { Stream } from "openai/core/streaming";
import type { ResponseInput } from "openai/resources/responses/responses.js";
import type { CompactionReplayRejection } from "../provider-options.js";
import type { createOpenAIResponsesClient } from "../providers/openai-provider-client.js";
import { registerSessionResourceCleanup } from "../session-resources.js";
import { prepareModelRequestBody } from "./model-request-body.js";
import type { createOpenAIResponsesClient } from "./openai-responses-client.js";
import {
DEFAULT_AZURE_OPENAI_API_VERSION,
isPreviousResponseRejection,

View file

@ -15,6 +15,7 @@ import { isRetryableAssistantError } from "../../../../src/llm/utils/retry.js";
import { createEmptyPluginRegistry } from "../../../../src/plugins/registry-empty.js";
import { withPluginRuntimeGenerationScope } from "../../../../src/plugins/runtime/generation-scope.js";
import { configureAiTransportHost, getAiTransportHost } from "../host.js";
import { createOpenAIResponsesClient as createClient } from "../providers/openai-provider-client.js";
import { makeProviderModel } from "../providers/provider-model.test-support.js";
import { cleanupSessionResources } from "../session-resources.js";
import {
@ -110,10 +111,7 @@ vi.mock("openai/resources/responses/ws.js", () => ({
},
}));
import {
createOpenAIResponsesClient as createClient,
createOpenAIResponsesTransportStreamFn as createStreamFn,
} from "./openai-responses-client.js";
import { createOpenAIResponsesTransportStreamFn as createStreamFn } from "./openai-responses-client.js";
import { createOpenAIResponsesWebSocketStream as createSocketStream } from "./openai-responses-websocket.js";
import { collectEventTypes } from "./openai-stream-cases.test-support.js";

View file

@ -344,15 +344,17 @@ function createDeadlineHook(packageRoot, artifactRoot) {
url: pathToFileURL(path.join(dist, file)).href,
source: fs.readFileSync(path.join(dist, file), "utf8"),
}));
const marker = "async function updatePluginsAfterCoreUpdate(params) {";
const declaration = /async function updatePluginsAfterCoreUpdate\((\w+)\)\s*\{/u;
const plugins = modules.filter(
(module) => module.name.startsWith("update-command-plugins-") && module.source.includes(marker),
(module) =>
module.name.startsWith("update-command-plugins-") && declaration.test(module.source),
);
const mutations = modules.filter((module) =>
/mutateConfigFileWithRetry as \w+/u.test(module.source),
);
assert.equal(plugins.length, 1, "one built plugin-convergence owner must be injectable");
assert.equal(mutations.length, 1, "one built config mutation owner must be exported");
const [marker, params] = declaration.exec(plugins[0].source);
const mutationExport = /mutateConfigFileWithRetry as (\w+)/u.exec(mutations[0].source)[1];
const output = path.join(artifactRoot, "repair-deadline-hook.mjs");
fs.writeFileSync(
@ -385,7 +387,7 @@ if (update >= 2 && process.argv[update + 1] === 'repair') {
if (url !== ${JSON.stringify(plugins[0].url)}) return loaded;
const source = typeof loaded.source === 'string' ? loaded.source : Buffer.from(loaded.source).toString('utf8');
if (!source.includes(${JSON.stringify(marker)})) throw new Error('Plugin deadline injection target changed');
return { ...loaded, source: source.replace(${JSON.stringify(marker)}, ${JSON.stringify(`${marker}\n await globalThis[Symbol.for('openclaw.e2e.repair-deadline')](params);`)}) };
return { ...loaded, source: source.replace(${JSON.stringify(marker)}, ${JSON.stringify(`${marker}\n await globalThis[Symbol.for('openclaw.e2e.repair-deadline')](${params});`)}) };
}});
}
`,

View file

@ -2,7 +2,7 @@
export function bundledModuleRegions(source) {
// Stable releases use regions. Rolldown 1.2.15 uses qualified module-ID labels;
// recognize one qualified token, not arbitrary plain line comments.
return source.matchAll(/^\/\/(?:#region | (?=\S+\/\S+$))(.+)$/gm);
return source.matchAll(/^\/\/(?:#region |(?:! ?| )(?=\S+\/\S+$))(.+)$/gm);
}
/**

View file

@ -0,0 +1,43 @@
import { parse } from "@babel/parser";
import { bundledModuleRegions } from "./bundled-module-regions.mjs";
/** Keep module labels as legal comments without shifting source-map positions. */
export function createCompactModuleLabelsPlugin() {
return {
name: "openclaw:compact-module-labels",
renderChunk(code: string, chunk: { fileName: string }) {
if (/\.d\.[cm]?ts$/u.test(chunk.fileName)) {
return null;
}
const { comments, program } = parse(code, { sourceType: "module", attachComment: false });
const parts: string[] = [];
let cursor = 0;
let statement = 0;
for (const comment of comments ?? []) {
if (
comment.type !== "CommentLine" ||
!comment.value.startsWith(" ") ||
bundledModuleRegions(`//${comment.value}`).next().done
) {
continue;
}
// Parser offsets are present; comments inside statements cannot establish an owner.
const start = comment.start!;
let next = program.body[statement];
while (next && next.end! <= start) {
next = program.body[++statement];
}
if (next && next.start! < start) {
continue;
}
parts.push(code.slice(cursor, start + 2), "!");
cursor = start + 3;
}
if (!cursor) {
return null;
}
parts.push(code.slice(cursor));
return { code: parts.join(""), map: null };
},
};
}

View file

@ -228,6 +228,7 @@ export const preservedModuleBuildSources = [
"src/cli/one-shot-exit.ts",
"src/cli/cron-cli.ts",
"src/cli/runtime-cleanup-scope.ts",
"src/cli/run-main-service-installation.ts",
"src/cli/runtime-cleanup.ts",
"src/state/openclaw-state-db-cache.ts",
"src/infra/worker-native-lifecycle.ts",

View file

@ -721,7 +721,7 @@ function extractJavaScriptImportSpecifiers(
regions.push({ ...previous, end: start });
}
const extensionId = /^extensions\/([a-z0-9][a-z0-9-]*)\//u.exec(marker[1] ?? "")?.[1];
stack.push({ start: end, extensionId, flat: marker[0].startsWith("// ") });
stack.push({ start: end, extensionId, flat: !marker[0].startsWith("//#region ") });
} else if (text.trim() === "#endregion") {
const region = stack.pop();
if (region) {

View file

@ -21,34 +21,34 @@ if (isMainThread) {
{
pattern: /\/diagnostic-events-[^/]+\.mjs$/,
counters: [
{ name: "emit", anchor: /function emitDiagnosticEventWithTrust\([^)]*\) \{/ },
{ name: "dispatch", anchor: /function dispatchDiagnosticEvent\([^)]*\) \{/ },
{ name: "emit", anchor: /function emitDiagnosticEventWithTrust\([^)]*\)\s*\{/ },
{ name: "dispatch", anchor: /function dispatchDiagnosticEvent\([^)]*\)\s*\{/ },
],
},
{
pattern: /\/main-thread-stall-[^/]+\.mjs$/,
counters: [
{ name: "task", anchor: /run\(name, run, taskPhase\) \{/ },
{ name: "continuation", anchor: /begin\(name, now\(\)\);/ },
{ name: "task", anchor: /run\(name,\s*run,\s*taskPhase\)\s*\{/ },
{ name: "continuation", anchor: /begin\(name,\s*now\(\)\);/ },
],
},
{
pattern: /\/diagnostic-profile-[^/]+\.mjs$/,
counters: [
{ name: "profile.parse", anchor: /function parseNativeCpuProfile\([^)]*\) \{/ },
{ name: "profile.frame", anchor: /function sanitizeDiagnosticProfileFrame\([^)]*\) \{/ },
{ name: "profile.parse", anchor: /function parseNativeCpuProfile\([^)]*\)\s*\{/ },
{ name: "profile.frame", anchor: /function sanitizeDiagnosticProfileFrame\([^)]*\)\s*\{/ },
],
},
{
pattern: /\/gateway-prewarm\.js$/,
counters: [{ name: "flight.sample", anchor: /sample\(stall\) \{/ }],
counters: [{ name: "flight.sample", anchor: /sample\(stall\)\s*\{/ }],
},
{
pattern: /\/extensions\/diagnostics-prometheus\/index\.js$/,
counters: [
{ name: "series", anchor: /const series = \([^)]*\) => \{/ },
{ name: "histogram", anchor: /const histogram = \([^)]*\) => \{/ },
{ name: "scrape", anchor: /const render = \(\) => \{/ },
{ name: "series", anchor: /const series\s*=\s*\([^)]*\)\s*=>\s*\{/ },
{ name: "histogram", anchor: /const histogram\s*=\s*\([^)]*\)\s*=>\s*\{/ },
{ name: "scrape", anchor: /const render\s*=\s*\(\)\s*=>\s*\{/ },
],
},
];

View file

@ -29,17 +29,17 @@ if (isMainThread) {
{
owner: "src/config/sessions/session-source-authority.ts",
name: "assertSources",
declaration: /const assertSources = \(\) => \{/g,
declaration: /const assertSources\s*=\s*\(\)\s*=>\s*\{/g,
},
{
owner: "src/gateway/operator-access-policy.ts",
name: "accessAssertCurrent",
declaration: /const assertCurrent = \(\) => \{/g,
declaration: /const assertCurrent\s*=\s*\(\)\s*=>\s*\{/g,
},
{
owner: "src/plugins/runtime/gateway-request-scope.ts",
name: "pluginGatewayScopeCalls",
declaration: /function runWithPluginGatewayScope\([^)]*\) \{/g,
declaration: /function runWithPluginGatewayScope\([^)]*\)\s*\{/g,
},
];
const census = {

View file

@ -173,7 +173,7 @@ try {
const observation = countFileSystemCalls();
const cpuStart = process.cpuUsage();
const start = performance.now();
const source = readWorkspaceSkillSources({
const source = await readWorkspaceSkillSources({
sourcePlan: plan,
executionWorkspaceDir: project,
limits: resolveSkillDiscoveryLimits(),

View file

@ -74,7 +74,7 @@ if (isMainThread) {
? result.source
: new TextDecoder().decode(result.source);
for (const name of names) {
const declaration = new RegExp(`function ${name}\\([^)]*\\) \\{`);
const declaration = new RegExp(`function ${name}\\([^)]*\\)\\s*\\{`);
if (declaration.test(source)) {
installed.add(name);
source = source.replace(

View file

@ -1494,6 +1494,7 @@ src/infra/worker-mutation-authority.ts
src/infra/worker-native-error.ts
src/infra/worker-native-handle.ts
src/infra/worker-native-lifecycle.ts
src/infra/worker-native-lifecycle.types.ts
src/infra/worker-native-lifecycle.worker.ts
src/infra/worker-native-port.ts
src/infra/worker-native-task-port.ts

View file

@ -13,6 +13,11 @@ import {
} from "./plugin-runtime-refresh.js";
import { clearToolSearchCatalog } from "./tool-search-catalog.js";
// mock-isolation: Gateway cleanup must not load the optional tool-dispatch bridge.
vi.mock("./code-mode-bridge.js", () => {
throw new Error("Code Mode cleanup loaded the tool-dispatch bridge");
});
function controlledRun(abortSignal?: AbortSignal) {
const started = createDeferredCore();
const release = createDeferredCore();

View file

@ -9,10 +9,10 @@ import { createSubsystemLogger } from "../logging/subsystem.js";
import { PluginRuntimeCloseRetainedError } from "../plugins/runtime-close-error.js";
import { createDeferredCore } from "../shared/deferred.js";
import { runInDetachedAsyncContext } from "../shared/detached-async-context.js";
import { createLazyRuntimeModule } from "../shared/lazy-runtime.js";
import { createPendingPromiseTracker } from "../shared/pending-promises.js";
import { observeAgentRunApprovalWait } from "./agent-run-approval-wait.js";
import { raceWithAbortSignal } from "./agent-tools.abort.js";
import { runBridgeRequest } from "./code-mode-bridge.js";
import type { CodeModeCatalogProjection } from "./code-mode-catalog.js";
import { CODE_MODE_EXEC_TOOL_NAME, CODE_MODE_WAIT_TOOL_NAME } from "./code-mode-control-tools.js";
import type {
@ -36,6 +36,9 @@ import type { ToolSearchRuntime } from "./tool-search-runtime.js";
import type { ToolSearchToolContext } from "./tool-search-types.js";
import { ToolInputError } from "./tools/common.js";
const loadCodeModeBridge = createLazyRuntimeModule(() => import("./code-mode-bridge.js"));
type RunBridgeRequest = typeof import("./code-mode-bridge.js").runBridgeRequest;
export type CodeModeBridgeDispatchState = {
started: boolean;
workerId?: string;
@ -478,13 +481,14 @@ export function reserveActiveRunSlot(ownedRunId?: string): () => void {
export function createPendingBridgeStates(
pendingRequests: PendingBridgeRequest[],
params: Omit<Parameters<typeof runBridgeRequest>[0], "request" | "reply" | "signal"> & {
params: Omit<Parameters<RunBridgeRequest>[0], "request" | "reply" | "signal"> & {
inbox: CodeModeProgramDataInbox;
activeRunId?: string;
signal: AbortSignal;
bridgeDispatch: CodeModeBridgeDispatchState;
},
): PendingBridgeState[] {
const deadline = performance.now() + params.remainingMs;
// Pending siblings retain dispatch context, never the original request batch.
return pendingRequests.map((request) => {
// Bridge calls start while the guest is parked. The wait tool delivers
@ -505,7 +509,7 @@ export function createPendingBridgeStates(
if (request.method !== "sleep") {
params.bridgeDispatch.started = true;
}
const bridgeCall = runBridgeRequest({
const bridgeParams = {
activeRunId: params.activeRunId,
workerId: params.bridgeDispatch.workerId,
runtime: params.runtime,
@ -516,12 +520,19 @@ export function createPendingBridgeStates(
parentToolCallId: params.parentToolCallId,
codeModeRunId: params.codeModeRunId,
reply,
remainingMs: Math.max(1, params.remainingMs),
completionRequired: params.completionRequired,
ctx: params.ctx,
request,
signal,
onUpdate: params.onUpdate,
};
const bridgeCall = loadCodeModeBridge().then(({ runBridgeRequest }) => {
// Cold loading consumes the existing call budget rather than extending it.
const remainingMs = deadline - performance.now();
if (remainingMs <= 0) {
throw new Error("interrupted");
}
return runBridgeRequest({ ...bridgeParams, remainingMs });
});
const completion = raceWithAbortSignal(bridgeCall, signal).catch(() => {
// Canceled leases are fenced; this cannot retain an arbitrary abort reason.

View file

@ -26,9 +26,13 @@ import {
import { runAgentAttempt as runAgentAttemptImpl } from "./attempt-execution.js";
const runCliAgentMock = vi.hoisted(() => vi.fn());
const cliRuntimeImports = vi.hoisted(() => ({ count: 0 }));
const runEmbeddedAgentMock = vi.hoisted(() => vi.fn());
// mock-isolation: Observe CLI admission without launching an external process.
vi.mock("../cli-runner.js", () => ({ runCliAgent: runCliAgentMock }));
vi.mock("../cli-runner.js", () => {
cliRuntimeImports.count++;
return { runCliAgent: runCliAgentMock };
});
// mock-isolation: Capture forwarded harness parameters without starting model execution.
vi.mock("../embedded-agent.js", () => ({ runEmbeddedAgent: runEmbeddedAgentMock }));
@ -101,6 +105,26 @@ describe("embedded attempt harness pinning", () => {
});
}
it("loads the CLI runtime only when a CLI attempt needs it", async () => {
expect(cliRuntimeImports.count).toBe(0);
await runHarnessAttempt({
providerOverride: "openai",
modelOverride: "gpt-5.4",
sessionEntry: makeSessionEntry("embedded-import"),
});
expect(runEmbeddedAgentMock).toHaveBeenCalledOnce();
expect(cliRuntimeImports.count).toBe(0);
runCliAgentMock.mockResolvedValueOnce({ meta: { durationMs: 1 } });
await runHarnessAttempt({
providerOverride: "claude-cli",
modelOverride: "opus",
sessionEntry: makeSessionEntry("cli-import"),
});
expect(cliRuntimeImports.count).toBe(1);
expect(runCliAgentMock).toHaveBeenCalledOnce();
});
it("renders the user turn's configured owners on a subagent completion turn", async () => {
const cfg: OpenClawConfig = {
commands: { ownerAllowFrom: ["telegram:111", "discord:222", "333"] },

View file

@ -24,7 +24,6 @@ import {
resolveCliExecutionAuthProfileId,
} from "../cli-execution-auth.js";
import { isClaudeCliProvider } from "../cli-output-records.js";
import { runCliAgent } from "../cli-runner.js";
import { hasCliLiveSession } from "../cli-runner/cli-live-session-registry.js";
import { buildCliMcpDelegationCapabilityBinding } from "../cli-runner/mcp-grant-context.js";
import { resolveCliRuntimeToolsAllow } from "../cli-runner/tool-policy.js";
@ -313,6 +312,7 @@ export async function runAgentAttempt(params: AgentAttemptParams) {
? "openclaw"
: undefined);
if (!isRawModelRun && isCliExecutionProvider) {
const { runCliAgent } = await import("../cli-runner.js");
const expectedLifecycleRevision = params.sessionEntry?.lifecycleRevision;
return withAdmittedCliCandidate(
{

View file

@ -17,7 +17,7 @@ import { createDeferredCore, type Deferred } from "../shared/deferred.js";
import { resolveAmbientOwnerAgentId } from "./agent-scope-config.js";
import { collectTextContentBlocks } from "./content-blocks.js";
import { abortable } from "./embedded-agent-runner/run/abortable.js";
import {
import type {
acquireSimpleCompletionModelForAgent as prepareModel,
completeWithPreparedSimpleCompletionModel as complete,
} from "./simple-completion-runtime.js";
@ -413,7 +413,10 @@ export function createModelExecAutoReviewer(params: {
const signal = params.signal
? AbortSignal.any([completionController.signal, params.signal])
: completionController.signal;
const preparedResult = createDeferredCore<Awaited<ReturnType<typeof prepareModel>>>();
const preparedResult = createDeferredCore<{
prepared: Awaited<ReturnType<typeof prepareModel>>;
complete: typeof complete;
}>();
const finished = createDeferredCore();
callerFinished = finished;
const work = new AsyncWorkScope();
@ -422,8 +425,10 @@ export function createModelExecAutoReviewer(params: {
void trackOwner(async () => {
let acquired: Awaited<ReturnType<typeof prepareModel>> | undefined;
try {
const runtime = await import("./simple-completion-runtime.js");
signal.throwIfAborted();
acquired = await work.track(() =>
prepareModel({
runtime.acquireSimpleCompletionModelForAgent({
cfg,
agentId,
modelRef,
@ -431,7 +436,10 @@ export function createModelExecAutoReviewer(params: {
signal,
}),
);
preparedResult.resolve(acquired);
preparedResult.resolve({
prepared: acquired,
complete: runtime.completeWithPreparedSimpleCompletionModel,
});
await finished.promise;
} catch (error) {
preparedResult.reject(error);
@ -442,14 +450,15 @@ export function createModelExecAutoReviewer(params: {
}
}
}).catch((error: unknown) => preparedResult.reject(error));
const prepared = await raceWithReviewerTimeout(preparedResult.promise, {
const preparation = await raceWithReviewerTimeout(preparedResult.promise, {
timeoutMs,
signal: params.signal,
onTimeout: () => completionController?.abort(),
});
if (prepared === EXEC_REVIEWER_TIMEOUT) {
if (preparation === EXEC_REVIEWER_TIMEOUT) {
return deferReview(`exec reviewer timed out after ${timeoutMs}ms`);
}
const { prepared, complete: runCompletion } = preparation;
if ("error" in prepared) {
return buildExecAutoReviewFailureDecision(
"exec reviewer model unavailable",
@ -459,7 +468,7 @@ export function createModelExecAutoReviewer(params: {
const result = await raceWithReviewerTimeout(
work.track(() =>
complete({
runCompletion({
model: prepared.model,
auth: prepared.auth,
cfg,

View file

@ -85,6 +85,12 @@ export const mcpImportBoundaryEntrypoints = {
}),
} as const;
// Keep service inspection imports observable in a cold child process.
export const cliServiceInspectionImportEntrypoint = legacy({
currentModuleUrl: import.meta.url,
sourceWorkerName: "run-main-service-installation",
});
// Preserve module boundaries so a child can retire only its own cleanup source copies.
export const cliCleanupRetirementEntrypoints = {
scope: legacy({ currentModuleUrl: import.meta.url, sourceWorkerName: "runtime-cleanup-scope" }),

View file

@ -1,3 +1,4 @@
import "../test-utils/prepare-compiled-subprocesses.js";
import fs from "node:fs/promises";
import path from "node:path";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
@ -7,12 +8,53 @@ import { decodeLaunchAgentPlistFixture } from "../daemon/launchd-plist.test-supp
import { resolveLaunchAgentPlistPath } from "../daemon/launchd-service-files.js";
import { buildTaskScript } from "../daemon/schtasks-layout.js";
import { withGatewayServiceInstallation } from "../infra/gateway-service-installation-context.js";
import { resolveRuntimeWorkerArgv, resolveRuntimeWorkerUrl } from "../infra/runtime-worker-url.js";
import * as exec from "../process/exec.js";
import { assertGatewayServiceSchemaMigrationAllowed } from "../state/openclaw-state-service-installation.js";
import { resolveTestNodeExecPath } from "../test-utils/node-process.js";
import { withMockedPlatform } from "../test-utils/vitest-spies.js";
import { cliServiceInspectionImportEntrypoint } from "./cli-entrypoint.test-support.js";
import { formatCliProcessFailure, runCliProcessChild } from "./cli-process-child.test-helpers.js";
import { inspectCliGatewayServiceInstallation } from "./run-main-service-installation.js";
const dirs = useAutoCleanupTempDirTracker(afterEach);
const inspectionEntry = resolveRuntimeWorkerUrl(cliServiceInspectionImportEntrypoint);
it("inspects a service without loading service mutation or database ownership", async () => {
const root = dirs.make("service-inspection-import-");
const nodeExecutable = resolveTestNodeExecPath();
const result = await runCliProcessChild({
nodeExecutable,
nodeArgs: [
...resolveRuntimeWorkerArgv(inspectionEntry, nodeExecutable).slice(0, -1),
"--input-type=module",
"--eval",
`
import assert from "node:assert/strict";
import { registerHooks } from "node:module";
let loadedOwner = false;
const hooks = registerHooks({ load(url, context, next) {
if (/\\/(?:daemon\\/service|state\\/openclaw-state-db)\\.(?:ts|js)(?:[?#].*)?$/.test(url)) loadedOwner = true;
return next(url, context);
}});
const { inspectCliGatewayServiceInstallation } = await import(${JSON.stringify(inspectionEntry.href)});
Object.defineProperty(process, "platform", { value: "aix" });
assert.equal(await inspectCliGatewayServiceInstallation({ HOME: ${JSON.stringify(root)} }), undefined);
assert.equal(loadedOwner, false, "read-only inspection loaded a mutation owner");
hooks.deregister();
`,
],
env: { PATH: process.env.PATH, HOME: root, OPENCLAW_STATE_DIR: root },
});
expect(
result,
formatCliProcessFailure({ reason: "service inspection imports", ...result }),
).toMatchObject({
code: 0,
signal: null,
});
});
const nativePlutil = process.platform === "darwin";
afterEach(() => vi.restoreAllMocks());
beforeEach(() => {

View file

@ -1,5 +1,6 @@
import fs from "node:fs/promises";
import path from "node:path";
import { readGatewayServiceCommand } from "../daemon/service-command.js";
import { mergeGatewayServiceEnv } from "../daemon/service-env-merge.js";
import {
inspectGatewayServiceInstallationDrift,
@ -7,7 +8,6 @@ import {
summarizeGatewayServiceLayout,
} from "../daemon/service-layout.js";
import type { GatewayServiceCommandConfig } from "../daemon/service-types.js";
import { resolveGatewayService } from "../daemon/service.js";
import { resolveIdentityPathViaExistingAncestorSync } from "../infra/boundary-path.js";
import { hasErrnoCode } from "../infra/errno.js";
import type { GatewayServiceInstallation } from "../infra/gateway-service-installation-context.js";
@ -112,9 +112,9 @@ export async function inspectCliGatewayServiceInstallation(
env: NodeJS.ProcessEnv,
): Promise<GatewayServiceInstallation | undefined> {
try {
const service = resolveGatewayService();
const ownCommand = await readOwnGatewayServiceCommand(env);
const command = ownCommand ?? (await service.readCommand(env, { inspectStateIdentity: true }));
const command =
ownCommand ?? (await readGatewayServiceCommand(env, { inspectStateIdentity: true }));
const root = await resolveOpenClawPackageRoot({ moduleUrl: import.meta.url });
const layout = await summarizeGatewayServiceLayout(command);
const drift = root ? await inspectGatewayServiceInstallationDrift(layout, root) : undefined;

View file

@ -75,7 +75,8 @@ it.each(["success", "failure"])(
executableAdmission = true;
try {
const command = withCliProcessScope(() => withCliCommandCleanup(false, async cleanup => {
assert(skillsResolutions > 0, "watcher callback was not retained before dispatch");
assert.equal(skillsResolutions, 0, "unused watcher runtime loaded before dispatch");
await import(entries.skills);
const closeWatchers = cleanup.closeSkillsWatchers;
const watcherGate = Promise.withResolvers();
const watcherStarted = Promise.withResolvers();

View file

@ -1,16 +1,21 @@
import "../test-utils/prepare-compiled-subprocesses.js";
import { afterAll, afterEach, beforeEach, expect, it, vi } from "vitest";
import * as postgres from "../infra/postgres-sync/pool.js";
import * as postgres from "../infra/postgres-sync/pool-state.js";
import {
captureRuntimeWorkerSource,
withRuntimeWorkerGeneration,
} from "../infra/runtime-worker-generation.js";
import * as workers from "../infra/worker-native-lifecycle.js";
import { NATIVE_WORKER_LIFETIMES_KEY } from "../infra/worker-native-lifecycle.types.js";
import { resolveGlobalSingleton } from "../shared/global-singleton.js";
import * as database from "../state/openclaw-state-db-cache.js";
import { STATE_DATABASE_LIFECYCLE_KEY } from "../state/openclaw-state-db-contract.js";
import * as lifecycle from "./gateway-cli/run-loop.test-support.js";
import { withCliCommandCleanup, withCliProcessScope } from "./runtime-cleanup-scope.js";
import { runCliDisposer, waitForPendingCliDisposers } from "./runtime-cleanup.js";
const resetDatabase = database.closeOpenClawStateDatabaseAsync;
const resetWorkers = workers.closeDefaultRetainedNativeWorkerSource;
const closeDatabase = vi.spyOn(database, "closeOpenClawStateDatabaseAsync");
const closeWorkers = vi.spyOn(workers, "closeDefaultRetainedNativeWorkerSource");
const closePostgres = vi.spyOn(postgres, "closePostgresPool");
@ -22,13 +27,28 @@ beforeEach(() => {
closeDatabase.mockReset().mockImplementation(async () => void events.push("database"));
closeWorkers.mockReset().mockImplementation(async () => void events.push("workers"));
closePostgres.mockReset().mockImplementation(async () => void events.push("postgres"));
resolveGlobalSingleton(
STATE_DATABASE_LIFECYCLE_KEY,
() => ({}),
() => closeDatabase(),
"shared-state",
);
resolveGlobalSingleton(
NATIVE_WORKER_LIFETIMES_KEY,
() => ({}),
() => closeWorkers(),
);
vi.spyOn(console, "error").mockImplementation(() => {});
});
afterEach(() => {
vi.useRealTimers();
vi.mocked(console.error).mockRestore();
});
afterAll(() => vi.restoreAllMocks());
afterAll(() => {
vi.restoreAllMocks();
resolveGlobalSingleton(STATE_DATABASE_LIFECYCLE_KEY, () => ({}), resetDatabase, "shared-state");
resolveGlobalSingleton(NATIVE_WORKER_LIFETIMES_KEY, () => ({}), resetWorkers);
});
const runCommand = <T>(run: () => T | Promise<T>) =>
withCliProcessScope(() =>

View file

@ -2,10 +2,17 @@ import { AsyncLocalStorage } from "node:async_hooks";
import "../plugins/plugin-source-capture-context.js";
import type { AgentHarness } from "../agents/harness/types.js";
import type { GatewayScheduler } from "../infra/gateway-scheduler.js";
import { closePostgresPool } from "../infra/postgres-sync/pool-state.js";
import { NATIVE_WORKER_LIFETIMES_KEY } from "../infra/worker-native-lifecycle.types.js";
import { LegacyPluginSdkResourceHost } from "../plugins/legacy-sdk-resource-host.js";
import type { PluginRegistry } from "../plugins/registry-types.js";
import { createDeferredCore } from "../shared/deferred.js";
import { resolveGlobalSingleton } from "../shared/global-singleton.js";
import {
resetGlobalSingletonLifecycleState,
resolveGlobalSingleton,
} from "../shared/global-singleton.js";
import { SKILLS_DISCOVERY_KEY } from "../skills/types.js";
import { STATE_DATABASE_LIFECYCLE_KEY } from "../state/openclaw-state-db-contract.js";
import { CliPluginInvocationResources } from "./plugin-invocation-resources.js";
import { installCliSignalExitHandlers, registerSignalExitGate } from "./signal-exit-barrier.js";
@ -63,14 +70,8 @@ export async function withCliCommandCleanup<T>(
return run();
}
const { GatewayScheduler } = await import("../infra/gateway-scheduler.js");
// A command can replace its own package. Retain cleanup before it can remove
// the files; another importer's module cache does not preserve this resolution.
// Registered owner callbacks survive package replacement without loading dormant runtimes.
const { runCliDisposerAfterPending } = await import("./runtime-cleanup.js");
const { closeOpenClawStateDatabaseAsync } = await import("../state/openclaw-state-db-cache.js");
const { closeDefaultRetainedNativeWorkerSource } =
await import("../infra/worker-native-lifecycle.js");
const { closePostgresPool } = await import("../infra/postgres-sync/pool.js");
const { closeSkillsWatchers } = await import("../skills/runtime/refresh.js");
const pluginResources = new CliPluginInvocationResources();
const releaseSignals = installCliSignalExitHandlers();
pluginResources.adopt({ release: async () => releaseSignals() });
@ -83,7 +84,7 @@ export async function withCliCommandCleanup<T>(
scheduler,
harnesses: new Map(),
registries: new Set(),
closeSkillsWatchers,
closeSkillsWatchers: () => resetGlobalSingletonLifecycleState(SKILLS_DISCOVERY_KEY),
pluginResources,
};
const finished = createDeferredCore();
@ -102,8 +103,8 @@ export async function withCliCommandCleanup<T>(
} finally {
// Owned shutdown runs before this drain; expired disposers keep their recorded outcome.
await runCliDisposerAfterPending("shared-state", async () => {
await closeOpenClawStateDatabaseAsync();
await closeDefaultRetainedNativeWorkerSource();
await resetGlobalSingletonLifecycleState(STATE_DATABASE_LIFECYCLE_KEY);
await resetGlobalSingletonLifecycleState(NATIVE_WORKER_LIFETIMES_KEY);
await closePostgresPool();
});
}

View file

@ -7,7 +7,6 @@ const fixture = vi.hoisted(() => ({
readRuntime: vi.fn(),
readLocation: vi.fn(),
readFile: vi.fn(),
readFullState: vi.fn(),
isEnabled: vi.fn(),
}));
vi.mock("node:fs/promises", () => ({ default: { readFile: fixture.readFile } }));
@ -15,16 +14,12 @@ vi.mock("./gateway-run-argv.js", async (importOriginal) => ({
...(await importOriginal<typeof import("./gateway-run-argv.js")>()),
isGatewayRunInvocationArgv: () => fixture.gateway,
}));
vi.mock("../daemon/service.js", async (importOriginal) => ({
...(await importOriginal<typeof import("../daemon/service.js")>()),
resolveGatewayService: () => fixture,
readGatewayServiceState: fixture.readFullState,
}));
vi.mock("../daemon/systemd-service-files.js", async (importOriginal) => ({
...(await importOriginal<typeof import("../daemon/systemd-service-files.js")>()),
resolveSystemdUnitPathForName: (_env: unknown, name: string) =>
"/home/example/.config/systemd/user/" + name + ".service",
readSystemdServiceCommandLocation: fixture.readLocation,
readSystemdServiceExecStart: fixture.readCommand,
}));
vi.mock("../daemon/systemd-runtime.js", async (importOriginal) => ({
...(await importOriginal<typeof import("../daemon/systemd-runtime.js")>()),
@ -75,7 +70,9 @@ function runtime(
}
const older = () =>
expect(inspectCliGatewayServiceInstallation({})).resolves.toMatchObject({ version: "2026.10.5" });
afterEach(() => vi.restoreAllMocks());
beforeEach(() => {
mockProcessPlatform("linux");
vi.clearAllMocks();
fixture.gateway = true;
fixture.versions = { user: "2026.10.5", system: "2026.10.8" };
@ -87,9 +84,6 @@ beforeEach(() => {
});
fixture.readLocation.mockResolvedValue({ kind: "command", command });
fixture.readRuntime.mockResolvedValue(runtime());
fixture.readFullState.mockRejectedValue(
Object.assign(new Error("root-only EnvironmentFile"), { code: "EACCES" }),
);
fixture.isEnabled.mockRejectedValue(new Error("unrelated user manager unavailable"));
fixture.readCommand.mockResolvedValue({
fixtureScope: "user",
@ -98,14 +92,11 @@ beforeEach(() => {
});
});
describe.skipIf(process.platform === "win32")("worker policy own-service selection", () => {
beforeEach(() => mockProcessPlatform("linux"));
afterEach(() => vi.restoreAllMocks());
it.each(["", "/gateway"])(
"uses loaded location and native runtime for self in %s despite root-only EnvironmentFile",
async (leaf) => {
fixture.readFile.mockResolvedValue(`0::/system.slice/openclaw-gateway.service${leaf}\n`);
await expect(inspectCliGatewayServiceInstallation({})).resolves.toBeUndefined();
expect(fixture.readFullState).not.toHaveBeenCalled();
expect(fixture.readFile).toHaveBeenCalledTimes(1);
expect(fixture.readCommand).not.toHaveBeenCalled();
expect(fixture.readRuntime).toHaveBeenCalledWith(
@ -129,7 +120,6 @@ describe.skipIf(process.platform === "win32")("worker policy own-service selecti
fixture.isEnabled.mockResolvedValue(false);
await expect(inspectCliGatewayServiceInstallation({})).resolves.toBeUndefined();
expect(fixture.isEnabled).not.toHaveBeenCalled();
expect(fixture.readFullState).not.toHaveBeenCalled();
});
it("ordinary CLI retains older installed-service guard without manager probes", async () => {
fixture.gateway = false;
@ -230,19 +220,4 @@ describe("worker policy installed-service fallbacks", () => {
await expect(inspectCliGatewayServiceInstallation({})).resolves.toBeUndefined();
expect(fixture.readRuntime).not.toHaveBeenCalled();
});
it.each(["darwin", "win32"])("preserves the installed-service guard on %s", async (platform) => {
const descriptor = Object.getOwnPropertyDescriptor(process, "platform");
if (!descriptor) {
throw new Error("missing platform descriptor");
}
try {
Object.defineProperty(process, "platform", { ...descriptor, value: platform });
await older();
expect(fixture.readFile).not.toHaveBeenCalled();
expect(fixture.readRuntime).not.toHaveBeenCalled();
expect(fixture.readLocation).not.toHaveBeenCalled();
} finally {
Object.defineProperty(process, "platform", descriptor);
}
});
});

View file

@ -6,7 +6,7 @@ import { resolveRealpathOrAbsolute as canonicalizePathForComparison } from "../.
import { getOpenClawDatabaseMaintenanceScope } from "../../state/openclaw-state-db-async-lifecycle.js";
import { isMigrationArchiveArtifactName } from "./artifacts.js";
import { resolveSessionArtifactDirectory } from "./paths.js";
import { listDurableSqliteTargetPathsForSessionStorePath } from "./session-sqlite-target.js";
import { listSqliteTargetCandidatePathsForSessionStorePath } from "./session-sqlite-target-paths.js";
export type SessionPhysicalDiskUsage = {
databaseWalBytes: number;
@ -91,7 +91,14 @@ export async function readSessionPhysicalDiskUsage(
const sessionsDirFiles = await readSessionsDirFiles(sessionsDir);
const coldArchiveFiles = await readSessionsDirFiles(path.join(sessionsDir, "cold"));
const promptBlobFiles = await readSessionPromptBlobFiles(sessionsDir);
const databasePaths = listDurableSqliteTargetPathsForSessionStorePath(storePath);
const candidates = listSqliteTargetCandidatePathsForSessionStorePath(storePath);
// Only sibling stores need database ownership inspection.
const databasePaths =
candidates.length === 1
? candidates
: (
await import("./session-sqlite-target.js")
).listDurableSqliteTargetPathsForSessionStorePath(storePath);
const databaseFiles = await readSqliteDatabaseFiles(databasePaths);
const databaseMetadataPaths = new Set(
databasePaths.flatMap((databasePath) =>

View file

@ -50,6 +50,37 @@ async function addSessionArtifacts(directory: string, index: number): Promise<vo
}
describe("physical session disk usage", () => {
it("loads database ownership inspection only for multi-store scans", async () => {
await withTestDir({ prefix: "openclaw-disk-scan-imports-" }, async (directory) => {
const databasePath = path.join(directory, "custom.sqlite");
await fs.writeFile(databasePath, Buffer.alloc(321));
const selectTargets = vi.fn(() => [databasePath]);
const loadTargets = vi.fn(() => ({
listDurableSqliteTargetPathsForSessionStorePath: selectTargets,
}));
vi.resetModules();
// mock-isolation: Observe cold ownership loading without initializing the database graph.
vi.doMock("./session-sqlite-target.js", loadTargets);
try {
const { readSessionPhysicalDiskUsage: scan } = await import("./disk-budget-files.js");
await expect(scan(databasePath)).resolves.toEqual({
databaseWalBytes: 0,
totalBytes: 321,
});
expect(loadTargets).not.toHaveBeenCalled();
await fs.writeFile(path.join(directory, "custom.other.sqlite"), Buffer.alloc(123));
const storePath = path.join(directory, "custom.json");
await scan(storePath);
expect(loadTargets).toHaveBeenCalledOnce();
expect(selectTargets).toHaveBeenCalledExactlyOnceWith(storePath);
} finally {
vi.doUnmock("./session-sqlite-target.js");
vi.resetModules();
}
});
});
// Bun does not emit Node's PROMISE async-hook events.
it.skipIf(Boolean(process.versions.bun))(
"bounds per-artifact promise allocation inside the scan worker",

View file

@ -9,20 +9,6 @@ import { readCronRunReceiptCurrentFactsInDatabase } from "./run-receipt-read.js"
type CronStateReadCommand = Extract<OpenClawStateReadCommand, { type: `cron.${string}` }>;
export function isCronStateReadCommand(
command: OpenClawStateReadCommand,
): command is CronStateReadCommand {
switch (command.type) {
case "cron.currentReceipt":
case "cron.scratch":
case "cron.jobNames":
case "cron.quarantine":
return true;
default:
return false;
}
}
/** Runs Cron queries inside the shared reader's admitted native frame. */
export function readCronStateCommandInDatabase(
db: EngineConnection,

View file

@ -0,0 +1,13 @@
import { readLaunchAgentProgramArguments } from "./launchd-runtime.js";
import { readScheduledTaskCommand } from "./schtasks-layout.js";
import type { GatewayService } from "./service-types.js";
import { readSystemdServiceExecStart } from "./systemd-service-files.js";
const commandReaders: Partial<Record<NodeJS.Platform, GatewayService["readCommand"]>> = {
darwin: readLaunchAgentProgramArguments,
linux: readSystemdServiceExecStart,
win32: readScheduledTaskCommand,
};
export const readGatewayServiceCommand: GatewayService["readCommand"] = (env, options) =>
commandReaders[process.platform]?.(env, options) ?? Promise.resolve(null);

View file

@ -1,4 +1,4 @@
import { expect, it } from "vitest";
import { expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import {
resolveGatewayInteractiveSurfaceAuth as interactiveAuth,
@ -30,6 +30,37 @@ const remotePasswordRef = missingSecretRef("MISSING_REMOTE_PASSWORD");
const remoteToken = params({ remote: { token: remoteTokenRef } }, "remote");
const remotePassword = params({ remote: { password: remotePasswordRef } }, "remote");
it("loads secret providers only when selected Gateway credentials use a ref", async () => {
const loadProviders = vi.fn(() => ({
resolveSecretRefValues: vi.fn(async () => new Map([["env:default:TEST_TOKEN", "ref-token"]])),
}));
vi.resetModules();
// mock-isolation: Observe provider initialization without loading the real provider graph.
vi.doMock("../secrets/resolve.js", loadProviders);
try {
const { resolveGatewayInteractiveSurfaceAuth } = await import("./auth-surface-resolution.js");
await expect(
resolveGatewayInteractiveSurfaceAuth(
params({ auth: { mode: "token", token: "plain-token" } }),
),
).resolves.toMatchObject({ token: "plain-token" });
await expect(
resolveGatewayInteractiveSurfaceAuth(params({ auth: { mode: "token" } })),
).resolves.toMatchObject({ token: "ambient-token" });
expect(loadProviders).not.toHaveBeenCalled();
await expect(
resolveGatewayInteractiveSurfaceAuth(
params({ auth: { mode: "token", token: missingSecretRef("TEST_TOKEN") } }),
),
).resolves.toMatchObject({ token: "ref-token" });
expect(loadProviders).toHaveBeenCalledOnce();
} finally {
vi.doUnmock("../secrets/resolve.js");
vi.resetModules();
}
});
it.each([
["local token mode", params({ auth: { mode: "token", token } }), "gateway.auth.token"],
["implicit local token mode", params({ auth: { token } }), "gateway.auth.token"],

View file

@ -285,16 +285,37 @@ function installPublicationRecoveryTests(engine: "sqlite" | "postgres") {
).toMatchObject({ status: "published" });
});
it("reads each pending queue once during empty startup and still reads unreported results", async () => {
it("reads pending queues serially once during empty startup and still reads unreported results", async () => {
const placements = withEngineStoreAdmission(() =>
createWorkerSessionPlacementStore({ database: openOpenClawStateDatabase() }),
);
const pendingResults = vi.spyOn(placements, "listPendingWorkspaceResultsAsync");
const runtime = createRecoveryRuntime(vi.fn(), placements);
const repositoryReads = vi.spyOn(publicationStore, "listRepositoryGitHubPublicationsAsync");
const sharedReads = vi.spyOn(publicationStore, "listSharedGitHubPublicationsAsync");
let activePendingReads = 0;
let maxPendingReads = 0;
const trackPendingRead = async <T>(read: () => Promise<T>) => {
maxPendingReads = Math.max(maxPendingReads, ++activePendingReads);
try {
return await read();
} finally {
activePendingReads--;
}
};
const listRepository = publicationStore.listRepositoryGitHubPublicationsAsync;
const listShared = publicationStore.listSharedGitHubPublicationsAsync;
const repositoryReads = vi
.spyOn(publicationStore, "listRepositoryGitHubPublicationsAsync")
.mockImplementation((input) =>
input?.pending ? trackPendingRead(() => listRepository(input)) : listRepository(input),
);
const sharedReads = vi
.spyOn(publicationStore, "listSharedGitHubPublicationsAsync")
.mockImplementation((input) =>
input?.pending ? trackPendingRead(() => listShared(input)) : listShared(input),
);
const defer = vi.spyOn(publicationStore, "deferGitHubPublicationRequestsAsync");
await runtime.reconcilePublications();
expect(maxPendingReads).toBe(1);
expect(defer).not.toHaveBeenCalled();
expect(pendingResults).not.toHaveBeenCalled();
expect(repositoryReads.mock.calls.map(([input]) => input)).toEqual([

View file

@ -76,10 +76,12 @@ export function createGitHubPublicationRuntime(params: {
loadingUpdates = updates;
pendingKnown = true;
try {
const [shared, repository] = await Promise.all([
listSharedGitHubPublicationsAsync({ pending: true }),
listRepositoryGitHubPublicationsAsync({ ownerProfileId: null, pending: true }),
]);
// Read serially so startup can reuse the same state-reader worker.
const shared = await listSharedGitHubPublicationsAsync({ pending: true });
const repository = await listRepositoryGitHubPublicationsAsync({
ownerProfileId: null,
pending: true,
});
pendingRequests.clear();
for (const row of shared) {
pendingRequests.add(JSON.stringify(["shared", row.request_id]));

View file

@ -9,7 +9,6 @@ import {
describeSecretResolutionOperatorRecovery,
isSecretResolutionError,
} from "../secrets/resolve-errors.js";
import { resolveSecretRefValues } from "../secrets/resolve.js";
import { formatConcreteConfigPath, tokenizeConcreteConfigPath } from "../shared/dot-path.js";
export type SecretInputUnresolvedReasonStyle = "generic" | "detailed"; // pragma: allowlist secret
@ -72,6 +71,7 @@ async function resolveConfiguredSecretInput(params: ConfiguredSecretInputParams)
const refLabel = `${ref.source}:${ref.provider}:${ref.id}`;
try {
const { resolveSecretRefValues } = await import("../secrets/resolve.js");
const resolved = await resolveSecretRefValues([ref], {
config: params.config,
env: params.env,

View file

@ -1,6 +1,5 @@
import type { CliDeps } from "../cli/deps.types.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { runCronIsolatedAgentTurn } from "../cron/isolated-agent.js";
import type { CronServiceDeps } from "../cron/service/state.js";
import { executeScheduledPluginCommandDispatch } from "../plugins/plugin-command-runtime.js";
import { listPluginCommandScheduleHosts } from "../plugins/plugin-command-schedule-hosts.js";
@ -48,6 +47,7 @@ function createGatewayCronAgentJobRunner(
if (pluginCommandResult) {
return pluginCommandResult;
}
const { runCronIsolatedAgentTurn } = await import("../cron/isolated-agent.js");
return await runCronIsolatedAgentTurn({
...request,
cfg,

View file

@ -8,7 +8,6 @@ import { createDeferred } from "../../test/helpers/promise.js";
import { createRequireRecord } from "../../test/helpers/record.js";
import { AgentDeletionCommitUncertainError } from "../agents/agent-deletion-errors.js";
import {
abortAndDrainEmbeddedAgentRun,
detachActiveEmbeddedRun,
isEmbeddedAgentRunHandleActive,
attachActiveEmbeddedRun,
@ -106,9 +105,6 @@ const mocks = vi.hoisted(() => {
hasHooks: (hookName: string) => hookName === "cron_changed",
runCronChanged: changed,
})),
abortRun: vi.fn<typeof import("../agents/embedded-agent.js").abortAndDrainEmbeddedAgentRun>(
async () => ({ aborted: true, drained: true, forceCleared: false }),
),
captureMcp:
vi.fn<
typeof import("../agents/agent-bundle-mcp-manager-api.js").captureSessionMcpRuntimeRetirement
@ -187,11 +183,6 @@ vi.mock("../plugins/hook-runner-global.js", () => ({
getGlobalHookRunner: mocks.hooks,
}));
// mock-isolation: Cancellation cases control drain settlement without starting embedded agents.
vi.mock("../agents/embedded-agent.js", () => ({
abortAndDrainEmbeddedAgentRun: mocks.abortRun,
}));
vi.mock("../agents/agent-bundle-mcp-manager-api.js", async (importOriginal) => ({
...(await importOriginal<typeof import("../agents/agent-bundle-mcp-manager-api.js")>()),
captureSessionMcpRuntimeRetirement: mocks.captureMcp,
@ -1289,7 +1280,6 @@ describe("buildGatewayCronService", () => {
async function cleanupTimedOutCronRun(state: CronServiceFixture) {
const job = await addAgentTurnJob(state, "shared session turn", "work");
mocks.abortRun.mockImplementation(abortAndDrainEmbeddedAgentRun);
await getCronDeps(state).cleanupTimedOutAgentRun?.({
job,
timeoutMs: 600_000,
@ -1324,7 +1314,6 @@ describe("buildGatewayCronService", () => {
afterEach(() => {
embeddedRunsTesting.resetActiveEmbeddedRuns();
mocks.abortRun.mockReset();
});
it.each(["replacement-run", "cron-run"])(

View file

@ -1,8 +1,7 @@
import { captureSessionMcpRuntimeRetirement } from "../agents/agent-bundle-mcp-manager-api.js";
import { prepareAgentRunTerminalReply } from "../agents/agent-run-terminal-reply.js";
import { listAgentIds, tryResolveAmbientOwnerAgentId } from "../agents/agent-scope-config.js";
import { abortAndDrainEmbeddedAgentRunByRunId } from "../agents/embedded-agent-runner/runs.js";
import { abortAndDrainEmbeddedAgentRun } from "../agents/embedded-agent.js";
import * as embeddedRuns from "../agents/embedded-agent-runner/runs.js";
import { loadPreparedInboundPluginRegistry } from "../agents/prepared-model-runtime.inbound-registry.js";
import { captureSessionEventTargetForHost } from "../auto-reply/reply/session-event-handoff.js";
import type { CliDeps } from "../cli/deps.types.js";
@ -490,8 +489,8 @@ export function buildGatewayCronService(params: {
reason: "cron_timeout",
};
const result = await (execution.runId
? abortAndDrainEmbeddedAgentRunByRunId({ ...cleanup, runId: execution.runId })
: abortAndDrainEmbeddedAgentRun(cleanup));
? embeddedRuns.abortAndDrainEmbeddedAgentRunByRunId({ ...cleanup, runId: execution.runId })
: embeddedRuns.abortAndDrainEmbeddedAgentRun(cleanup));
if (!result) {
cronLogger.warn(
{ jobId: job.id, sessionId: execution.sessionId, sessionKey: execution.sessionKey },

View file

@ -7,6 +7,8 @@ const graph = createRuntimeImportGraph(
repoRoot,
[
"src/gateway/method-scopes.ts",
"src/gateway/server-cron.ts",
"src/gateway/worker-environments/worker-exec-approval.ts",
"src/gateway/server-close.runtime.ts",
"src/gateway/server-methods/sessions-create.ts",
"src/gateway/server-methods/chat-send-external-entry.ts",
@ -42,6 +44,18 @@ function collectStaticValueImports(entryRelativePath: string): string[] {
}
describe("gateway startup import boundaries", () => {
it("prepares worker exec approvals without loading model completion", () => {
expect(
collectStaticValueImports("src/gateway/worker-environments/worker-exec-approval.ts"),
).not.toContain(path.normalize("src/agents/simple-completion-runtime.ts"));
});
it("starts cron without loading agent turn execution", () => {
const modules = collectStaticValueImports("src/gateway/server-cron.ts");
expect(modules).not.toContain(path.normalize("src/cron/isolated-agent/run.ts"));
expect(modules).not.toContain(path.normalize("src/agents/embedded-agent-runner/run.ts"));
});
it("prepares Gateway turns without local command or broad session runtimes", () => {
const modules = collectStaticValueImports("src/gateway/agent-turn/agent-turn-service.ts");
expect(modules).not.toContain(path.normalize("src/agents/agent-command-local.ts"));

View file

@ -73,10 +73,10 @@ it("reads remote skill status, cards and binary requirements through the workspa
agents: { entries: { main: { workspace: gateway } } },
};
const loadSkills = vi.fn(async (request: WorkspaceSkillSourceRequest) => ({
...readWorkspaceSkillSources({
...(await readWorkspaceSkillSources({
...request,
sourcePlan: resolveWorkspaceSkillSourcePlan(remote, { workspaceOnly: true }),
}),
})),
runtime: { platform: hostPlatform, bins: ["host-tool", "brew"] },
}));
const release = registerAgentWorkspaceAccess(gateway, {

View file

@ -118,10 +118,10 @@ vi.mock("./server-startup-model-runtime.js", async (importOriginal) => ({
...(await importOriginal<typeof import("./server-startup-model-runtime.js")>()),
publishConfiguredModelRuntimeSnapshots: async () => {},
}));
// mock-isolation: Transcript reload startup does not execute config-driven replies or their agent-runtime graph.
vi.mock("../auto-reply/reply/get-reply-from-config.runtime.js", () => ({
getReplyFromConfig: vi.fn(),
prewarmConfigDrivenReplyRuntime: async () => {},
// mock-isolation: Transcript reload startup does not execute the agent-runtime graph.
vi.mock("../auto-reply/reply/get-reply-run-helpers.js", () => ({
loadEmbeddedAgentRuntime: async () => {},
loadSessionUpdatesRuntime: async () => {},
}));
// mock-isolation: Transcript reload startup owns this import, not authenticated WebSocket connection dispatch.
vi.mock("./server/ws-connection/message-handler.js", () => ({

View file

@ -103,8 +103,10 @@ vi.mock("./server-startup-model-runtime.js", () => ({
publishConfiguredModelRuntimeSnapshots: async () => {},
hydrateConfiguredExternalCliAuth: async () => ({}),
}));
vi.mock("../auto-reply/reply/dispatch-from-config.runtime-loaders.js", () => ({
loadGetReplyFromConfigRuntime: async () => ({ prewarmConfigDrivenReplyRuntime: async () => {} }),
// mock-isolation: Restart-sentinel setup prepares runtimes without executing agent turns.
vi.mock("../auto-reply/reply/get-reply-run-helpers.js", () => ({
loadEmbeddedAgentRuntime: async () => {},
loadSessionUpdatesRuntime: async () => {},
}));
const { startGatewaySidecars } = await import("./server-startup-sidecars.js");
const sessionWorker = await vi.importActual<typeof SessionWorker>(

View file

@ -115,6 +115,8 @@ beforeEach(() => {
mocks.events.length = 0;
vi.clearAllMocks();
mocks.getMemoryCapabilityRegistration.mockReset();
vi.spyOn(process, "constrainedMemory").mockReturnValue(0);
vi.spyOn(process, "availableMemory").mockReturnValue(0);
});
afterEach(async () => {
@ -122,6 +124,7 @@ afterEach(async () => {
admission.release();
}
await Promise.all(sidecars.splice(0).map((sidecar) => Promise.resolve(sidecar.stop())));
vi.restoreAllMocks();
vi.useRealTimers();
resetGatewayWorkAdmission();
});
@ -336,6 +339,24 @@ function scheduleDependencyPreparation(options: Partial<PrewarmOptions> = {}) {
return sidecar;
}
it.each([
[0, 0, false],
[1000, 201, false],
[1000, 200, true],
] as const)(
"reserves constrained memory for requests (limit: %i, available: %i)",
async (limit, available, skipped) => {
vi.spyOn(process, "constrainedMemory").mockReturnValue(limit);
vi.spyOn(process, "availableMemory").mockReturnValue(available);
const handles = scheduleGatewayPrewarm(prewarmOptions());
sidecars.push(...handles);
expect(handles).toHaveLength(skipped ? 0 : 2);
await drainPrewarm();
expect(mocks.prewarmLocalWorkspaceTemplates).toHaveBeenCalledTimes(skipped ? 0 : 1);
expect(mocks.prewarmGatewaySessionHistory).toHaveBeenCalledTimes(skipped ? 0 : 1);
},
);
it.each(["close", "restart"] as const)(
"cancels dependency preparation on %s and joins installer cleanup",
async (kind) => {

View file

@ -172,6 +172,11 @@ export function scheduleGatewayHandlerPrewarm(params: {
export function scheduleGatewayPrewarm(
params: Parameters<typeof scheduleGatewayHandlerPrewarm>[0],
): GatewayIdleTaskHandle[] {
const memoryLimit = process.constrainedMemory();
// Leave scarce cgroup memory to requests; every warmup has a request-time loader.
if (memoryLimit > 0 && process.availableMemory() <= memoryLimit * 0.2) {
return [];
}
return [
scheduleGatewayHandlerPrewarm(params),
scheduleGatewayIdleTask({

View file

@ -52,7 +52,8 @@ const postAttachMocks = vi.hoisted(() => {
refreshPreparedModelRuntimeSnapshots: vi.fn<
typeof import("../agents/prepared-model-runtime.js").refreshPreparedModelRuntimeSnapshots
>(async () => {}),
prewarmConfigDrivenReplyRuntime: vi.fn(async () => {}),
loadEmbeddedAgentRuntime: vi.fn(async () => {}),
loadSessionUpdatesRuntime: vi.fn(async () => {}),
scheduleGatewayHandlerPrewarm: vi.fn(() => ({ stop: vi.fn() })),
};
});
@ -176,11 +177,19 @@ vi.mock("../agents/prepared-model-runtime.js", () => ({
}));
// mock-isolation: Observe reply prewarming without dispatching turns or loading a native agent runtime.
vi.mock("../auto-reply/reply/get-reply-from-config.runtime.js", () => ({
getReplyFromConfig: vi.fn(),
prewarmConfigDrivenReplyRuntime: postAttachMocks.prewarmConfigDrivenReplyRuntime,
vi.mock("../auto-reply/reply/get-reply-run-helpers.js", () => ({
loadEmbeddedAgentRuntime: postAttachMocks.loadEmbeddedAgentRuntime,
loadSessionUpdatesRuntime: postAttachMocks.loadSessionUpdatesRuntime,
prewarmReplyRunRuntimes: () => {
throw new Error("channel reply runner prewarmed during Gateway startup");
},
}));
// mock-isolation: Agent RPC startup must not load the channel reply resolver to prewarm shared runtimes.
vi.mock("../auto-reply/reply/get-reply-from-config.runtime.js", () => {
throw new Error("channel reply resolver loaded during Gateway startup");
});
// mock-isolation: Startup owns the import, not WebSocket request execution.
vi.mock("./server/ws-connection/message-handler.js", () => ({
attachGatewayWsMessageHandler: vi.fn(),

View file

@ -298,20 +298,16 @@ describe("startGatewayPostAttachRuntime", () => {
hoisted.resolveHooksGmailModel.mockReset();
hoisted.resolveHooksGmailModel.mockReturnValue(null);
hoisted.loadFullModelCatalog.mockClear();
hoisted.loadModelCatalog.mockReset();
hoisted.loadModelCatalog.mockResolvedValue({});
hoisted.getModelRefStatus.mockReset();
hoisted.getModelRefStatus.mockReturnValue({
hoisted.loadModelCatalog.mockReset().mockResolvedValue({});
hoisted.getModelRefStatus.mockReset().mockReturnValue({
key: "openai/gpt-5.4",
allowed: true,
inCatalog: true,
});
hoisted.prepareModelRuntimeSnapshot.mockReset();
hoisted.prepareModelRuntimeSnapshot.mockResolvedValue({});
hoisted.refreshPreparedModelRuntimeSnapshots.mockReset();
hoisted.refreshPreparedModelRuntimeSnapshots.mockResolvedValue(undefined);
hoisted.prewarmConfigDrivenReplyRuntime.mockReset();
hoisted.prewarmConfigDrivenReplyRuntime.mockResolvedValue(undefined);
hoisted.prepareModelRuntimeSnapshot.mockReset().mockResolvedValue({});
hoisted.refreshPreparedModelRuntimeSnapshots.mockReset().mockResolvedValue(undefined);
hoisted.loadEmbeddedAgentRuntime.mockReset().mockResolvedValue(undefined);
hoisted.loadSessionUpdatesRuntime.mockReset().mockResolvedValue(undefined);
hoisted.scheduleGatewayHandlerPrewarm.mockClear();
});
@ -411,7 +407,7 @@ describe("startGatewayPostAttachRuntime", () => {
let closing = false;
const hooksStarted = createDeferred();
const requestsPrepared = createDeferred();
hoisted.prewarmConfigDrivenReplyRuntime.mockImplementationOnce(async () => {
hoisted.refreshPreparedModelRuntimeSnapshots.mockImplementationOnce(async () => {
requestsPrepared.resolve();
});
hoisted.prepareInternalHooks.mockImplementationOnce(() => {
@ -1486,6 +1482,15 @@ describe("startGatewayPostAttachRuntime", () => {
});
});
it("reaches readiness without loading request-owned agent runtimes", async () => {
const onSidecarsReady = vi.fn();
await startGatewayPostAttachRuntime({ ...createPostAttachParams(), onSidecarsReady });
expect(onSidecarsReady).toHaveBeenCalledOnce();
expect(hoisted.loadEmbeddedAgentRuntime).not.toHaveBeenCalled();
expect(hoisted.loadSessionUpdatesRuntime).not.toHaveBeenCalled();
});
it("prepares the model runtime with the active Gateway plugin registry", async () => {
const pluginRegistry = createPostAttachParams().pluginRegistry;
hoisted.refreshPreparedModelRuntimeSnapshots.mockImplementationOnce(async () => {
@ -1500,22 +1505,19 @@ describe("startGatewayPostAttachRuntime", () => {
expect(getPluginRuntimeGatewayRequestScope()).toBeUndefined();
});
it.each(["model", "reply", "connection"] as const)(
it.each(["model", "connection"] as const)(
"prepares request runtimes concurrently and keeps readiness blocked on %s",
async (lastRuntime) => {
const gates = {
model: createDeferred(),
reply: createDeferred(),
connection: createDeferred(),
};
const started = {
model: createDeferred(),
reply: createDeferred(),
connection: createDeferred(),
};
const completed = {
model: createDeferred(),
reply: createDeferred(),
connection: createDeferred(),
};
const prepare = async (name: keyof typeof gates) => {
@ -1524,7 +1526,6 @@ describe("startGatewayPostAttachRuntime", () => {
completed[name].resolve();
};
hoisted.refreshPreparedModelRuntimeSnapshots.mockImplementationOnce(() => prepare("model"));
hoisted.prewarmConfigDrivenReplyRuntime.mockImplementationOnce(() => prepare("reply"));
const trace = createStartupTraceRecorder();
const startChannels = vi.fn(async () => {});
const onSidecarsReady = vi.fn();
@ -1551,7 +1552,7 @@ describe("startGatewayPostAttachRuntime", () => {
);
expect(startChannels).not.toHaveBeenCalled();
expect(onSidecarsReady).not.toHaveBeenCalled();
for (const name of ["model", "reply", "connection"] as const) {
for (const name of ["model", "connection"] as const) {
if (name !== lastRuntime) {
gates[name].resolve();
await completed[name].promise;
@ -1578,20 +1579,14 @@ describe("startGatewayPostAttachRuntime", () => {
it("joins pending request imports after preparation fails and preserves the model failure", async () => {
const model = createDeferred();
const reply = createDeferred();
const connection = createDeferred();
const modelStarted = createDeferred();
const replyStarted = createDeferred();
const connectionStarted = createDeferred();
const modelFailure = new Error("model preparation failed");
hoisted.refreshPreparedModelRuntimeSnapshots.mockImplementationOnce(() => {
modelStarted.resolve();
return model.promise;
});
hoisted.prewarmConfigDrivenReplyRuntime.mockImplementationOnce(() => {
replyStarted.resolve();
return reply.promise;
});
const trace = createStartupTraceRecorder();
const startChannels = vi.fn(async () => {});
const onSidecarsReady = vi.fn();
@ -1624,11 +1619,10 @@ describe("startGatewayPostAttachRuntime", () => {
try {
await awaitGateBeforeSettlement(
Promise.all([modelStarted.promise, replyStarted.promise, connectionStarted.promise]),
Promise.all([modelStarted.promise, connectionStarted.promise]),
startup,
"startup settled before all request runtimes started",
);
reply.reject(new Error("reply import failed"));
model.reject(modelFailure);
await vi.dynamicImportSettled();
expect(settled).toBe(false);
@ -1638,31 +1632,30 @@ describe("startGatewayPostAttachRuntime", () => {
expect(onSidecarsReady).not.toHaveBeenCalled();
} finally {
model.resolve();
reply.resolve();
connection.resolve();
await outcome;
}
});
it("does not start channels when close begins during deferred sidecar preparation", async () => {
const replyRuntime = createDeferred();
const modelRuntime = createDeferred();
let closing = false;
hoisted.prewarmConfigDrivenReplyRuntime.mockReturnValueOnce(replyRuntime.promise);
hoisted.refreshPreparedModelRuntimeSnapshots.mockReturnValueOnce(modelRuntime.promise);
const params = createPostAttachParams({ sidecarStartup: "defer", isClosing: () => closing });
const onChannelsStarted = vi.fn();
const runtime = await startGatewayPostAttachRuntime({ ...params, onChannelsStarted });
try {
await waitForGatewayTestState(() =>
expect(hoisted.prewarmConfigDrivenReplyRuntime).toHaveBeenCalledOnce(),
expect(hoisted.refreshPreparedModelRuntimeSnapshots).toHaveBeenCalledOnce(),
);
closing = true;
replyRuntime.resolve();
modelRuntime.resolve();
await expect(runtime.startupSettled).resolves.toBeUndefined();
expect(params.startChannels).not.toHaveBeenCalled();
expect(onChannelsStarted).not.toHaveBeenCalled();
expect(params.unlockStartupMethods).not.toHaveBeenCalled();
} finally {
replyRuntime.resolve();
modelRuntime.resolve();
await runtime.startupSettled;
}
});

View file

@ -1,4 +1,3 @@
import { loadGetReplyFromConfigRuntime } from "../auto-reply/reply/dispatch-from-config.runtime-loaders.js";
import type { CliDeps } from "../cli/deps.types.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { type InternalHookSelection, resolveInternalHookSelection } from "../hooks/configured.js";
@ -168,11 +167,6 @@ export async function startGatewaySidecars(params: {
),
);
})(),
// Readiness owns process-stable request imports so the first turn and connection do not.
measureStartup(params.startupTrace, "sidecars.reply-runtime", async () => {
const { prewarmConfigDrivenReplyRuntime } = await loadGetReplyFromConfigRuntime();
await prewarmConfigDrivenReplyRuntime();
}),
measureStartup(
params.startupTrace,
"sidecars.connection-runtime",

View file

@ -56,7 +56,6 @@ const STARTUP_PROGRESS_PHASES = new Set([
"sidecars.worker-environments.publications",
"sidecars.internal-hooks",
"sidecars.model-runtime",
"sidecars.reply-runtime",
"sidecars.chat-metadata",
"sidecars.channels",
"sidecars.plugin-services",

View file

@ -26,6 +26,11 @@ import {
} from "./skill-library-authoring.js";
import { createWorkerConnectionIdentity } from "./worker-environments/connection-identity.test-support.js";
// mock-isolation: ordinary authoring must not load archive or ClawHub import machinery.
vi.mock("../skills/library/import.js", () => {
throw new Error("ordinary skill authoring must not load archive import runtime");
});
const catalogReleases: Array<() => void> = [];
const temps = useAutoCleanupTempDirTracker((cleanup) =>
afterEach(async () => {

View file

@ -54,3 +54,17 @@ export function bindPostgresPool(endpoints: PostgresPoolEndpoints): void {
}
postgresPoolState.endpoints = endpoints;
}
/** Executable cleanup calls this after SQL hosts and native backend-close settlement. */
export async function closePostgresPool(): Promise<void> {
const workers = postgresPoolState.workers;
postgresPoolState.workers = undefined;
for (const { port } of postgresPoolState.endpoints?.services ?? []) {
port.close();
}
postgresPoolState.endpoints = undefined;
postgresPoolState.ready = [];
postgresPoolState.refresh = undefined;
postgresPoolState.failure = undefined;
await Promise.all(workers?.map((worker) => worker.terminate()) ?? []);
}

View file

@ -17,11 +17,14 @@ import { resolveRuntimeWorkerThreadExecArgv } from "../runtime-worker-url.js";
import { recordPostgresPoolSelfRoot, trackNativeWorkerForCpu } from "../worker-cpu.js";
import { workerScriptName } from "../worker-diagnostic-script.js";
import {
closePostgresPool,
postgresPoolState as state,
type PostgresPoolEndpoint,
type PostgresPoolEndpoints,
} from "./pool-state.js";
export { closePostgresPool } from "./pool-state.js";
export type PostgresPoolSetup =
| { host: MessagePort }
| { connection: import("./protocol.js").BridgeWorkerData };
@ -215,17 +218,3 @@ export async function preparePostgresPoolForUpdate(nextStep: string): Promise<vo
}
}
}
/** Executable cleanup calls this after SQL hosts and native backend-close settlement. */
export async function closePostgresPool(): Promise<void> {
const workers = state.workers;
state.workers = undefined;
for (const { port } of state.endpoints?.services ?? []) {
port.close();
}
state.endpoints = undefined;
state.ready = [];
state.refresh = undefined;
state.failure = undefined;
await Promise.all(workers?.map((worker) => worker.terminate()) ?? []);
}

View file

@ -29,13 +29,14 @@ import {
import { createCpuTrackedWorker, receiveWorkerMemoryPort } from "./worker-cpu.js";
import { observeWorkerMessage, workerMessagePayloadBytes } from "./worker-message-metrics.js";
import { NativeWorker } from "./worker-native-handle.js";
import type {
NativeWorkerReply,
NativeWorkerRuntime,
NativeWorkerResourceDescriptor,
NativeWorkerResourceConnection,
NativeWorkerTaskPorts,
RetainedNativeWorker,
import {
NATIVE_WORKER_LIFETIMES_KEY,
type NativeWorkerReply,
type NativeWorkerRuntime,
type NativeWorkerResourceDescriptor,
type NativeWorkerResourceConnection,
type NativeWorkerTaskPorts,
type RetainedNativeWorker,
} from "./worker-native-lifecycle.types.js";
import { nativePortIsOpen } from "./worker-native-port.js";
@ -79,7 +80,7 @@ type NativeSource = RetainedNativeWorkerSource & {
};
const lifetime = resolveGlobalSingleton(
Symbol.for("openclaw.nativeWorkerLifetimes"),
NATIVE_WORKER_LIFETIMES_KEY,
(): {
nextId: number;
sources: WeakMap<RuntimeWorkerGeneration, NativeSource>;

View file

@ -7,6 +7,8 @@ import type {
import type { captureSqliteWorkerEnvironmentData } from "./bun-sqlite-library.js";
import type { NativeWorkerFailure } from "./worker-native-error.js";
export const NATIVE_WORKER_LIFETIMES_KEY = Symbol.for("openclaw.nativeWorkerLifetimes");
export type { NativeWorkerEvents, RetainedNativeWorker } from "@openclaw/worker-runtime/lifecycle";
/** The host endpoint never crosses the supervisor transport. */

View file

@ -411,6 +411,41 @@ describe("diagnostic memory", () => {
);
});
it.each([
[1000, 210, false],
[1000, 200, true],
[4000, 200, false],
] as const)(
"reclaims constrained process headroom despite a heap warning: limit=%i available=%i reclaim=%s",
(processMemoryLimitBytes, available, reclaim) => {
mockProcessPlatform("linux");
vi.spyOn(process, "availableMemory").mockReturnValue(available);
const pressure = channel("openclaw.memory.critical");
const retireIdle = vi.fn();
pressure.subscribe(retireIdle);
try {
const options = {
emitSample: false,
isBunRuntime: false,
processMemoryLimitBytes,
physicalMemoryBytes: 4000,
heapSizeLimitBytes: 1000,
memoryUsage: memoryUsage({ heapUsed: 850 }),
};
emitDiagnosticMemorySample(options, false);
expect(retireIdle).toHaveBeenCalledTimes(reclaim ? 1 : 0);
expect(events).toEqual([]);
emitDiagnosticMemorySample(options);
expect(retireIdle).toHaveBeenCalledTimes(reclaim ? 2 : 0);
expect(events).toEqual([
expect.objectContaining({ level: "warning", reason: "heap_threshold" }),
]);
} finally {
pressure.unsubscribe(retireIdle);
}
},
);
it.each([
["linux", false, 0, 4000],
["linux", false, 4000, 4000],

View file

@ -371,7 +371,11 @@ export function emitDiagnosticMemorySample(
options?.thresholds,
processMemory,
);
if (pressure?.level === "critical") {
if (
pressure?.level === "critical" ||
(processMemory &&
processMemory.usedBytes >= Math.floor(processMemory.limitBytes * WARNING_RATIO))
) {
memoryPressure.publish(undefined);
}
if (

View file

@ -5,6 +5,7 @@ import {
readGlobalSingleton,
resolveGlobalMap as mapFor,
resolveGlobalSingleton,
resetGlobalSingletonLifecycleState,
} from "./global-singleton.js";
const TEST_KEY = Symbol("global-singleton:test");
@ -15,6 +16,28 @@ const register = (reset: () => void | Promise<void>, key = TEST_KEY) =>
afterEach(() => [TEST_KEY, TEST_MAP_KEY].forEach((key) => Reflect.deleteProperty(globalThis, key)));
describe("resolveGlobalSingleton", () => {
it("resets only the current registered owner without creating an absent one", async () => {
const absent = Symbol("absent-owner");
await resetGlobalSingletonLifecycleState(absent);
expect(readGlobalSingleton(absent)).toBeUndefined();
const [oldReset, currentReset, otherReset] = [vi.fn(), vi.fn(), vi.fn()];
register(oldReset);
register(currentReset);
register(otherReset, TEST_MAP_KEY);
await resetGlobalSingletonLifecycleState(TEST_KEY);
expect(oldReset).not.toHaveBeenCalled();
expect(currentReset).toHaveBeenCalledOnce();
expect(otherReset).not.toHaveBeenCalled();
});
it("propagates the registered owner's cleanup failure", async () => {
const error = new Error("owner cleanup failed");
register(() => Promise.reject(error));
await expect(resetGlobalSingletonLifecycleState(TEST_KEY)).rejects.toBe(error);
register(() => {});
});
it("reuses an initialized singleton", () => {
const create = vi.fn(() => ({ value: 1 }));

View file

@ -33,6 +33,11 @@ export function readGlobalSingleton(key: symbol): unknown {
return Object.hasOwn(globalStore, key) ? globalStore[key] : undefined;
}
/** Run an existing owner's cleanup without importing or initializing that owner. */
export async function resetGlobalSingletonLifecycleState(key: symbol): Promise<void> {
await resolveGlobalSingletonResetRegistry().get(key)?.reset();
}
/** Resolves a process-local singleton for caches and registries that tolerate helper lookup. */
export function resolveGlobalSingleton<T>(
key: symbol,

View file

@ -53,13 +53,13 @@ async function remoteFixture(name: string, description: string, bins?: string[])
const release = registerAgentWorkspaceAccess(gateway, {
bridge: bridge(),
loadSkills: async (request) => ({
...readWorkspaceSkillSources({
...(await readWorkspaceSkillSources({
...request,
sourcePlan:
bins && request.sourcePlan.roots.length === 0
? request.sourcePlan
: resolveWorkspaceSkillSourcePlan(remote, { workspaceOnly: true }),
}),
})),
...(bins ? { runtime: { platform: process.platform, bins } } : {}),
}),
});

View file

@ -142,11 +142,13 @@ describe("remote skill discovery", () => {
] as const) {
await writeNamedSkill(dir, name, description);
}
sources.entries = readWorkspaceSkillSources({
sourcePlan: resolveWorkspaceSkillSourcePlan(remote, { workspaceOnly: true }),
limits: resolveSkillDiscoveryLimits(),
additionalBins: [],
}).entries;
sources.entries = (
await readWorkspaceSkillSources({
sourcePlan: resolveWorkspaceSkillSourcePlan(remote, { workspaceOnly: true }),
limits: resolveSkillDiscoveryLimits(),
additionalBins: [],
})
).entries;
const loadSkills = vi.fn(async (_request: WorkspaceSkillSourceRequest) => sources);
bind(loadSkills);
const params = { ...options, config, agentId: "main" };

View file

@ -1,7 +1,6 @@
import path from "node:path";
import type { OpenClawConfig } from "../../config/types.openclaw.js";
import { shouldRejectHardlinkedPluginFiles } from "../../plugins/hardlink-policy.js";
import { readWorkspaceSkillStatusFacts } from "../discovery/status-files.js";
import type { SkillEntry } from "../types.js";
import { resolveBundledSkillsDir } from "./bundled-dir.js";
import { hasBinary, resolveSkillRequiredBins } from "./config.js";
@ -22,10 +21,10 @@ import type {
} from "./workspace-skill-sources.types.js";
/** Run on the workspace host using an admitted source plan and native discovery limits. */
export function readWorkspaceSkillSources(
export async function readWorkspaceSkillSources(
request: WorkspaceSkillSourceRequest,
batch?: SkillSourceReadBatch,
): WorkspaceSkillSources {
): Promise<WorkspaceSkillSources> {
const config: OpenClawConfig = {
skills: {
limits: request.limits,
@ -57,20 +56,19 @@ export function readWorkspaceSkillSources(
]
.filter(hasBinary)
.toSorted();
const status = request.status
? (await import("../discovery/status-files.js")).readWorkspaceSkillStatusFacts({
entries: mergeSkillRecords(entries, request.sourcePlan.workspaceDir),
workspaceDir: request.sourcePlan.workspaceDir,
managedSkillsDir: request.sourcePlan.managedSkillsDir,
skillCardKey: request.status.skillCardKey,
})
: undefined;
return {
entries,
executionEntries,
runtime: { platform: process.platform, bins },
...(request.status
? {
status: readWorkspaceSkillStatusFacts({
entries: mergeSkillRecords(entries, request.sourcePlan.workspaceDir),
workspaceDir: request.sourcePlan.workspaceDir,
managedSkillsDir: request.sourcePlan.managedSkillsDir,
skillCardKey: request.status.skillCardKey,
}),
}
: {}),
...(status ? { status } : {}),
};
}

View file

@ -9,11 +9,15 @@ import {
} from "./discovery-worker-contract.js";
const worker = vi.hoisted(() => ({
read: undefined as ((input: SkillsDiscoveryInput) => SkillsDiscoveryOutput) | undefined,
read: undefined as
| ((input: SkillsDiscoveryInput) => SkillsDiscoveryOutput | Promise<SkillsDiscoveryOutput>)
| undefined,
}));
vi.mock("../../infra/worker-task-server.js", async (importOriginal) => ({
...(await importOriginal<typeof import("../../infra/worker-task-server.js")>()),
serveWorkerTasks: (read: (input: SkillsDiscoveryInput) => SkillsDiscoveryOutput) => {
serveWorkerTasks: (
read: (input: SkillsDiscoveryInput) => SkillsDiscoveryOutput | Promise<SkillsDiscoveryOutput>,
) => {
worker.read = read;
},
}));
@ -29,6 +33,10 @@ vi.mock("../../plugins/plugin-metadata-snapshot.js", () => {
vi.mock("../loading/plugin-skill-bundle.js", () => {
throw new Error("Skill discovery must not load plugin skill preview readers");
});
// mock-isolation: Ordinary skill scans do not need status cards or registry links.
vi.mock("../discovery/status-files.js", () => {
throw new Error("Skill scans must not load status readers");
});
const temporary = useAutoCleanupTempDirTracker(afterEach);
afterEach(() => vi.unstubAllEnvs());
@ -45,7 +53,7 @@ it("scans workspace and prepared plugin skills without loading metadata discover
if (!worker.read) {
throw new Error("Discovery worker did not register its task handler");
}
const result = worker.read({
const result = await worker.read({
kind: "scan",
path: "",
requests: [
@ -94,7 +102,7 @@ it("scans workspace and prepared plugin skills without loading metadata discover
],
});
const plan = worker.read({
const plan = await worker.read({
kind: "plan",
input: {
workspaceDir,

View file

@ -8,7 +8,6 @@ import {
type PluginCache,
} from "../../plugins/plugin-cache.js";
import { pinConfigDir } from "../../utils.js";
import { readWorkspaceSkillStatusFacts } from "../discovery/status-files.js";
import {
resolvePluginSkillRoots,
resolvePluginSkillRootsFromContributions,
@ -62,7 +61,7 @@ function resolveInputPlan(input: SkillsSourcePlanInput, cache: PluginCache = cre
});
}
serveWorkerTasks<SkillsDiscoveryOutput>((input, channel) => {
serveWorkerTasks<SkillsDiscoveryOutput>(async (input, channel) => {
// SAFETY: This private worker only receives the discovery owner's typed requests.
const request = input as SkillsDiscoveryInput;
channel?.consumeInput();
@ -86,6 +85,7 @@ serveWorkerTasks<SkillsDiscoveryOutput>((input, channel) => {
}
if (request.kind === "status") {
applyEnvironment(request.environment);
const { readWorkspaceSkillStatusFacts } = await import("../discovery/status-files.js");
return { kind: "status", facts: readWorkspaceSkillStatusFacts(request.input) };
}
if (request.path === undefined) {
@ -103,64 +103,64 @@ serveWorkerTasks<SkillsDiscoveryOutput>((input, channel) => {
plans.set(key, plan);
return plan;
};
return {
kind: "scan",
results: request.requests.map(({ key, environment, selection }) => {
applyEnvironment(environment);
const resolvedPlan =
selection.kind === "planned"
const results: Extract<SkillsDiscoveryOutput, { kind: "scan" }>["results"] = [];
// Each read must finish before the next workspace replaces process.env.
for (const { key, environment, selection } of request.requests) {
applyEnvironment(environment);
const resolvedPlan =
selection.kind === "planned"
? selectPlan(selection.plan.input)
: selection.plan
? selectPlan(selection.plan.input)
: selection.plan
? selectPlan(selection.plan.input)
: selection.request.sourcePlan;
const sourceRequest = {
...selection.request,
sourcePlan:
selection.kind === "selected" && selection.plan?.scope === "gateway"
? splitSkillSourcePlan(resolvedPlan).gatewayPlan
: resolvedPlan,
};
const environmentKey = JSON.stringify(environment);
let batch = sourceReads.get(environmentKey);
if (!batch) {
batch = new Map();
sourceReads.set(environmentKey, batch);
: selection.request.sourcePlan;
const sourceRequest = {
...selection.request,
sourcePlan:
selection.kind === "selected" && selection.plan?.scope === "gateway"
? splitSkillSourcePlan(resolvedPlan).gatewayPlan
: resolvedPlan,
};
const environmentKey = JSON.stringify(environment);
let batch = sourceReads.get(environmentKey);
if (!batch) {
batch = new Map();
sourceReads.set(environmentKey, batch);
}
const sources = await readWorkspaceSkillSources(sourceRequest, batch);
// Fingerprints need precedence winners, not collision diagnostics.
const entries = mergeSkillRecords(sources.entries, key, false);
appendLowerPrecedenceSkillRecords(
entries,
mergeSkillRecords(sources.executionEntries, key, false),
() => {},
);
// Generated plugin links are shared outputs; watch their canonical sources instead.
const watchDirectories = new Set([
...sourceRequest.sourcePlan.roots.map((root) => root.dir),
...(sourceRequest.executionWorkspaceDir
? resolveWorkspaceSkillDirectories(sourceRequest.executionWorkspaceDir).map(
(root) => root.dir,
)
: []),
]);
const physicalRoots = [...watchDirectories].map((directory) => tryRealpath(directory));
// Workspace entries retain configured paths; watch their admitted physical targets.
for (const { skill } of [...sources.entries, ...sources.executionEntries]) {
const target = tryRealpath(skill.baseDir);
if (
target &&
!physicalRoots.some((directory) => directory && isPathInside(directory, target))
) {
watchDirectories.add(target);
}
const sources = readWorkspaceSkillSources(sourceRequest, batch);
// Fingerprints need precedence winners, not collision diagnostics.
const entries = mergeSkillRecords(sources.entries, key, false);
appendLowerPrecedenceSkillRecords(
entries,
mergeSkillRecords(sources.executionEntries, key, false),
() => {},
);
// Generated plugin links are shared outputs; watch their canonical sources instead.
const watchDirectories = new Set([
...sourceRequest.sourcePlan.roots.map((root) => root.dir),
...(sourceRequest.executionWorkspaceDir
? resolveWorkspaceSkillDirectories(sourceRequest.executionWorkspaceDir).map(
(root) => root.dir,
)
: []),
]);
const physicalRoots = [...watchDirectories].map((directory) => tryRealpath(directory));
// Workspace entries retain configured paths; watch their admitted physical targets.
for (const { skill } of [...sources.entries, ...sources.executionEntries]) {
const target = tryRealpath(skill.baseDir);
if (
target &&
!physicalRoots.some((directory) => directory && isPathInside(directory, target))
) {
watchDirectories.add(target);
}
}
return {
key,
sources,
watchDirectories: [...watchDirectories],
plan: selection.plan ? resolvedPlan : undefined,
fingerprint: sha256Hex(JSON.stringify([entries, sources.runtime])),
};
}),
};
}
results.push({
key,
sources,
watchDirectories: [...watchDirectories],
plan: selection.plan ? resolvedPlan : undefined,
fingerprint: sha256Hex(JSON.stringify([entries, sources.runtime])),
});
}
return { kind: "scan", results };
});

View file

@ -26,7 +26,7 @@ import type {
WorkspaceSkillSourceRequest,
WorkspaceSkillSources,
} from "../loading/workspace-skill-sources.types.js";
import type { SkillSnapshot } from "../types.js";
import { SKILLS_DISCOVERY_KEY, type SkillSnapshot } from "../types.js";
import {
captureSkillsDiscoveryEnvironment,
type SkillsDiscoveryEnvironment,
@ -82,7 +82,7 @@ type DiscoveryState = {
};
const state = resolveGlobalSingleton<DiscoveryState>(
Symbol.for("openclaw.skillsDiscovery"),
SKILLS_DISCOVERY_KEY,
() => ({ sources: new Map(), plans: new Map(), catalog: new Map() }),
() => closeSkillsWatchers(),
);

View file

@ -24,6 +24,21 @@ it("validates dependency recipes without source paths", async () => {
expect(messages).toMatchObject([expected]);
});
it("serializes completed skill discovery", async () => {
const f = fixture();
await f.invoke("discovery", {
sourcePlan: {
workspaceDir: f.workspace,
managedSkillsDir: path.join(f.home, "managed"),
pluginSkillRoots: [],
roots: [],
},
limits: {},
additionalBins: [],
});
expect(f.messages).toMatchObject([{ entries: [], executionEntries: [] }]);
});
function fixture() {
const home = temporary.make("skills-worker-");
const workspace = path.join(home, "workspace");

View file

@ -357,7 +357,7 @@ export async function serveWorkspaceSkills(options: {
assertWorkspace(discovery, workspace);
const { readWorkspaceSkillSources } =
await import("../loading/workspace-skill-source-reader.js");
await write(readWorkspaceSkillSources(discovery));
await write(await readWorkspaceSkillSources(discovery));
return;
}
default:

View file

@ -1,6 +1,8 @@
import type { SkillLibraryFile } from "../../packages/gateway-protocol/src/schema/skill-library.js";
import type { Skill } from "./loading/skill-contract.js";
export const SKILLS_DISCOVERY_KEY = Symbol.for("openclaw.skillsDiscovery");
export type SkillInstallSpec = {
id?: string;
kind: "brew" | "node" | "go" | "uv" | "download";

View file

@ -28,11 +28,12 @@ import { createStateDatabaseCacheAdmission } from "./openclaw-state-db-cache.adm
import { createStateDatabaseIdleRetirement } from "./openclaw-state-db-cache.idle.js";
import type { StateDatabaseLifecycle } from "./openclaw-state-db-cache.types.js";
import { createStateDatabaseWalOwner } from "./openclaw-state-db-cache.wal.js";
import type {
OpenClawStateDatabase,
OpenClawStateDatabaseCloseOptions,
OpenClawStateDatabaseLifecycleEvent,
StateDatabaseHandle,
import {
STATE_DATABASE_LIFECYCLE_KEY,
type OpenClawStateDatabase,
type OpenClawStateDatabaseCloseOptions,
type OpenClawStateDatabaseLifecycleEvent,
type StateDatabaseHandle,
} from "./openclaw-state-db-contract.js";
import {
closeStateDatabaseHandle,
@ -45,7 +46,6 @@ import { openClawStateSnapshotOwners } from "./openclaw-state-db-snapshot-owner.
import { resolveDatabasePath } from "./openclaw-state-db.paths.js";
import type { OpenClawStateWorkerContext } from "./openclaw-state-worker-context.types.js";
const STATE_DATABASE_LIFECYCLE_KEY = Symbol.for("openclaw.stateDatabaseLifecycle");
const stateDatabaseLifecycle = resolveGlobalSingleton<StateDatabaseLifecycle>(
STATE_DATABASE_LIFECYCLE_KEY,
() => ({

View file

@ -3,6 +3,8 @@ import type { EngineConnection } from "../infra/sql-connection.js";
import type { SqliteWalMaintenance } from "../infra/sqlite-wal.js";
import type { DatabasePathIdentity } from "../infra/sqlite-worker-identity.js";
export const STATE_DATABASE_LIFECYCLE_KEY = Symbol.for("openclaw.stateDatabaseLifecycle");
export type OpenClawStateSchemaReadAdmission = (database: DatabaseSync) => (() => void) | undefined;
export const MEETING_TRANSCRIPT_START_TIME_SCHEMA_VERSION = 23;

View file

@ -1,9 +1,5 @@
import type { ExecutionIdentityInspectionOutcome } from "../audit/execution-identity-inspection.types.js";
import { isChannelIngressReadCommand } from "../channels/message/ingress-queue-read-contract.js";
import {
isCronStateReadCommand,
readCronStateCommandInDatabase,
} from "../cron/store/read-command.js";
import { isWorkspaceJournalReadCommand } from "../gateway/worker-environments/placement-workspace-journal.types.js";
import type { EngineConnection } from "../infra/sql-connection.js";
import { isUpdateRunAdmissionReadCommand } from "../infra/update-run-admission.types.js";
@ -104,7 +100,13 @@ export async function prepareOpenClawStateRead(
const { readBackupRunsInDatabase } = await import("./backup-run-records.kernel.js");
return (db: EngineConnection) => ({ type: command.type, runs: readBackupRunsInDatabase(db) });
}
if (isCronStateReadCommand(command)) {
if (
command.type === "cron.currentReceipt" ||
command.type === "cron.scratch" ||
command.type === "cron.jobNames" ||
command.type === "cron.quarantine"
) {
const { readCronStateCommandInDatabase } = await import("../cron/store/read-command.js");
return (db: EngineConnection) => readCronStateCommandInDatabase(db, command);
}
if (

View file

@ -111,12 +111,14 @@ it("keeps shared-state and session-upstream workers independent of host runtime
findSourceImportBackedges(
[
"src/state/openclaw-state.worker.ts",
"src/state/openclaw-state-read-handlers.ts",
"src/sessions/session-upstream-links.worker.ts",
// Loaded lazily by the shared-state worker; dynamic imports are separate entries.
"src/infra/update-run-dispatch.worker.ts",
],
[
"src/config/io.snapshot.ts",
"src/cron/store/read-command.ts",
"src/config/validation-core.ts",
"src/plugins/loader-runtime-load.ts",
"src/channels/plugins/registry.ts",

View file

@ -524,12 +524,12 @@ function makeLegacyFixture(source = legacyCompanionSource, fileName?: string) {
};
}
it.each(["//#region", "//"])(
it.each(["//#region", "//", "//!"])(
"uses generated source ownership only when the compatibility gate is enabled (%s)",
(label) => {
const source = legacyCompanionSource
.replace("//#region", label)
.replace("//#endregion", label === "//" ? "" : "//#endregion");
.replace("//#region ", label === "//!" ? label : `${label} `)
.replace("//#endregion", label === "//#region" ? "//#endregion" : "");
const { packageRoot, legacyErrors } = makeLegacyFixture(source);
expectDependencyErrors(packageRoot, "@discordjs/voice");
expect(legacyErrors()).toStrictEqual([]);

View file

@ -90,6 +90,7 @@ it("rejects private QA and local build artifacts while allowing runtime files",
it.each([
["dist/entry.js", "//#region extensions/qa-lab/src/cli.ts\n", "//#region extensions/qa-lab/"],
["dist/entry.js", "// extensions/qa-lab/src/cli.ts\n", "// extensions/qa-lab/src/cli.ts"],
["dist/entry.js", "//!extensions/qa-lab/src/cli.ts\n", "//!extensions/qa-lab/src/cli.ts"],
[
PACKAGE_DIST_INVENTORY_RELATIVE_PATH,
JSON.stringify(["dist/extensions/qa-lab/runtime-api.js"]),

View file

@ -218,11 +218,11 @@ it.skipIf(process.platform === "win32")("flags non-executable skill shell script
});
describe("collectForbiddenPackPaths", () => {
it.each(["//#region", "//"])(
it.each(["//#region ", "// ", "//!"])(
"blocks root dist chunks that still reference private qa lab sources (%s)",
(label) => {
const tempRoot = fixture({
"dist/entry.js": `${label} extensions/qa-lab/src/runtime-api.ts\n`,
"dist/entry.js": `${label}extensions/qa-lab/src/runtime-api.ts\n`,
"CHANGELOG.md": "local QA notes mention extensions/qa-lab/\n",
});
@ -232,11 +232,11 @@ describe("collectForbiddenPackPaths", () => {
},
);
it.each(["//#region", "//"])(
it.each(["//#region ", "// ", "//!"])(
"blocks root plugin SDK declarations that still reference private test helpers (%s)",
(label) => {
const file = "dist/plugin-sdk/channel-test-helpers.d.ts";
const tempRoot = fixture({ [file]: `${label} src/plugin-sdk/test-helpers/session.ts\n` });
const tempRoot = fixture({ [file]: `${label}src/plugin-sdk/test-helpers/session.ts\n` });
expect(collectForbiddenPackContentPaths([file], tempRoot)).toEqual([file]);
},

View file

@ -21,9 +21,10 @@ const program = {
};
describe("standalone fixed code generation", () => {
it.skipIf(process.platform === "win32").each(["//#region", "//"])(
it.skipIf(process.platform === "win32").each(["//#region", "//", "//!"])(
"captures remaining static programs (%s)",
(label) => {
const marker = label === "//!" ? label : `${label} `;
const root = fs.realpathSync(dirs.make("standalone-capture-"));
const runtimeRoot = path.join(root, "runtime");
const dist = path.join(runtimeRoot, "dist");
@ -38,7 +39,7 @@ describe("standalone fixed code generation", () => {
fs.writeFileSync(
filename,
`import { compileFunction } from "node:vm";
${label} ${owner}
${marker}${owner}
const SOURCE${index} = "const offset = ${index + 1};";
export const run = compileFunction(\`\${SOURCE${index}}\nreturn value + offset + ${index};\`, ["value"]);`,
);
@ -46,7 +47,7 @@ export const run = compileFunction(\`\${SOURCE${index}}\nreturn value + offset +
});
fs.writeFileSync(
path.join(dist, "workspace-manifest-script.runtime-fixture.mjs"),
`${label} src/gateway/worker-environments/workspace-manifest-script.runtime.ts\nexport function run() { return 99; }\n`,
`${marker}src/gateway/worker-environments/workspace-manifest-script.runtime.ts\nexport function run() { return 99; }\n`,
);
const receiver = path.join(dist, "worker/workspace-rsync-receiver.mjs");
fs.writeFileSync(
@ -54,7 +55,7 @@ export const run = compileFunction(\`\${SOURCE${index}}\nreturn value + offset +
`import { compileFunction } from "node:vm";
const REMOTE_WORKSPACE_ACCEPTED_RSYNC_RECEIVER_JS = "return 41;";
const REMOTE_WORKSPACE_RSYNC_RECEIVER_JS = "return 42;";
${label} src/worker/workspace-rsync-receiver.ts
${marker}src/worker/workspace-rsync-receiver.ts
export const select = (accepted) => compileFunction(accepted ? REMOTE_WORKSPACE_ACCEPTED_RSYNC_RECEIVER_JS : REMOTE_WORKSPACE_RSYNC_RECEIVER_JS, ["require"]);`,
);
const driver = path.join(root, "capture.mjs");

View file

@ -601,11 +601,11 @@ describe("runtime postbuild static assets", () => {
);
});
it.each(["//#region", "//"])(
it.each(["//#region ", "// ", "//!"])(
"does not treat source-owned implementations as runtime wrappers (%s)",
async (label) => {
const f = await runtimeFixture({
"owner.runtime-Impl123.mjs": `import { value } from "./owner.runtime-Other456.mjs";\n${label} src/infra/owner.ts\nexport const current = value;\n`,
"owner.runtime-Impl123.mjs": `import { value } from "./owner.runtime-Other456.mjs";\n${label}src/infra/owner.ts\nexport const current = value;\n`,
"owner.runtime-Other456.mjs": "export const value = true;\n",
"owner.runtime.js": 'export * from "./owner.runtime-Stale.js";\n',
});

View file

@ -285,41 +285,44 @@ it("loads each built entry's own package exports despite controller source alias
}
});
it("counts built policy preparation only inside the diagnostic measurement window", async () => {
const root = temporary.make("team-stress-policy-counts-");
fs.writeFileSync(
path.join(root, "operator-model-policy-fixture.mjs"),
`export function prepareOperatorModelPolicy(value) { prepareRefs(); return value; }
export function restoreOperatorModelPolicySnapshot(value) { return value; }
function prepareRefs() {}`,
);
const entry = path.join(root, "entry.mjs");
fs.writeFileSync(
entry,
`import {prepareOperatorModelPolicy, restoreOperatorModelPolicySnapshot} from "./operator-model-policy-fixture.mjs";
it.each([" {", "{"])(
"counts built policy preparation inside its measurement window (%s)",
async (brace) => {
const root = temporary.make("team-stress-policy-counts-");
fs.writeFileSync(
path.join(root, "operator-model-policy-fixture.mjs"),
`export function prepareOperatorModelPolicy(value)${brace} prepareRefs(); return value; }
export function restoreOperatorModelPolicySnapshot(value)${brace} return value; }
function prepareRefs()${brace}}`,
);
const entry = path.join(root, "entry.mjs");
fs.writeFileSync(
entry,
`import {prepareOperatorModelPolicy, restoreOperatorModelPolicySnapshot} from "./operator-model-policy-fixture.mjs";
prepareOperatorModelPolicy("outside");
process.emit("message", {channel: "team-shape-stress", action: "start"});
if (prepareOperatorModelPolicy("inside") !== "inside") throw new Error("changed output");
restoreOperatorModelPolicySnapshot({});
process.emit("message", {channel: "team-shape-stress", action: "stop", output: ${JSON.stringify(root)}});`,
);
const loader = new URL("../../scripts/perf/team-shape-stress-loader.mjs", import.meta.url);
await executeFile(resolveTestNodeExecPath(), ["--import", loader.href, entry], {
cwd: root,
env: { ...process.env, OPENCLAW_BENCH_OPERATOR_MODEL_POLICY_COUNTS: "1" },
timeout: 15_000,
});
const result = JSON.parse(
fs.readFileSync(path.join(root, "operator-model-policy-counts.json"), "utf8"),
);
expect(result.counts).toEqual({
prepareOperatorModelPolicy: 1,
restoreOperatorModelPolicySnapshot: 1,
prepareRefs: 1,
prepareSessionRowRecipient: 0,
});
expect(result.installed).toHaveLength(3);
});
);
const loader = new URL("../../scripts/perf/team-shape-stress-loader.mjs", import.meta.url);
await executeFile(resolveTestNodeExecPath(), ["--import", loader.href, entry], {
cwd: root,
env: { ...process.env, OPENCLAW_BENCH_OPERATOR_MODEL_POLICY_COUNTS: "1" },
timeout: 15_000,
});
const result = JSON.parse(
fs.readFileSync(path.join(root, "operator-model-policy-counts.json"), "utf8"),
);
expect(result.counts).toEqual({
prepareOperatorModelPolicy: 1,
restoreOperatorModelPolicySnapshot: 1,
prepareRefs: 1,
prepareSessionRowRecipient: 0,
});
expect(result.installed).toHaveLength(3);
},
);
it.each([false, true])(
"counts main-isolate receipt restoration only when guarded assignment succeeds (candidate: %s)",
@ -776,7 +779,7 @@ it.each([false, true])("keeps partial worker evidence (%s)", (worktreeToolChurn)
}
});
it.each(["//#region", "//"])(
it.each(["//#region ", "// ", "//!"])(
"ranks allocating self bytes across stacks and preserves source-mapping uncertainty (%s)",
async (label) => {
const root = temporary.make("survivor-leaf-rank-");
@ -789,7 +792,7 @@ it.each(["//#region", "//"])(
const built = path.join(root, "dist/owner.js");
fs.writeFileSync(
built,
`${label} src/owner.ts\nfunction allocate() {}\n${label === "//#region" ? "//#endregion" : "// src/other.ts"}\n`,
`${label}src/owner.ts\nfunction allocate() {}\n${label.startsWith("//#region") ? "//#endregion" : `${label}src/other.ts`}\n`,
);
const mapped = path.join(root, "dist/mapped.js");
fs.writeFileSync(mapped, "function mapped() {}\n//# sourceMappingURL=mapped.js.map\n");

View file

@ -1,7 +1,7 @@
// Tsdown config tests protect package artifact build contracts.
import { execFile, type ExecFileOptionsWithStringEncoding } from "node:child_process";
import fs from "node:fs";
import { createRequire, isBuiltin } from "node:module";
import { createRequire, isBuiltin, SourceMap } from "node:module";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { build } from "tsdown";
@ -11,6 +11,7 @@ import {
collectRootPackageExcludedExtensionDirs,
DOCKER_SELECTED_PLUGIN_BUILD_IDS_ENV,
} from "../../scripts/lib/bundled-plugin-build-entries.mjs";
import { createCompactModuleLabelsPlugin } from "../../scripts/lib/compact-module-labels.mts";
import {
collectPackageDistImportErrors,
collectPackageDistImports,
@ -296,6 +297,68 @@ if (loaded.length) assert(loaded[0].startsWith(path.dirname(rootDir) + path.sep)
`;
describe("tsdown config", () => {
it("emits compact JavaScript alongside TypeScript declarations", async () => {
const selected = configs.find((config) => config.name === TSDOWN_PACKAGE_CONFIG_GROUP)!;
const root = fs.realpathSync(createTempDir("openclaw-tsdown-mixed-"));
materializeNativeCompiler(root);
fs.writeFileSync(path.join(root, "package.json"), '{"type":"module"}');
fs.writeFileSync(
path.join(root, "entry.ts"),
'export type Value = { value: string };\nexport const value: Value = { value: "ok" };',
);
const tsconfig = path.join(root, "tsconfig.json");
fs.writeFileSync(
tsconfig,
JSON.stringify({
compilerOptions: { module: "NodeNext", target: "ES2023", types: [], skipLibCheck: true },
files: ["entry.ts"],
}),
);
const { bundles } = await build({
...selected,
config: false,
cwd: root,
entry: { entry: "entry.ts" },
outDir: path.join(root, "dist"),
tsconfig,
dts: { tsconfig },
logLevel: "silent",
});
try {
expect(fs.readFileSync(path.join(root, "dist/entry.d.ts"), "utf8")).toMatch(
/type Value\s*=/u,
);
const runtime = await import(pathToFileURL(path.join(root, "dist/entry.js")).href);
expect(runtime.value).toEqual({ value: "ok" });
} finally {
for (const bundle of bundles) {
await bundle[Symbol.asyncDispose]();
}
}
});
it("preserves source positions and ignores module-like text inside strings and functions", () => {
const source = `// src/owner.ts
const script = \`
// src/string.ts
\`;
function run() {
// src/nested.ts
return script;
}
// src/next.ts
export { run };`;
const output = createCompactModuleLabelsPlugin().renderChunk(source, {
fileName: "fixture.js",
});
expect(output?.code).toBe(
source
.replace("// src/owner.ts", "//!src/owner.ts")
.replace("// src/next.ts", "//!src/next.ts"),
);
expect(output?.code.length).toBe(source.length);
});
it("reduces emitted source while preserving names, update origins, and lazy execution", async () => {
const selected = configs.find((config) => config.name === TSDOWN_UNIFIED_CONFIG_GROUP);
if (!selected) {
@ -339,6 +402,7 @@ export class PublicClass {
tsconfig: false,
dts: false,
logLevel: "silent",
sourcemap: "hidden",
});
try {
const chunks = bundles.flatMap((bundle) =>
@ -349,7 +413,16 @@ export class PublicClass {
expect(entry?.dynamicImports).toHaveLength(1);
const owner = chunks.find((chunk) => findBundledModuleRegion(chunk.code, "src/owner.ts"));
expect(owner).toBeDefined();
expect(owner?.code).toMatch(/\b(?:const|var) originValue = 7;/u);
expect(owner?.code).toMatch(/\b(?:const|var) originValue\s*=\s*7;/u);
const beforeFunction = owner!.code.slice(0, owner!.code.indexOf("function PublicFunction("));
const lines = beforeFunction.split("\n");
const sourceMap = new SourceMap(
JSON.parse(fs.readFileSync(path.join(outDir, `${owner!.fileName}.map`), "utf8")),
);
expect(sourceMap.findEntry(lines.length - 1, lines.at(-1)!.length)).toMatchObject({
originalSource: expect.stringContaining("src/owner.ts"),
originalLine: 1,
});
const relocated = path.join(root, "relocated");
fs.renameSync(outDir, relocated);
fs.rmSync(path.join(root, "src"), { recursive: true });
@ -377,7 +450,7 @@ assert.equal(process.env.OPENCLAW_FIXTURE_LAZY_LOADED, "yes");
// Bound the source retained by the loader, including the required owner labels.
expect(
chunks.reduce((total, chunk) => total + Buffer.byteLength(chunk.code), 0),
).toBeLessThan(600);
).toBeLessThan(400);
} finally {
for (const bundle of bundles) {
await bundle[Symbol.asyncDispose]();

View file

@ -155,32 +155,35 @@ it.each([
).toThrow("Cannot trace surface-abcdefgh.mjs export external to its release source");
});
it("bridges Rolldown module labels without confusing an unrelated same-named export", async () => {
const f = candidateFixture();
for (const file of fs.readdirSync(f.file("dist"))) {
if (file.startsWith("candidate-")) {
const source = fs.readFileSync(f.file("dist/" + file), "utf8");
f.write("dist/" + file, source.replaceAll("//#region ", "// "));
it.each(["// ", "//!"])(
"bridges %s module labels without confusing an unrelated same-named export",
async (label) => {
const f = candidateFixture();
for (const file of fs.readdirSync(f.file("dist"))) {
if (file.startsWith("candidate-")) {
const source = fs.readFileSync(f.file("dist/" + file), "utf8");
f.write("dist/" + file, source.replaceAll("//#region ", label));
}
}
}
const origin = chunk.exports[0]!.origin;
f.write(
"dist/optional-worker.mjs",
"// src/infra/unrelated.ts\nconst example = " +
String.fromCharCode(96) +
"\n// " +
origin.module +
"\n" +
String.fromCharCode(96) +
";\nfunction " +
origin.symbol +
'() { return "wrong owner"; }\nexport { ' +
origin.symbol +
" };\n",
);
f.emit();
await expectLegacy(f);
});
const origin = chunk.exports[0]!.origin;
f.write(
"dist/optional-worker.mjs",
"// src/infra/unrelated.ts\nconst example = " +
String.fromCharCode(96) +
"\n// " +
origin.module +
"\n" +
String.fromCharCode(96) +
";\nfunction " +
origin.symbol +
'() { return "wrong owner"; }\nexport { ' +
origin.symbol +
" };\n",
);
f.emit();
await expectLegacy(f);
},
);
it("records updater imports and export origins from Rolldown module labels", () => {
const f = fixtureFiles(createTempDir("update-compat-module-labels-"));

View file

@ -32,6 +32,51 @@ const testBashExecPath = process.platform === "darwin" ? "/bin/bash" : "bash";
const ASSERTIONS_PATH = "scripts/e2e/lib/upgrade-survivor/assertions.mjs";
it.each(["(params) {", "(e) {", "(e){"])(
"injects the upgrade deadline into a built plugin owner %s",
(signature) => {
const root = mkdtempSync(join(tmpdir(), "openclaw-upgrade-deadline-"));
try {
const dist = join(root, "dist");
mkdirSync(dist);
writeFileSync(
join(dist, "update-command-plugins-fixture.mjs"),
`export async function updatePluginsAfterCoreUpdate${signature} return "uninterrupted"; }`,
);
const error = "Update finalization timed out in plugins after 15000ms";
writeFileSync(
join(dist, "writer.mjs"),
`function mutateConfigFileWithRetry() { throw new Error(${JSON.stringify(error)}); }
export { mutateConfigFileWithRetry as write };`,
);
execFileSync(testNodeExecPath, [
"scripts/e2e/lib/upgrade-survivor/abandoned-update.mjs",
"deadline-hook",
root,
root,
]);
const driver = join(root, "driver.mjs");
writeFileSync(
driver,
`import assert from "node:assert/strict";
import { pathToFileURL } from "node:url";
globalThis.setTimeout = callback => queueMicrotask(callback);
await import(pathToFileURL(${JSON.stringify(join(root, "repair-deadline-hook.mjs"))}).href);
const { updatePluginsAfterCoreUpdate } = await import(pathToFileURL(${JSON.stringify(join(dist, "update-command-plugins-fixture.mjs"))}).href);
await assert.rejects(updatePluginsAfterCoreUpdate({ configWriteOptions: {} }), { message: ${JSON.stringify(error)} });`,
);
execFileSync(testNodeExecPath, [driver, "update", "repair"]);
expect(JSON.parse(readFileSync(join(root, "deadline-write-refused.json"), "utf8"))).toEqual({
phase: "plugins",
deadlineMs: 15_000,
refused: true,
});
} finally {
rmSync(root, { recursive: true, force: true });
}
},
);
function selectFrozenUpgradeOracle(
root: string,
version: string,

View file

@ -10,6 +10,7 @@ import {
collectSourceCheckoutPluginBuildEntries,
createBundledPluginBuildInventory,
} from "./scripts/lib/bundled-plugin-build-entries.mjs";
import { createCompactModuleLabelsPlugin } from "./scripts/lib/compact-module-labels.mts";
import { createFsSafeWindowsHelperAssetPlugin } from "./scripts/lib/fs-safe-windows-helper-asset.mts";
import { createGatewayRunChunkMetadataPlugin } from "./scripts/lib/gateway-run-chunk-metadata.mts";
import { createManagedHandoffBuildConfigs } from "./scripts/lib/managed-handoff-build-config.mts";
@ -289,13 +290,16 @@ function nodeBuildConfig(
minify:
typeof declarations === "object" && declarations.emitDtsOnly
? undefined
: { codegen: false, compress: false, mangle: { toplevel: false, keepNames: true } },
: { codegen: true, compress: false, mangle: { toplevel: false, keepNames: true } },
...config,
// Recovery diagnostics must parse on Node 22; runtime admission still guards live writers.
target: "node22",
dts: declarations,
hooks: createDeclarationBoundaryHooks(config.hooks),
plugins: [
!(typeof declarations === "object" && declarations.emitDtsOnly) && config.minify === undefined
? createCompactModuleLabelsPlugin()
: undefined,
typeof declarations === "object" && declarations.emitDtsOnly
? undefined
: createBashParserAssetsPlugin(),