Skip to content

Commit d97b15a

Browse files
committed
refactor(channels): unify trusted configured-state probes
1 parent 4eea0d3 commit d97b15a

8 files changed

Lines changed: 87 additions & 98 deletions

‎src/channels/plugins/configured-state.test.ts‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { createRequire } from "node:module";
33
import { describe, expect, it } from "vitest";
44
import type { OpenClawConfig } from "../../config/types.openclaw.js";
55
import {
6-
hasBundledChannelConfiguredState,
7-
listBundledChannelIdsWithConfiguredState,
8-
} from "./configured-state.js";
6+
hasBundledChannelPackageState,
7+
listBundledChannelIdsForPackageState,
8+
} from "./package-state-probes.js";
99

1010
const nodeRequire = createRequire(import.meta.url);
1111

1212
describe("bundled channel configured-state metadata", () => {
1313
it("lists the shipped metadata-first configured-state channels", () => {
14-
expect(listBundledChannelIdsWithConfiguredState()).toEqual([
14+
expect(listBundledChannelIdsForPackageState("configuredState")).toEqual([
1515
"buzz",
1616
"clickclack",
1717
"discord",
@@ -37,28 +37,32 @@ describe("bundled channel configured-state metadata", () => {
3737

3838
it("resolves Discord, Slack, Telegram, and IRC env probes without full plugin loads", () => {
3939
expect(
40-
hasBundledChannelConfiguredState({
40+
hasBundledChannelPackageState({
41+
metadataKey: "configuredState",
4142
channelId: "discord",
4243
cfg: {},
4344
env: { DISCORD_BOT_TOKEN: "token" },
4445
}),
4546
).toBe(true);
4647
expect(
47-
hasBundledChannelConfiguredState({
48+
hasBundledChannelPackageState({
49+
metadataKey: "configuredState",
4850
channelId: "slack",
4951
cfg: {},
5052
env: { SLACK_BOT_TOKEN: "xoxb-test", SLACK_APP_TOKEN: "xapp-test" },
5153
}),
5254
).toBe(true);
5355
expect(
54-
hasBundledChannelConfiguredState({
56+
hasBundledChannelPackageState({
57+
metadataKey: "configuredState",
5558
channelId: "telegram",
5659
cfg: {},
5760
env: { TELEGRAM_BOT_TOKEN: "token" },
5861
}),
5962
).toBe(true);
6063
expect(
61-
hasBundledChannelConfiguredState({
64+
hasBundledChannelPackageState({
65+
metadataKey: "configuredState",
6266
channelId: "irc",
6367
cfg: {},
6468
env: { IRC_HOST: "irc.example.com", IRC_NICK: "openclaw" },
@@ -79,7 +83,9 @@ describe("bundled channel configured-state metadata", () => {
7983
{ channelId: "nextcloud-talk", env: { NEXTCLOUD_TALK_BOT_SECRET: "secret" } },
8084
{ channelId: "zalo", env: { ZALO_WEBHOOK_SECRET: "secret" } },
8185
])("rejects incomplete $channelId environment credentials", ({ channelId, env }) => {
82-
expect(hasBundledChannelConfiguredState({ channelId, cfg: {}, env })).toBe(false);
86+
expect(
87+
hasBundledChannelPackageState({ metadataKey: "configuredState", channelId, cfg: {}, env }),
88+
).toBe(false);
8389
});
8490

8591
it.each([
@@ -109,12 +115,15 @@ describe("bundled channel configured-state metadata", () => {
109115
env: { SYNOLOGY_CHAT_TOKEN: "token", SYNOLOGY_CHAT_INCOMING_URL: "https://example.test" },
110116
},
111117
])("accepts complete $channelId environment credentials", ({ channelId, env }) => {
112-
expect(hasBundledChannelConfiguredState({ channelId, cfg: {}, env })).toBe(true);
118+
expect(
119+
hasBundledChannelPackageState({ metadataKey: "configuredState", channelId, cfg: {}, env }),
120+
).toBe(true);
113121
});
114122

115123
it("keeps explicit blank Teams credentials authoritative over ambient credentials", () => {
116124
expect(
117-
hasBundledChannelConfiguredState({
125+
hasBundledChannelPackageState({
126+
metadataKey: "configuredState",
118127
channelId: "msteams",
119128
cfg: { channels: { msteams: { appId: "", appPassword: "", tenantId: "" } } },
120129
env: {
@@ -168,15 +177,18 @@ describe("bundled channel configured-state metadata", () => {
168177
cfg: OpenClawConfig;
169178
env: NodeJS.ProcessEnv;
170179
}>)("accepts the owner-specific $name contract", ({ channelId, cfg, env }) => {
171-
expect(hasBundledChannelConfiguredState({ channelId, cfg, env })).toBe(true);
180+
expect(
181+
hasBundledChannelPackageState({ metadataKey: "configuredState", channelId, cfg, env }),
182+
).toBe(true);
172183
});
173184

174185
it("uses declarative env metadata without a TypeScript source require hook", () => {
175186
const previousTsHook = nodeRequire.extensions[".ts"];
176187
delete nodeRequire.extensions[".ts"];
177188
try {
178189
expect(
179-
hasBundledChannelConfiguredState({
190+
hasBundledChannelPackageState({
191+
metadataKey: "configuredState",
180192
channelId: "discord",
181193
cfg: {},
182194
env: { DISCORD_BOT_TOKEN: "token" },

‎src/channels/plugins/configured-state.ts‎

Lines changed: 0 additions & 38 deletions
This file was deleted.

‎src/channels/plugins/package-state-probes.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,27 @@ describe("channel package-state probes", () => {
133133
).toBe(false);
134134
});
135135

136+
it.each([
137+
{ name: "PATH", env: { PATH: "/usr/bin" }, configured: false },
138+
{ name: "mixed_case_token", env: { MIXED_CASE_TOKEN: "token" }, configured: true },
139+
])("applies the safe channel env-trigger contract to $name", ({ name, env, configured }) => {
140+
listChannelCatalogEntriesMock.mockReturnValue([
141+
{
142+
...makeBundledChannelCatalogEntry({ pluginId: "env-chat", channelId: "env-chat" }),
143+
channel: { id: "env-chat", configuredState: { env: { allOf: [name] } } },
144+
} satisfies PluginChannelCatalogEntry,
145+
]);
146+
147+
expect(
148+
hasBundledChannelPackageState({
149+
metadataKey: "configuredState",
150+
channelId: "env-chat",
151+
cfg: {},
152+
env,
153+
}),
154+
).toBe(configured);
155+
});
156+
136157
it("prefers built bundled package-state probes when the catalog root is source", () => {
137158
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-package-state-probe-"));
138159
tempDirs.push(root);

‎src/channels/plugins/package-state-probes.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
getCachedPluginModuleLoader,
2121
type PluginModuleLoaderCache,
2222
} from "../../plugins/plugin-module-loader-cache.js";
23+
import { isSafeChannelEnvVarTriggerName } from "../../secrets/channel-env-var-names.js";
2324
import { loadChannelPluginModule, resolveExistingPluginModulePath } from "./module-loader.js";
2425

2526
type ChannelPackageStateChecker = (params: {
@@ -81,7 +82,12 @@ function loadChannelPackageStateModule(params: { modulePath: string; rootDir: st
8182
}
8283

8384
function hasNonEmptyEnvValue(env: NodeJS.ProcessEnv | undefined, key: string): boolean {
84-
return typeof env?.[key] === "string" && env[key].trim().length > 0;
85+
if (!env || !isSafeChannelEnvVarTriggerName(key)) {
86+
return false;
87+
}
88+
const normalized = key.trim();
89+
const value = env[normalized] ?? env[normalized.toUpperCase()];
90+
return typeof value === "string" && value.trim().length > 0;
8591
}
8692

8793
function resolveSourceBundledPluginRoot(rootDir: string): {

‎src/config/channel-configured.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Determines whether a channel is configured from bootstrap and plugin state.
22
import { getBootstrapChannelPlugin } from "../channels/plugins/bootstrap-registry.js";
3-
import { hasBundledChannelConfiguredState } from "../channels/plugins/configured-state.js";
3+
import { hasBundledChannelPackageState } from "../channels/plugins/package-state-probes.js";
44
import {
55
hasMeaningfulChannelConfigShallow,
66
resolveChannelConfigRecord,
@@ -19,7 +19,7 @@ export function isChannelConfigured(
1919
return true;
2020
}
2121
// Bundled channels can expose configured state through env vars or persisted credential files.
22-
if (hasBundledChannelConfiguredState({ channelId, cfg, env })) {
22+
if (hasBundledChannelPackageState({ metadataKey: "configuredState", channelId, cfg, env })) {
2323
return true;
2424
}
2525
// Bootstrap plugins cover channels that are available before full plugin registry loading.

‎src/config/plugin-auto-enable.core.test.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ import {
2020
import type { OpenClawConfig } from "./types.openclaw.js";
2121
import { validateConfigObject } from "./validation.js";
2222

23-
vi.mock("../channels/plugins/configured-state.js", async (importOriginal) => {
24-
const actual = await importOriginal<typeof import("../channels/plugins/configured-state.js")>();
23+
vi.mock("../channels/plugins/package-state-probes.js", async (importOriginal) => {
24+
const actual =
25+
await importOriginal<typeof import("../channels/plugins/package-state-probes.js")>();
2526
return {
2627
...actual,
27-
hasBundledChannelConfiguredState: (params: {
28-
channelId: string;
29-
cfg: OpenClawConfig;
30-
env?: NodeJS.ProcessEnv;
31-
}) => {
28+
hasBundledChannelPackageState: (
29+
params: Parameters<typeof actual.hasBundledChannelPackageState>[0],
30+
) => {
31+
if (params.metadataKey !== "configuredState") {
32+
return actual.hasBundledChannelPackageState(params);
33+
}
3234
if (params.channelId === "cache-channel") {
3335
return Boolean(params.env?.CACHE_CHANNEL_TOKEN?.trim());
3436
}
@@ -40,7 +42,7 @@ vi.mock("../channels/plugins/configured-state.js", async (importOriginal) => {
4042
Boolean(params.env?.[key]?.trim()),
4143
);
4244
}
43-
return actual.hasBundledChannelConfiguredState(params);
45+
return actual.hasBundledChannelPackageState(params);
4446
},
4547
};
4648
});

‎src/config/plugin-auto-enable.shared.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import {
1616
type ChannelPresenceSignalSource,
1717
} from "../channels/config-presence.js";
1818
import {
19-
hasBundledChannelConfiguredState,
20-
listBundledChannelIdsWithConfiguredState,
21-
} from "../channels/plugins/configured-state.js";
19+
hasBundledChannelPackageState,
20+
listBundledChannelIdsForPackageState,
21+
} from "../channels/plugins/package-state-probes.js";
2222
import { findChatChannelMeta, normalizeChatChannelId } from "../channels/registry.js";
2323
import { isBlockedObjectKey } from "../infra/prototype-keys.js";
2424
import { normalizePluginsConfig } from "../plugins/config-state.js";
@@ -299,7 +299,9 @@ function collectConfiguredChannelIds(
299299
discovery?: PluginDiscoveryResult,
300300
ambientEnvTriggers: AmbientEnvTriggerPolicy = "allow",
301301
): string[] {
302-
const configuredStateChannelIds = new Set(listBundledChannelIdsWithConfiguredState(discovery));
302+
const configuredStateChannelIds = new Set(
303+
listBundledChannelIdsForPackageState("configuredState", discovery),
304+
);
303305
return listPotentialConfiguredChannelPresenceSignals(cfg, env, {
304306
includePersistedAuthState: false,
305307
discovery,
@@ -333,7 +335,8 @@ function isAutoEnableConfiguredChannelSignal(params: {
333335
if (
334336
params.source === "env" &&
335337
params.configuredStateChannelIds.has(params.channelId) &&
336-
!hasBundledChannelConfiguredState({
338+
!hasBundledChannelPackageState({
339+
metadataKey: "configuredState",
337340
channelId: params.channelId,
338341
cfg: params.cfg,
339342
env: params.env,

‎src/plugins/channel-presence-policy.ts‎

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
import { hasChannelPackageState } from "../channels/plugins/package-state-probes.js";
1313
import { resolveConfigWidePluginManifestRegistry } from "../config/io.plugin-metadata.js";
1414
import type { OpenClawConfig } from "../config/types.openclaw.js";
15-
import { isSafeChannelEnvVarTriggerName } from "../secrets/channel-env-var-names.js";
1615
import { resolveManifestActivationPluginIds } from "./activation-planner.js";
1716
import {
1817
createPluginActivationSource,
@@ -74,15 +73,6 @@ function normalizeChannelIds(channelIds: Iterable<string>): string[] {
7473
);
7574
}
7675

77-
function hasNonEmptyEnvValue(env: NodeJS.ProcessEnv, key: string): boolean {
78-
if (!isSafeChannelEnvVarTriggerName(key)) {
79-
return false;
80-
}
81-
const trimmed = key.trim();
82-
const value = env[trimmed] ?? env[trimmed.toUpperCase()];
83-
return typeof value === "string" && value.trim().length > 0;
84-
}
85-
8676
/** True when config contains meaningful enabled channel settings. */
8777
export function hasExplicitChannelConfig(params: {
8878
config: OpenClawConfig;
@@ -175,27 +165,20 @@ function listManifestEnvConfiguredChannelSignals(params: {
175165
continue;
176166
}
177167
contractChannelIds.add(normalizedChannelId);
178-
if (hasModuleContract) {
179-
if (
180-
!params.envSignalChannelIds.has(normalizedChannelId) ||
181-
!packageChannel ||
182-
!hasChannelPackageState({
183-
entry: {
184-
pluginId: record.id,
185-
origin: record.origin,
186-
rootDir: record.rootDir,
187-
channel: packageChannel,
188-
},
189-
metadataKey: "configuredState",
190-
cfg: params.config,
191-
env: params.env,
192-
})
193-
) {
194-
continue;
195-
}
196-
} else if (
197-
!allOf.every((envVar) => hasNonEmptyEnvValue(params.env, envVar)) ||
198-
(anyOf.length > 0 && !anyOf.some((envVar) => hasNonEmptyEnvValue(params.env, envVar)))
168+
if (
169+
(hasModuleContract && !params.envSignalChannelIds.has(normalizedChannelId)) ||
170+
!packageChannel ||
171+
!hasChannelPackageState({
172+
entry: {
173+
pluginId: record.id,
174+
origin: record.origin,
175+
rootDir: record.rootDir,
176+
channel: packageChannel,
177+
},
178+
metadataKey: "configuredState",
179+
cfg: params.config,
180+
env: params.env,
181+
})
199182
) {
200183
continue;
201184
}

0 commit comments

Comments
 (0)