Skip to content

Commit 610acfe

Browse files
committed
fix(gateway): refresh hook policy on hot reload
1 parent daafb2b commit 610acfe

8 files changed

Lines changed: 553 additions & 256 deletions

‎src/gateway/config-diff.ts‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,32 @@ export function diffConfigPaths(prev: unknown, next: unknown, prefix = ""): stri
3535
return [prefix || "<root>"];
3636
}
3737

38+
function projectGatewayReloadBoundaries(config: OpenClawConfig) {
39+
return {
40+
mcp: { apps: config.mcp?.apps },
41+
agents: {
42+
ownership: config.agents?.ownership,
43+
defaults: { sessionStore: config.agents?.defaults?.sessionStore },
44+
entries: config.agents?.entries,
45+
},
46+
session: {
47+
scope: config.session?.scope,
48+
store: config.session?.store,
49+
},
50+
};
51+
}
52+
3853
/** Preserve startup-only restart boundaries hidden by whole-object config changes. */
3954
export function diffGatewayReloadPaths(
4055
prevConfig: OpenClawConfig,
4156
nextConfig: OpenClawConfig,
4257
): string[] {
4358
const changedPaths = diffConfigPaths(prevConfig, nextConfig);
44-
if (!changedPaths.includes("mcp")) {
45-
return changedPaths;
46-
}
47-
// Adding or removing the whole `mcp` object collapses to the broad `mcp`
48-
// path. Preserve the Apps boundary so the listener still restarts.
49-
return [
50-
...changedPaths,
51-
...diffConfigPaths(
52-
{ mcp: { apps: prevConfig.mcp?.apps } },
53-
{ mcp: { apps: nextConfig.mcp?.apps } },
54-
),
55-
];
59+
const boundaryPaths = diffConfigPaths(
60+
projectGatewayReloadBoundaries(prevConfig),
61+
projectGatewayReloadBoundaries(nextConfig),
62+
);
63+
// Preserve only startup/reload ownership boundaries hidden by whole-object
64+
// collapse without changing ordinary diff multiplicity or ordering.
65+
return [...changedPaths, ...boundaryPaths.filter((path) => !changedPaths.includes(path))];
5666
}

