Skip to content

Commit 82d1a03

Browse files
authored
refactor(agents): move implicit-main fallback into load-time roster injection (#112678)
* refactor(agents): require explicit roster defaults * feat(onboard): create named first roster agent * refactor(agents): remove runtime main fallbacks * style(agents): apply roster refactor formatting * refactor(agents): finish roster-only runtime sweep * fix(doctor): migrate legacy main session sqlite * fix(doctor): harden roster session migrations * fix(onboard): commit first agent atomically * fix(config): support empty-roster analysis * fix(agents): preserve legacy main state during creation * fix(setup): materialize baseline agent roster * fix(agents): harden legacy default transfer recovery * fix(agents): simplify roster-only legacy compatibility * fix(agents): preserve staged first-agent entries * fix(config): migrate persisted implicit-main rosters * fix(config): preserve staged empty rosters * fix(agents): finalize roster-only upgrade paths * fix(sessions): close legacy main migration outcomes * fix(config): migrate legacy roster markers at load * fix(sessions): preserve roster upgrade history * refactor(sessions): restore lean legacy main compatibility * fix(setup): prepare first-agent credentials before publish * fix(config): stabilize roster snapshot migration * refactor(sessions): shrink legacy main compatibility * fix(agents): restore roster compatibility fidelity * fix(sessions): preserve divergent legacy history * refactor(agents): narrow roster-only scope * fix(config): isolate roster migration * test(agents): align roster-only fixtures * fix(agents): keep main agent undeletable * fix(agents): harden roster migration invariants * fix(agents): close setup and audit scope gaps * fix(cron): scope session reaper throttles by agent * fix(agents): preserve scoped owner precedence * fix(config): preserve authored config ownership * fix(setup): keep default workspace and roster in sync * fix(setup): preserve default entry workspace on bare runs * fix(agents): adapt roster rebase to keyed entries * fix(agents): honor both roster representations * fix(agents): route roster reads through shared helpers * fix(config): preserve canonical roster writes * fix(cron): resolve dynamic default for session reaper * fix(agents): close dynamic default migration gaps * fix(agents): align scoped session ownership * fix(sessions): preserve legacy main directory casing * fix(agents): align cron and legacy auth ownership * fix(setup): provision the committed default workspace * fix(cron): align scoped ownership and reaping * fix(cron): treat blank agent ids as absent * fix(cron): retain configured session-store owners * fix(agents): repair roster-aware CI boundaries * fix(cron): preserve scoped ownership resolution * fix(agents): preserve rosterless maintenance paths * fix(agents): propagate roster ownership through runtime boundaries * fix(agents): preserve roster ownership across runtime paths * fix(agents): harden roster diagnostics and legacy routing * fix(agents): remove redundant diagnostic import * test(agents): type CLI policy fixture explicitly * fix(config): preserve canonical roster mutation identity * fix(doctor): read canonical agent rosters consistently * fix(config): resolve compound roster unsets safely * fix(config): finalize main-session reconciliation * fix(doctor): read canonical session state safely * fix(sessions): preserve current visibility alias * fix(config): track roster include provenance * test(config): type roster provenance cases * fix(config): refine roster include ownership * fix(agents): preserve staged roster invariants * test(config): align fixtures with explicit roster ownership * test(node-host): preserve optional plan typing * fix(config): preserve authored roster projections * test(config): keep raw roster fixtures explicit * test(config): normalize rosters at runtime fixtures * fix(config): protect authored roster ownership * fix(agents): require explicit session ownership * fix(agents): enforce scoped roster ownership * fix(sessions): merge fixed-store agent partitions * fix(agents): harden roster ownership boundaries * fix(config): reject ambiguous roster projections * fix(sessions): preserve persisted store ownership * fix(sessions): keep collision diagnostics additive * fix(security): scan malformed roster workspaces * test(config): align snapshot fixtures after rebase * test(agents): use explicit roster fixtures * fix(config): harden roster diagnostic boundaries * fix(sessions): isolate fixed-store agent databases * test(agents): type malformed default markers * refactor(sessions): extract store collision resolution * test(system-agent): split oversized setup coverage * style(system-agent): format split setup suite * fix(sessions): preserve promoted store ownership * fix(sessions): derive scoped owner before target * fix(sessions): preserve explicit sqlite ownership * fix(agents): restore roster compatibility across CI * fix(agents): enforce roster-owned runtime boundaries * fix(agents): satisfy default lookup lint * test(sessions): split known-owner coverage * fix(state): satisfy path identity lint * fix(agents): preserve malformed roster safety boundaries * fix(agents): restore roster compatibility at runtime boundaries * fix(config): satisfy roster boundary type checks * fix(agents): preserve roster ownership across runtime probes Setup inference probes now execute as the configured roster owner. Malformed agent-prefixed session rows are intentionally omitted by the fail-closed visibility contract rather than normalized by tests. * fix(agents): satisfy session list owner lint * fix(agents): preserve roster-owned runtime boundaries Restore shared logical rows for exact SQLite session locators while keeping their physical database owner separate. The ownership regression test now constructs an explicit sole-owner database directly instead of relying on first-touch capture, matching the intentional shared-store contract. * fix(sessions): preserve multiply owned exact stores * fix(sessions): restore runtime owner boundaries Keep incognito sentinels agent-owned, fold default-agent approvals into the global snapshot, and preserve the configless legacy-main CLI policy fallback. Also repair the existing CLI watchdog test lifecycle so the compact shard observes its timeout without an unawaited assertion or async timer stall; product behavior is unchanged by that test-only fix. * test(ci): align owner-scoped fixtures These assertions are unchanged. The fixtures now declare the intended non-default runner, expose the session-key constant imported by production status code, and select the main approvals bucket explicitly on Windows. * fix(agents): close final roster ownership gaps
1 parent 5f63f74 commit 82d1a03

430 files changed

Lines changed: 13831 additions & 2619 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ a5f59c9acbcaa3f82247bf806eb5ba08032373fb853719f0ec9457690f16fc70 module/media-m
8484
c5e3eb1a584f4b8126d9d6c177a840ec9103671e8d1242634ee67db9b5b9e573 module/media-understanding
8585
c0ffaed532578cf33493992e1ff806b2268b8e3774a92edbaede5cf5bda162a6 module/media-understanding-runtime
8686
bebd2931dc51d67c063ff19fa1c278f8dcfe00ab23cfbd480d47329ea8e5088e module/meeting-runtime
87-
d09ddb38c9d20a41fcf4cedaf6a2c28eb5333a9fba2287e34b458fa5caba751c module/memory-core-host-engine-foundation
88-
646773d8282a2ac6a89101685c406200935ddb3459a2830e820da132fa433b3c module/memory-host-core
87+
6b396a29c4efcd562accbd22dfa474f7815ce039cb8314a4771c2e37b5431ab4 module/memory-core-host-engine-foundation
88+
5d4d709d5ae573186459462fe5119bd253c13104554eb94aefbdab5f5c7ad46f module/memory-host-core
8989
87b7a3206346c0d4b294fb3a2395cbaabc3e73ff8b1b9ea925bc3aade3e52687 module/messaging-targets
9090
09f842a2787b87117d88ba6545aa0441ff8afdb85e2e66ae4d1838ebecb16148 module/model-session-runtime
9191
44655a08ce111a036c837b8d2e796cf0036941379ab317e5764c6c01e73b39f3 module/models-provider-runtime
@@ -105,7 +105,7 @@ aa2a56b4448c8ebdec9d06aac95d809995f533093d42fa32cd75e1d852967245 module/questio
105105
ac2b199e95c5c8b1e2a65e62bd41d1b6322e531bca294ef4979a297a12640bce module/reply-history
106106
f394fe4d5a7ed9e4d574063ae44e8d6af85c9a0e7d8b329f750ca16b0664325f module/reply-payload
107107
94356c388b1c5bc4ffb7fef6ece1e2fc5f6c9f0eaf0c280594b29230baf9af81 module/reply-runtime
108-
d78db621b8f4f0cc679cad2d5d21b6c95b5418c58611dd347cdf09049ed124f6 module/routing
108+
536341e301631a14ac67bd7e8d10d2ba770ff91c5f7a2c5dc8dd9dfd1c1c7ec4 module/routing
109109
ff6cca86f54f94f238205f5b122af36666314e0a380f3ec7f0ccb9ed9208df31 module/run-command
110110
53b0295cec105696a1664c5c7f5576a7b55d197eb95dcd9185486f010bd53750 module/runtime
111111
fcf103d18c1d64ffa9719f1d5a1ca349df3a6125d00c9d54a1b76c195ba8691b module/runtime-config-snapshot
@@ -117,7 +117,7 @@ b6b8edc50ecab8386c9acd8f374a207212b5a99c8f518538bbcf0c458dda3881 module/runtime
117117
17a6a199714ba8308e62928c0491bcf9fd214c923c3879aa99a047a30138253d module/secret-ref-runtime
118118
596a315d426121c9620b314e3a9a7f523840b46e007d94d0d5e83cdedf789d15 module/security-runtime
119119
31b785e74f1f8f56241b7756ef6a5d86199c5ce177cbb1c234a261866972f270 module/session-discussion
120-
82f1f06f875097ee894348e74346fde79325c8fee7106b51438aaec1f539cbff module/session-store-runtime
120+
32fb6d253abf22440bc76c7a68d1f35fc0ef369b0ad738aedba9c3054a76e48e module/session-store-runtime
121121
7cad408673562b0ff33f60071698bea2c39d02c64f4f235851c5c2ceb946910d module/setup
122122
fbcb853789db5a1ad5d81e8094ed7f5bcea657ac2281e7887a19c1d6f1194571 module/setup-runtime
123123
cd431f6ba8327b81438b7a63b1963120f200f5abd145fb6aa7c5c561339cb0b1 module/setup-tools

‎docs/channels/discord.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ See [Slash commands](/tools/slash-commands) for the command catalog and behavior
810810
agents: {
811811
entries: {
812812
codex: {
813+
default: true,
813814
runtime: {
814815
type: "acp",
815816
acp: {

‎docs/channels/group-messages.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Make display-name pings work even when WhatsApp strips the visual `@` from the t
4444
agents: {
4545
entries: {
4646
main: {
47+
default: true,
4748
groupChat: {
4849
mentionPatterns: ["@?openclaw", "\\+?15555550123"],
4950
},

‎docs/channels/groups.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ Each fact defaults to enabled when the channel produces it. Set the correspondin
346346
agents: {
347347
entries: {
348348
main: {
349+
default: true,
349350
groupChat: {
350351
mentionPatterns: ["@openclaw", "openclaw", "\\+15555550123"],
351352
historyLimit: 50,

‎extensions/msteams/doctor-contract-api.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,15 @@ function resolveLegacySanitizedSessionKey(
147147
return matches.length === 1 && match ? match : null;
148148
}
149149

150-
function listAgentIds(config: { agents?: { list?: Array<{ id?: unknown }> } }): string[] {
150+
function listAgentIds(config: OpenClawConfig): string[] {
151151
const ids = new Set<string>(["main"]);
152+
if (isRecord(config.agents?.entries)) {
153+
for (const agentId of Object.keys(config.agents.entries)) {
154+
if (agentId.trim()) {
155+
ids.add(agentId.trim());
156+
}
157+
}
158+
}
152159
for (const agent of config.agents?.list ?? []) {
153160
if (typeof agent.id === "string" && agent.id.trim()) {
154161
ids.add(agent.id.trim());
@@ -162,7 +169,6 @@ function listCandidateStorePaths(params: {
162169
env: NodeJS.ProcessEnv;
163170
}): string[] {
164171
const paths = new Set<string>();
165-
paths.add(resolveStorePath(params.config.session?.store, { env: params.env }));
166172
for (const agentId of listAgentIds(params.config)) {
167173
paths.add(resolveStorePath(params.config.session?.store, { agentId, env: params.env }));
168174
}

‎extensions/telegram/src/bot-handlers.message-context.runtime.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Telegram reply-chain cache and prompt-context projection.
22
import type { Message } from "grammy/types";
3+
import { resolveDefaultAgentId } from "openclaw/plugin-sdk/agent-runtime";
34
import type { OpenClawConfig, TelegramAccountConfig } from "openclaw/plugin-sdk/config-contracts";
45
import { DEFAULT_GROUP_HISTORY_LIMIT } from "openclaw/plugin-sdk/reply-history";
56
import { stripInlineDirectiveTagsForDelivery } from "openclaw/plugin-sdk/text-chunking";
@@ -55,7 +56,11 @@ export function createTelegramMessageContextRuntime({
5556
"cfg" | "accountId" | "opts" | "telegramCfg" | "telegramDeps"
5657
>) {
5758
const messageCache = createTelegramMessageCache({
58-
scope: resolveTelegramMessageCacheScope(telegramDeps.resolveStorePath(cfg.session?.store)),
59+
scope: resolveTelegramMessageCacheScope(
60+
telegramDeps.resolveStorePath(cfg.session?.store, {
61+
agentId: cfg.agents ? resolveDefaultAgentId(cfg) : "main",
62+
}),
63+
),
5964
});
6065
const resolvePromptSender = (
6166
node: TelegramCachedMessageNode,

‎extensions/telegram/src/message-topic-binding.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
import type { TelegramRuntime } from "./runtime.types.js";
1818

1919
const cfg = {
20+
agents: { entries: { main: { default: true } } },
2021
channels: { telegram: { botToken: "tok" } },
2122
session: { store: "/tmp/openclaw-telegram-topic-binding-test.json" },
2223
} as OpenClawConfig;
@@ -66,7 +67,9 @@ async function recordMessage(params: {
6667
accountId?: string;
6768
}) {
6869
const cache = createTelegramMessageCache({
69-
scope: resolveTelegramMessageCacheScope(resolveStorePath(cfg.session?.store)),
70+
scope: resolveTelegramMessageCacheScope(
71+
resolveStorePath(cfg.session?.store, { agentId: "main" }),
72+
),
7073
});
7174
await cache.record({
7275
accountId: params.accountId ?? "default",

‎extensions/telegram/src/message-topic-binding.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Telegram provider-owned authorization for message mutations in forum topics.
22
import { normalizeAccountId, normalizeOptionalAccountId } from "openclaw/plugin-sdk/account-core";
3+
import { resolveDefaultAgentId } from "openclaw/plugin-sdk/agent-runtime";
34
import type {
45
ChannelMessageActionContext,
56
ChannelThreadingToolContext,
@@ -112,7 +113,11 @@ export async function resolveTelegramMessageMutationChatId(params: {
112113
}
113114

114115
const cache = createTelegramMessageCache({
115-
scope: resolveTelegramMessageCacheScope(resolveStorePath(params.cfg.session?.store)),
116+
scope: resolveTelegramMessageCacheScope(
117+
resolveStorePath(params.cfg.session?.store, {
118+
agentId: params.cfg.agents ? resolveDefaultAgentId(params.cfg) : "main",
119+
}),
120+
),
116121
});
117122
const cached = await cache.get({
118123
accountId: selectedAccountId,

‎extensions/telegram/src/outbound-message-context.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Telegram plugin module implements outbound message context behavior.
22
import type { Message } from "grammy/types";
3+
import { resolveDefaultAgentId } from "openclaw/plugin-sdk/agent-runtime";
34
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
45
import { logVerbose } from "openclaw/plugin-sdk/runtime-env";
56
import { resolveStorePath } from "openclaw/plugin-sdk/session-store-runtime";
@@ -151,7 +152,11 @@ export async function recordOutboundMessageForPromptContext(params: {
151152
...(messageThreadId !== undefined ? { messageThreadId } : {}),
152153
});
153154
const cache = createTelegramMessageCache({
154-
scope: resolveTelegramMessageCacheScope(resolveStorePath(params.cfg.session?.store)),
155+
scope: resolveTelegramMessageCacheScope(
156+
resolveStorePath(params.cfg.session?.store, {
157+
agentId: params.cfg.agents ? resolveDefaultAgentId(params.cfg) : "main",
158+
}),
159+
),
155160
});
156161
await cache.record({
157162
accountId: params.account.accountId,

‎extensions/telegram/src/sent-message-cache.ts‎

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Telegram plugin module implements sent message cache behavior.
22
import { createHash } from "node:crypto";
33
import fs from "node:fs";
4+
import { resolveDefaultAgentId } from "openclaw/plugin-sdk/agent-runtime";
45
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
56
import type { PluginStateSyncKeyedStore } from "openclaw/plugin-sdk/plugin-state-runtime";
67
import { logVerbose } from "openclaw/plugin-sdk/runtime-env";
@@ -31,6 +32,8 @@ type SentMessageState = {
3132
bucketsByScope: Map<string, SentMessageBucket>;
3233
};
3334

35+
type SentMessageConfig = Pick<OpenClawConfig, "agents" | "session">;
36+
3437
function getSentMessageState(): SentMessageState {
3538
const globalStore = globalThis as Record<PropertyKey, unknown>;
3639
const existing = globalStore[TELEGRAM_SENT_MESSAGES_STATE_KEY] as SentMessageState | undefined;
@@ -48,15 +51,30 @@ function createSentMessageStore(): SentMessageStore {
4851
return new Map<string, Map<string, number>>();
4952
}
5053

51-
function resolveSentMessageStorePath(cfg?: Pick<OpenClawConfig, "session">): string {
52-
return `${resolveStorePath(cfg?.session?.store)}.telegram-sent-messages.json`;
54+
function resolveSentMessageAgentId(cfg?: SentMessageConfig, agentId?: string): string {
55+
return agentId?.trim() || (cfg?.agents ? resolveDefaultAgentId(cfg as OpenClawConfig) : "main");
56+
}
57+
58+
function resolveSentMessageStorePath(cfg?: SentMessageConfig, agentId?: string): string {
59+
return `${resolveStorePath(cfg?.session?.store, {
60+
agentId: resolveSentMessageAgentId(cfg, agentId),
61+
})}.telegram-sent-messages.json`;
5362
}
5463

55-
function resolveSentMessageScopeKey(cfg?: Pick<OpenClawConfig, "session">): string {
56-
const storePath = resolveStorePath(cfg?.session?.store);
64+
function sentMessageScopeKeyForStorePath(storePath: string): string {
5765
return createHash("sha256").update(storePath, "utf8").digest("hex").slice(0, 24);
5866
}
5967

68+
function resolveSentMessageScopeKey(cfg?: SentMessageConfig, agentId?: string): string {
69+
// This 24-hour cache follows the current agent owner. Do not revive a prior owner's
70+
// transient bucket when the configured default changes.
71+
return sentMessageScopeKeyForStorePath(
72+
resolveStorePath(cfg?.session?.store, {
73+
agentId: resolveSentMessageAgentId(cfg, agentId),
74+
}),
75+
);
76+
}
77+
6078
function sentMessageEntryKey(scopeKey: string, chatId: string, messageId: string): string {
6179
return createHash("sha256")
6280
.update(`${scopeKey}\0${chatId}\0${messageId}`, "utf8")
@@ -142,7 +160,7 @@ function readPersistedSentMessages(scopeKey: string): SentMessageStore {
142160
return store;
143161
}
144162

145-
function getSentMessageBucket(cfg?: Pick<OpenClawConfig, "session">): SentMessageBucket {
163+
function getSentMessageBucket(cfg?: SentMessageConfig): SentMessageBucket {
146164
const state = getSentMessageState();
147165
const scopeKey = resolveSentMessageScopeKey(cfg);
148166
const existing = state.bucketsByScope.get(scopeKey);
@@ -157,7 +175,7 @@ function getSentMessageBucket(cfg?: Pick<OpenClawConfig, "session">): SentMessag
157175
return bucket;
158176
}
159177

160-
function getSentMessages(cfg?: Pick<OpenClawConfig, "session">): SentMessageStore {
178+
function getSentMessages(cfg?: SentMessageConfig): SentMessageStore {
161179
return getSentMessageBucket(cfg).store;
162180
}
163181

@@ -177,7 +195,7 @@ function persistSentMessage(
177195
export function recordSentMessage(
178196
chatId: number | string,
179197
messageId: number,
180-
cfg?: Pick<OpenClawConfig, "session">,
198+
cfg?: SentMessageConfig,
181199
): void {
182200
const scopeKey = String(chatId);
183201
const idKey = String(messageId);
@@ -201,7 +219,7 @@ export function recordSentMessage(
201219
export function wasSentByBot(
202220
chatId: number | string,
203221
messageId: number,
204-
cfg?: Pick<OpenClawConfig, "session">,
222+
cfg?: SentMessageConfig,
205223
): boolean {
206224
const scopeKey = String(chatId);
207225
const idKey = String(messageId);
@@ -215,11 +233,15 @@ export function wasSentByBot(
215233
}
216234

217235
export function listTelegramLegacySentMessageCacheEntries(params: {
218-
cfg?: Pick<OpenClawConfig, "session">;
236+
cfg?: SentMessageConfig;
237+
agentId?: string;
219238
persistedPath?: string;
239+
targetStorePath?: string;
220240
}): Array<{ key: string; value: PersistedSentMessage; ttlMs?: number; timestamp?: number }> {
221-
const scopeKey = resolveSentMessageScopeKey(params.cfg);
222-
const filePath = params.persistedPath ?? resolveSentMessageStorePath(params.cfg);
241+
const scopeKey = params.targetStorePath
242+
? sentMessageScopeKeyForStorePath(params.targetStorePath)
243+
: resolveSentMessageScopeKey(params.cfg, params.agentId);
244+
const filePath = params.persistedPath ?? resolveSentMessageStorePath(params.cfg, params.agentId);
223245
const legacy = fs.existsSync(filePath)
224246
? readLegacySentMessages(filePath)
225247
: createSentMessageStore();

0 commit comments

Comments
 (0)