Skip to content

Commit 8b994fc

Browse files
authored
fix(gateway): keep canonical hooks on pristine startup path (#117493)
1 parent a14ada1 commit 8b994fc

2 files changed

Lines changed: 93 additions & 1 deletion

File tree

‎src/commands/doctor/shared/pristine-startup-state.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,51 @@ describe("pristine startup state", () => {
148148
expect(canSkipPristineStartupStateMigrations(env)).toBe(true);
149149
});
150150

151+
it("accepts canonical internal hook configuration", () => {
152+
const env = createFixture({
153+
hooks: {
154+
internal: {
155+
enabled: true,
156+
entries: {
157+
"session-memory": {
158+
enabled: true,
159+
env: { OPENCLAW_HOOK_TEST: "enabled" },
160+
customOption: "value",
161+
},
162+
},
163+
},
164+
},
165+
});
166+
167+
expect(planPristineStartupStateMigrations(env)).toEqual({
168+
skipAllStateMigrations: true,
169+
skipCoreStateMigrations: true,
170+
});
171+
});
172+
173+
it("retains migrations for legacy, external, and malformed hook configuration", () => {
174+
const unsafeHooks = [
175+
{ gmail: { account: "[email protected]" } },
176+
{ internal: { installs: { "session-memory": { source: "bundled" } } } },
177+
{ internal: { handlers: [] } },
178+
{ internal: { load: { extraDirs: ["/tmp/hooks"] } } },
179+
{ internal: { enabled: "yes" } },
180+
{ internal: { entries: [] } },
181+
{ internal: { entries: { "session-memory": { enabled: "yes" } } } },
182+
{ internal: { entries: { "session-memory": { env: { INVALID: true } } } } },
183+
];
184+
185+
for (const hooks of unsafeHooks) {
186+
expect(
187+
planPristineStartupStateMigrations(createFixture({ hooks })),
188+
JSON.stringify(hooks),
189+
).toEqual({
190+
skipAllStateMigrations: false,
191+
skipCoreStateMigrations: false,
192+
});
193+
}
194+
});
195+
151196
it("retains migrations for bundled plugins with doctor state surfaces", () => {
152197
const env = addBundledPlugin(
153198
createFixture({ plugins: { entries: { example: { enabled: true } } } }),

‎src/commands/doctor/shared/pristine-startup-state.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const STATEFUL_CONFIG_KEYS = new Set([
3131
"cron",
3232
"discovery",
3333
"env",
34-
"hooks",
3534
"marketplaces",
3635
"mcp",
3736
"media",
@@ -48,6 +47,51 @@ const STATEFUL_CONFIG_KEYS = new Set([
4847
"web",
4948
]);
5049

50+
// Canonical internal entries have no legacy machine state to import. Keep every
51+
// older or external hook shape on Doctor's full migration path.
52+
function hasOnlyMigrationSafeInternalHooks(config: Record<string, unknown>): boolean {
53+
const hooks = config.hooks;
54+
if (hooks === undefined) {
55+
return true;
56+
}
57+
if (!isRecord(hooks) || Object.keys(hooks).some((key) => key !== "internal")) {
58+
return false;
59+
}
60+
61+
const internal = hooks.internal;
62+
if (internal === undefined) {
63+
return true;
64+
}
65+
if (
66+
!isRecord(internal) ||
67+
Object.keys(internal).some((key) => !["enabled", "entries"].includes(key)) ||
68+
(internal.enabled !== undefined && typeof internal.enabled !== "boolean")
69+
) {
70+
return false;
71+
}
72+
73+
if (internal.entries === undefined) {
74+
return true;
75+
}
76+
if (!isRecord(internal.entries)) {
77+
return false;
78+
}
79+
return Object.values(internal.entries).every((entry) => {
80+
if (!isRecord(entry)) {
81+
return false;
82+
}
83+
if (entry.enabled !== undefined && typeof entry.enabled !== "boolean") {
84+
return false;
85+
}
86+
if (entry.env === undefined) {
87+
return true;
88+
}
89+
return (
90+
isRecord(entry.env) && Object.values(entry.env).every((value) => typeof value === "string")
91+
);
92+
});
93+
}
94+
5195
function containsObjectKey(value: unknown, targetKey: string): boolean {
5296
if (Array.isArray(value)) {
5397
return value.some((entry) => containsObjectKey(entry, targetKey));
@@ -142,6 +186,9 @@ function configIsPristineCoreStateSafe(config: Record<string, unknown>): boolean
142186
if ([...STATEFUL_CONFIG_KEYS].some((key) => Object.hasOwn(config, key))) {
143187
return false;
144188
}
189+
if (!hasOnlyMigrationSafeInternalHooks(config)) {
190+
return false;
191+
}
145192
if (containsObjectKey(config.agents, "memorySearch")) {
146193
return false;
147194
}

0 commit comments

Comments
 (0)