‎src/gateway/config-reload-plan.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export type GatewayReloadPlan = {
2121
restartReasons: string[];
2222
hotReasons: string[];
2323
reloadHooks: boolean;
24+
/** Refresh the hook target-policy snapshot without invalidating transform modules. */
25+
refreshHooksPolicy?: boolean;
2426
restartGmailWatcher: boolean;
2527
restartCron: boolean;
2628
restartHeartbeat: boolean;
@@ -38,6 +40,7 @@ export function isNoopGatewayReloadPlan(plan: GatewayReloadPlan): boolean {
3840
!plan.restartGateway &&
3941
plan.hotReasons.length === 0 &&
4042
!plan.reloadHooks &&
43+
!plan.refreshHooksPolicy &&
4144
!plan.restartGmailWatcher &&
4245
!plan.restartCron &&
4346
!plan.restartHeartbeat &&
@@ -62,6 +65,7 @@ type ConfigReloadMetadata = {
6265

6366
type ReloadAction =
6467
| "reload-hooks"
68+
| "refresh-hooks-policy"
6569
| "restart-gmail-watcher"
6670
| "restart-cron"
6771
| "restart-heartbeat"
@@ -95,6 +99,11 @@ const BASE_RELOAD_RULES: ReloadRule[] = [
9599
kind: "hot",
96100
actions: ["restart-heartbeat"],
97101
},
102+
{
103+
prefix: "agents.defaults.sessionStore",
104+
kind: "hot",
105+
actions: ["refresh-hooks-policy"],
106+
},
98107
{ prefix: "agents.defaults", kind: "hot" },
99108
{
100109
prefix: "agents.defaults.models",
@@ -119,8 +128,9 @@ const BASE_RELOAD_RULES: ReloadRule[] = [
119128
{
120129
prefix: "agents.entries",
121130
kind: "hot",
122-
actions: ["restart-heartbeat"],
131+
actions: ["restart-heartbeat", "refresh-hooks-policy"],
123132
},
133+
{ prefix: "agents.ownership", kind: "hot", actions: ["refresh-hooks-policy"] },
124134
{ prefix: "agent.heartbeat", kind: "hot", actions: ["restart-heartbeat"] },
125135
{ prefix: "cron", kind: "hot", actions: ["restart-cron"] },
126136
// The dedicated Apps listener and origin are created once during Gateway
@@ -149,6 +159,8 @@ const BASE_RELOAD_RULES_TAIL: ReloadRule[] = [
149159
{ prefix: "talk", kind: "none" },
150160
{ prefix: "skills", kind: "none" },
151161
{ prefix: "secrets", kind: "none" },
162+
{ prefix: "session.scope", kind: "hot", actions: ["refresh-hooks-policy"] },
163+
{ prefix: "session.store", kind: "hot", actions: ["refresh-hooks-policy"] },
152164
{ prefix: "plugins", kind: "hot", actions: ["reload-plugins", "dispose-mcp-runtimes"] },
153165
{ prefix: "tui", kind: "none" },
154166
{ prefix: "ui", kind: "none" },
@@ -445,6 +457,9 @@ export function buildGatewayReloadPlan(
445457
case "reload-hooks":
446458
plan.reloadHooks = true;
447459
break;
460+
case "refresh-hooks-policy":
461+
plan.refreshHooksPolicy = true;
462+
break;
448463
case "restart-gmail-watcher":
449464
plan.restartGmailWatcher = true;
450465
break;

‎src/gateway/config-reload.test.ts‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,81 @@ describe("diffConfigPaths", () => {
159159
expect(changedPaths).toEqual(["mcp", "mcp.apps"]);
160160
expect(buildGatewayReloadPlan(changedPaths).restartReasons).toContain("mcp.apps");
161161
});
162+
163+
it.each([
164+
{
165+
name: "adds agents",
166+
prev: {},
167+
next: {
168+
agents: {
169+
ownership: "explicit",
170+
defaults: { sessionStore: { agentId: "ops" } },
171+
entries: { ops: {} },
172+
},
173+
},
174+
expectedPaths: ["agents.ownership", "agents.defaults.sessionStore", "agents.entries"],
175+
},
176+
{
177+
name: "removes agents",
178+
prev: {
179+
agents: {
180+
ownership: "explicit",
181+
defaults: { sessionStore: { agentId: "ops" } },
182+
entries: { ops: {} },
183+
},
184+
},
185+
next: {},
186+
expectedPaths: ["agents.ownership", "agents.defaults.sessionStore", "agents.entries"],
187+
},
188+
{
189+
name: "adds agent defaults",
190+
prev: { agents: { ownership: "explicit", entries: { ops: {} } } },
191+
next: {
192+
agents: {
193+
ownership: "explicit",
194+
defaults: { sessionStore: { agentId: "ops" } },
195+
entries: { ops: {} },
196+
},
197+
},
198+
expectedPaths: ["agents.defaults.sessionStore"],
199+
},
200+
{
201+
name: "removes agent defaults",
202+
prev: {
203+
agents: {
204+
ownership: "explicit",
205+
defaults: { sessionStore: { agentId: "ops" } },
206+
entries: { ops: {} },
207+
},
208+
},
209+
next: { agents: { ownership: "explicit", entries: { ops: {} } } },
210+
expectedPaths: ["agents.defaults.sessionStore"],
211+
},
212+
{
213+
name: "adds session config",
214+
prev: {},
215+
next: { session: { scope: "global", store: "/tmp/fixed.sqlite" } },
216+
expectedPaths: ["session.scope", "session.store"],
217+
},
218+
{
219+
name: "removes session config",
220+
prev: { session: { scope: "global", store: "/tmp/fixed.sqlite" } },
221+
next: {},
222+
expectedPaths: ["session.scope", "session.store"],
223+
},
224+
])("preserves hook policy dependencies when it $name", ({ prev, next, expectedPaths }) => {
225+
const changedPaths = diffGatewayReloadPaths(prev as OpenClawConfig, next as OpenClawConfig);
226+
227+
for (const expectedPath of expectedPaths) {
228+
expect(changedPaths).toContain(expectedPath);
229+
}
230+
expect(buildGatewayReloadPlan(changedPaths)).toMatchObject({
231+
restartGateway: false,
232+
refreshHooksPolicy: true,
233+
reloadHooks: false,
234+
restartGmailWatcher: false,
235+
});
236+
});
162237
});
163238

164239
describe("buildGatewayReloadPlan", () => {
@@ -343,6 +418,37 @@ describe("buildGatewayReloadPlan", () => {
343418
});
344419
});
345420

421+
it.each([
422+
{ path: "agents.entries", restartHeartbeat: true },
423+
{ path: "agents.ownership", restartHeartbeat: false },
424+
{ path: "agents.defaults.sessionStore", restartHeartbeat: false },
425+
{ path: "agents.defaults.sessionStore.agentId", restartHeartbeat: false },
426+
{ path: "session.scope", restartHeartbeat: false },
427+
{ path: "session.store", restartHeartbeat: false },
428+
])("refreshes only hook target policy for $path", ({ path, restartHeartbeat }) => {
429+
const plan = buildGatewayReloadPlan([path]);
430+
431+
expect(plan).toMatchObject({
432+
restartGateway: false,
433+
refreshHooksPolicy: true,
434+
reloadHooks: false,
435+
restartGmailWatcher: false,
436+
restartHeartbeat,
437+
});
438+
expect(isNoopGatewayReloadPlan(plan)).toBe(false);
439+
});
440+
441+
it.each([
442+
"agents",
443+
"agents.defaults",
444+
"agents.defaults.systemAgent.agentId",
445+
"agents.list",
446+
"session",
447+
"session.mainKey",
448+
])("does not broadly refresh hook target policy for %s", (path) => {
449+
expect(buildGatewayReloadPlan([path]).refreshHooksPolicy).not.toBe(true);
450+
});
451+
346452
it.each([
347453
"agents.defaults",
348454
"agents.defaults.compaction",

0 commit comments

Comments
 (0)