Skip to content

Commit a7df71c

Browse files
fix(channels): isolate unavailable account discovery
Co-authored-by: wangmiao0668000666 <[email protected]>
1 parent 021e145 commit a7df71c

33 files changed

Lines changed: 511 additions & 150 deletions

‎docs/gateway/secrets.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Plaintext credentials remain agent-readable when they sit in files the agent can
2525
- Reload validates each mapped owner independently, then publishes one atomic snapshot. Healthy owners refresh. An eligible failed owner keeps its last-known-good value and becomes stale only when its ref identities, provider definitions, and complete non-secret owner contract are unchanged; a changed or new failed owner becomes cold. A strict failure rejects the reload and preserves the active snapshot.
2626
- Policy violations (for example an OAuth-mode auth profile combined with SecretRef input) fail activation before the runtime swap.
2727
- Runtime requests read only the active in-memory snapshot. Model-provider SecretRef credentials pass through auth storage and stream options as process-local sentinels until egress. Outbound delivery paths (Discord reply/thread delivery, Telegram action sends) also read that snapshot and do not re-resolve refs per send.
28+
- Read-only channel capability discovery evaluates accounts independently. A configured-but-unavailable account does not hide healthy sibling accounts' message actions, while direct sends through the unavailable account still fail closed.
2829

2930
This keeps secret-provider outages off hot request paths.
3031

‎extensions/googlechat/src/accounts.ts‎

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import {
99
import { safeParseJsonWithSchema, safeParseWithSchema } from "openclaw/plugin-sdk/extension-shared";
1010
import { mergePairLoopGuardConfig } from "openclaw/plugin-sdk/pair-loop-guard-runtime";
1111
import { tryReadSecretFileSync } from "openclaw/plugin-sdk/secret-file-runtime";
12-
import { isSecretRef } from "openclaw/plugin-sdk/secret-input";
12+
import {
13+
coerceSecretRef,
14+
isSecretRef,
15+
resolveSecretInputString,
16+
type SecretInputStringResolutionMode,
17+
} from "openclaw/plugin-sdk/secret-input";
1318
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
1419
import { resolveUserPath } from "openclaw/plugin-sdk/text-utility-runtime";
1520
import { z } from "zod";
@@ -114,8 +119,10 @@ function parseServiceAccount(value: unknown): Record<string, unknown> | null {
114119
}
115120

116121
function resolveCredentialsFromConfig(params: {
122+
cfg: OpenClawConfig;
117123
accountId: string;
118124
account: GoogleChatAccountConfig;
125+
mode: SecretInputStringResolutionMode;
119126
}): {
120127
credentials?: Record<string, unknown>;
121128
credentialsFile?: string;
@@ -129,10 +136,17 @@ function resolveCredentialsFromConfig(params: {
129136
return { credentials: inline, source: "inline", status: "available" };
130137
}
131138

132-
if (isSecretRef(account.serviceAccount)) {
133-
throw new Error(
134-
`channels.googlechat.accounts.${accountId}.serviceAccount: unresolved SecretRef "${account.serviceAccount.source}:${account.serviceAccount.provider}:${account.serviceAccount.id}". Resolve this command against an active gateway runtime snapshot before reading it.`,
135-
);
139+
const serviceAccountRef = coerceSecretRef(account.serviceAccount, params.cfg.secrets?.defaults);
140+
if (serviceAccountRef) {
141+
const resolved = resolveSecretInputString({
142+
value: account.serviceAccount,
143+
defaults: params.cfg.secrets?.defaults,
144+
path: `channels.googlechat.accounts.${accountId}.serviceAccount`,
145+
mode: params.mode,
146+
});
147+
return resolved.status === "configured_unavailable"
148+
? { source: "none", status: "configured_unavailable" }
149+
: { source: "none", status: "missing" };
136150
}
137151

138152
const file = normalizeOptionalString(account.serviceAccountFile);
@@ -191,9 +205,10 @@ function resolveCredentialsFromConfig(params: {
191205
return { source: "none", status: "missing" };
192206
}
193207

194-
export function resolveGoogleChatAccount(params: {
208+
function resolveGoogleChatAccountWithMode(params: {
195209
cfg: OpenClawConfig;
196210
accountId?: string | null;
211+
mode: SecretInputStringResolutionMode;
197212
}): ResolvedGoogleChatAccount {
198213
const accountId = normalizeAccountId(
199214
params.accountId ?? params.cfg.channels?.["googlechat"]?.defaultAccount,
@@ -202,7 +217,12 @@ export function resolveGoogleChatAccount(params: {
202217
const merged = mergeGoogleChatAccountConfig(params.cfg, accountId);
203218
const accountEnabled = merged.enabled !== false;
204219
const enabled = baseEnabled && accountEnabled;
205-
const credentials = resolveCredentialsFromConfig({ accountId, account: merged });
220+
const credentials = resolveCredentialsFromConfig({
221+
cfg: params.cfg,
222+
accountId,
223+
account: merged,
224+
mode: params.mode,
225+
});
206226

207227
return {
208228
accountId,
@@ -217,6 +237,20 @@ export function resolveGoogleChatAccount(params: {
217237
};
218238
}
219239

240+
export function resolveGoogleChatAccount(params: {
241+
cfg: OpenClawConfig;
242+
accountId?: string | null;
243+
}): ResolvedGoogleChatAccount {
244+
return resolveGoogleChatAccountWithMode({ ...params, mode: "strict" });
245+
}
246+
247+
export function inspectGoogleChatAccount(params: {
248+
cfg: OpenClawConfig;
249+
accountId?: string | null;
250+
}): ResolvedGoogleChatAccount {
251+
return resolveGoogleChatAccountWithMode({ ...params, mode: "inspect" });
252+
}
253+
220254
export function listEnabledGoogleChatAccounts(cfg: OpenClawConfig): ResolvedGoogleChatAccount[] {
221255
return listGoogleChatAccountIds(cfg)
222256
.map((accountId) => resolveGoogleChatAccount({ cfg, accountId }))

‎extensions/googlechat/src/actions.ts‎

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,14 @@ import {
55
readStringParam,
66
} from "openclaw/plugin-sdk/channel-actions";
77
import type { ChannelMessageActionAdapter } from "openclaw/plugin-sdk/channel-contract";
8-
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
98
import { extractToolSend } from "openclaw/plugin-sdk/tool-send";
10-
import { listEnabledGoogleChatAccounts, resolveGoogleChatAccount } from "./accounts.js";
9+
import { resolveGoogleChatAccount } from "./accounts.js";
1110
import { sendGoogleChatMessage } from "./api.js";
11+
import { describeGoogleChatMessageTool } from "./message-tool-api.js";
1212
import { resolveGoogleChatOutboundSpace } from "./targets.js";
1313

1414
const providerId = "googlechat";
1515

16-
function listEnabledAccounts(cfg: OpenClawConfig) {
17-
return listEnabledGoogleChatAccounts(cfg).filter(
18-
(account) =>
19-
account.enabled &&
20-
account.credentialSource !== "none" &&
21-
account.tokenStatus !== "configured_unavailable",
22-
);
23-
}
24-
2516
const OUTBOUND_MEDIA_KEYS = ["media", "mediaUrl", "path", "filePath", "fileUrl"] as const;
2617
const STRUCTURED_ATTACHMENT_MEDIA_KEYS = [...OUTBOUND_MEDIA_KEYS, "url"] as const;
2718

@@ -47,20 +38,7 @@ function hasGoogleChatOutboundAttachment(params: Record<string, unknown>): boole
4738
}
4839

4940
export const googlechatMessageActions: ChannelMessageActionAdapter = {
50-
describeMessageTool: ({ cfg, accountId }) => {
51-
const accounts = accountId
52-
? [resolveGoogleChatAccount({ cfg, accountId })].filter(
53-
(account) =>
54-
account.enabled &&
55-
account.credentialSource !== "none" &&
56-
account.tokenStatus !== "configured_unavailable",
57-
)
58-
: listEnabledAccounts(cfg);
59-
if (accounts.length === 0) {
60-
return null;
61-
}
62-
return { actions: ["send"] };
63-
},
41+
describeMessageTool: describeGoogleChatMessageTool,
6442
supportsAction: ({ action }) => action === "send",
6543
extractToolSend: ({ args }) => {
6644
return extractToolSend(args, "sendMessage");

‎extensions/googlechat/src/channel-base.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { ChannelPlugin } from "openclaw/plugin-sdk/channel-core";
99
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/string-coerce-runtime";
1010
import {
1111
type GoogleChatConfigAccessorAccount,
12+
inspectGoogleChatAccount,
1213
listGoogleChatAccountIds,
1314
resolveDefaultGoogleChatAccountId,
1415
resolveGoogleChatConfigAccessorAccount,
@@ -71,6 +72,12 @@ const googleChatConfigAdapter = createScopedChannelConfigAdapter<
7172
resolveDefaultTo: (account) => account.config.defaultTo,
7273
});
7374

75+
function isGoogleChatAccountConfigured(account: ResolvedGoogleChatAccount): boolean {
76+
return account.tokenStatus
77+
? account.tokenStatus !== "missing"
78+
: account.credentialSource !== "none";
79+
}
80+
7481
type GoogleChatPluginBase = Pick<
7582
ChannelPlugin<ResolvedGoogleChatAccount>,
7683
| "id"
@@ -112,11 +119,12 @@ export function createGoogleChatPluginBase(
112119
...(params.configSchema ? { configSchema: params.configSchema } : {}),
113120
config: {
114121
...googleChatConfigAdapter,
115-
isConfigured: (account) => account.credentialSource !== "none",
122+
inspectAccount: adaptScopedAccountAccessor(inspectGoogleChatAccount),
123+
isConfigured: isGoogleChatAccountConfigured,
116124
describeAccount: (account) =>
117125
describeAccountSnapshot({
118126
account,
119-
configured: account.credentialSource !== "none",
127+
configured: isGoogleChatAccountConfigured(account),
120128
extra: {
121129
credentialSource: account.credentialSource,
122130
tokenStatus: account.tokenStatus,

‎extensions/googlechat/src/channel.ts‎

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ import {
2727
GoogleChatConfigSchema,
2828
isGoogleChatSpaceTarget,
2929
isGoogleChatUserTarget,
30-
listGoogleChatAccountIds,
3130
normalizeGoogleChatTarget,
32-
resolveGoogleChatAccount,
3331
resolveGoogleChatOutboundSessionRoute,
3432
type ChannelMessageActionAdapter,
3533
type ChannelStatusIssue,
@@ -41,6 +39,7 @@ import {
4139
} from "./doctor-contract.js";
4240
import { collectGoogleChatMutableAllowlistWarnings } from "./doctor.js";
4341
import { startGoogleChatGatewayAccount } from "./gateway.js";
42+
import { describeGoogleChatMessageTool } from "./message-tool-api.js";
4443
import { collectRuntimeConfigAssignments, secretTargetRegistryEntries } from "./secret-contract.js";
4544

4645
const loadGoogleChatChannelRuntime = createLazyRuntimeNamedExport(
@@ -49,27 +48,7 @@ const loadGoogleChatChannelRuntime = createLazyRuntimeNamedExport(
4948
);
5049

5150
const googlechatActions: ChannelMessageActionAdapter = {
52-
describeMessageTool: ({ cfg, accountId }) => {
53-
const accounts = accountId
54-
? [resolveGoogleChatAccount({ cfg, accountId })].filter(
55-
(account) =>
56-
account.enabled &&
57-
account.credentialSource !== "none" &&
58-
account.tokenStatus !== "configured_unavailable",
59-
)
60-
: listGoogleChatAccountIds(cfg)
61-
.map((id) => resolveGoogleChatAccount({ cfg, accountId: id }))
62-
.filter(
63-
(account) =>
64-
account.enabled &&
65-
account.credentialSource !== "none" &&
66-
account.tokenStatus !== "configured_unavailable",
67-
);
68-
if (accounts.length === 0) {
69-
return null;
70-
}
71-
return { actions: ["send"] };
72-
},
51+
describeMessageTool: describeGoogleChatMessageTool,
7352
supportsAction: ({ action }) => action === "send",
7453
extractToolSend: ({ args }) => extractToolSend(args, "sendMessage"),
7554
handleAction: async (ctx) => {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Google Chat tests cover account-isolated message-tool discovery.
2+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
3+
import { afterEach, describe, expect, it, vi } from "vitest";
4+
import { inspectGoogleChatAccount, resolveGoogleChatAccount } from "./accounts.js";
5+
import { describeGoogleChatMessageTool } from "./message-tool-api.js";
6+
7+
const unresolvedRef = {
8+
source: "env",
9+
provider: "default",
10+
id: "OPENCLAW_TEST_MISSING_GOOGLE_CHAT_SERVICE_ACCOUNT",
11+
} as const;
12+
13+
function buildTwoAccountConfig(): OpenClawConfig {
14+
return {
15+
channels: {
16+
googlechat: {
17+
accounts: {
18+
broken: { serviceAccount: unresolvedRef },
19+
healthy: {
20+
serviceAccount:
21+
'{"client_email":"[email protected]","private_key":"proof-key"}',
22+
},
23+
},
24+
},
25+
},
26+
} as OpenClawConfig;
27+
}
28+
29+
describe("Google Chat message-tool SecretRef inspection", () => {
30+
afterEach(() => vi.unstubAllEnvs());
31+
32+
it("keeps healthy account actions when one account credential is unavailable", () => {
33+
const cfg = buildTwoAccountConfig();
34+
expect(describeGoogleChatMessageTool({ cfg })).toEqual({ actions: ["send"] });
35+
expect(describeGoogleChatMessageTool({ cfg, accountId: "broken" })).toBeNull();
36+
});
37+
38+
it("keeps direct account resolution strict", () => {
39+
expect(() =>
40+
resolveGoogleChatAccount({ cfg: buildTwoAccountConfig(), accountId: "broken" }),
41+
).toThrow(/unresolved SecretRef/);
42+
});
43+
44+
it("does not fall through an unavailable configured ref to the environment", () => {
45+
vi.stubEnv(
46+
"GOOGLE_CHAT_SERVICE_ACCOUNT",
47+
'{"client_email":"[email protected]","private_key":"fallback"}',
48+
);
49+
const account = inspectGoogleChatAccount({
50+
cfg: { channels: { googlechat: { serviceAccount: unresolvedRef } } } as OpenClawConfig,
51+
});
52+
expect(account).toMatchObject({
53+
credentialSource: "none",
54+
tokenStatus: "configured_unavailable",
55+
});
56+
expect(account.credentials).toBeUndefined();
57+
});
58+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Google Chat message-tool discovery stays read-only and account-isolated.
2+
import type { ChannelMessageActionAdapter } from "openclaw/plugin-sdk/channel-contract";
3+
import { inspectGoogleChatAccount, listGoogleChatAccountIds } from "./accounts.js";
4+
5+
export function describeGoogleChatMessageTool({
6+
cfg,
7+
accountId,
8+
}: Parameters<NonNullable<ChannelMessageActionAdapter["describeMessageTool"]>>[0]) {
9+
const accounts = accountId
10+
? [inspectGoogleChatAccount({ cfg, accountId })]
11+
: listGoogleChatAccountIds(cfg).map((listedAccountId) =>
12+
inspectGoogleChatAccount({ cfg, accountId: listedAccountId }),
13+
);
14+
const hasAvailableAccount = accounts.some(
15+
(account) =>
16+
account.enabled && account.credentialSource !== "none" && account.tokenStatus === "available",
17+
);
18+
return hasAvailableAccount ? { actions: ["send" as const] } : null;
19+
}

‎extensions/mattermost/src/channel-config-shared.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ export const mattermostConfigAdapter = createScopedChannelConfigAdapter<Resolved
6565
});
6666

6767
export function isMattermostConfigured(account: ResolvedMattermostAccount): boolean {
68-
return Boolean(account.botToken && account.baseUrl);
68+
const tokenConfigured = account.botTokenStatus
69+
? account.botTokenStatus !== "missing"
70+
: Boolean(account.botToken);
71+
return tokenConfigured && Boolean(account.baseUrl);
6972
}
7073

7174
export function describeMattermostAccount(account: ResolvedMattermostAccount) {
@@ -74,6 +77,7 @@ export function describeMattermostAccount(account: ResolvedMattermostAccount) {
7477
configured: isMattermostConfigured(account),
7578
extra: {
7679
botTokenSource: account.botTokenSource,
80+
botTokenStatus: account.botTokenStatus,
7781
baseUrl: account.baseUrl,
7882
},
7983
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Mattermost tests cover account-isolated message-tool discovery.
2+
import { describe, expect, it } from "vitest";
3+
import type { OpenClawConfig } from "../runtime-api.js";
4+
import { mattermostPlugin } from "./channel.js";
5+
6+
describe("Mattermost message-tool SecretRef inspection", () => {
7+
const cfg = {
8+
channels: {
9+
mattermost: {
10+
accounts: {
11+
broken: {
12+
botToken: {
13+
source: "env",
14+
provider: "default",
15+
id: "OPENCLAW_TEST_MISSING_MATTERMOST_TOKEN",
16+
},
17+
baseUrl: "https://mm.example.com",
18+
},
19+
healthy: { botToken: "healthy-token", baseUrl: "https://mm.example.com" },
20+
},
21+
},
22+
},
23+
} as OpenClawConfig;
24+
25+
it("keeps healthy account actions discoverable", () => {
26+
expect(mattermostPlugin.actions?.describeMessageTool({ cfg })?.actions).toEqual(
27+
expect.arrayContaining(["send", "react"]),
28+
);
29+
});
30+
31+
it("does not advertise actions for the unavailable selected account", () => {
32+
expect(
33+
mattermostPlugin.actions?.describeMessageTool({ cfg, accountId: "broken" })?.actions,
34+
).toEqual([]);
35+
});
36+
});

0 commit comments

Comments
 (0)