Skip to content

Commit 0d1573e

Browse files
authored
perf(gateway): preserve pristine startup facts (#106282)
1 parent 26312fd commit 0d1573e

4 files changed

Lines changed: 27 additions & 3 deletions

File tree

src/cli/program/preaction.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type EnsureConfigReadyOptions = {
1414
beforeStateMigrations?: () => Promise<boolean>;
1515
commandPath?: string[];
1616
requireConfig?: boolean;
17+
skipPristineCoreStateMigrations?: boolean;
18+
skipPristineStartupStateMigrations?: boolean;
1719
};
1820
const ensureConfigReadyMock = vi.fn<(_opts: EnsureConfigReadyOptions) => Promise<void>>(
1921
async () => {},
@@ -23,6 +25,8 @@ const routeLogsToStderrMock = vi.fn();
2325
const prepareGatewayRunBootstrapMock = vi.fn(async () => true);
2426
const recheckGatewayRunBootstrapMock = vi.fn(async () => true);
2527
const reloadTrustedGatewayRunEnvironmentMock = vi.fn(async () => true);
28+
const wasPreparedGatewayRunCoreStatePristineMock = vi.fn(() => true);
29+
const wasPreparedGatewayRunStatePristineMock = vi.fn(() => true);
2630

2731
const runtimeMock = {
2832
log: vi.fn(),
@@ -64,6 +68,8 @@ vi.mock("../gateway-cli/pre-bootstrap.js", () => ({
6468
prepareGatewayRunBootstrap: prepareGatewayRunBootstrapMock,
6569
recheckGatewayRunBootstrap: recheckGatewayRunBootstrapMock,
6670
reloadTrustedGatewayRunEnvironment: reloadTrustedGatewayRunEnvironmentMock,
71+
wasPreparedGatewayRunCoreStatePristine: wasPreparedGatewayRunCoreStatePristineMock,
72+
wasPreparedGatewayRunStatePristine: wasPreparedGatewayRunStatePristineMock,
6773
}));
6874

6975
let registerPreActionHooks: typeof import("./preaction.js").registerPreActionHooks;
@@ -319,6 +325,8 @@ describe("registerPreActionHooks", () => {
319325
expect.objectContaining({
320326
beforeStateMigrations: expect.any(Function),
321327
commandPath: ["gateway", "run"],
328+
skipPristineCoreStateMigrations: true,
329+
skipPristineStartupStateMigrations: true,
322330
}),
323331
);
324332
const beforeStateMigrations = ensureConfigReadyMock.mock.calls[0]?.[0]?.beforeStateMigrations;

src/cli/program/preaction.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,15 @@ export function registerPreActionHooks(program: Command, programVersion: string)
151151
return;
152152
}
153153
let beforeStateMigrations: ((snapshot?: ConfigFileSnapshot) => Promise<boolean>) | undefined;
154+
let skipPristineStartupStateMigrations = false;
155+
let skipPristineCoreStateMigrations = false;
154156
if (isGatewayRunAction(actionCommand)) {
155-
const { prepareGatewayRunBootstrap, recheckGatewayRunBootstrap } =
156-
await import("../gateway-cli/pre-bootstrap.js");
157+
const {
158+
prepareGatewayRunBootstrap,
159+
recheckGatewayRunBootstrap,
160+
wasPreparedGatewayRunCoreStatePristine,
161+
wasPreparedGatewayRunStatePristine,
162+
} = await import("../gateway-cli/pre-bootstrap.js");
157163
const { resolveGatewayRunOptions } = await import("../gateway-cli/run-options.js");
158164
const resolvedOptions = resolveGatewayRunOptions(actionCommand.opts(), actionCommand);
159165
const opts = {
@@ -164,6 +170,8 @@ export function registerPreActionHooks(program: Command, programVersion: string)
164170
if (!shouldBootstrap) {
165171
return;
166172
}
173+
skipPristineStartupStateMigrations = wasPreparedGatewayRunStatePristine();
174+
skipPristineCoreStateMigrations = wasPreparedGatewayRunCoreStatePristine();
167175
beforeStateMigrations = (snapshot) =>
168176
recheckGatewayRunBootstrap({
169177
opts,
@@ -177,6 +185,8 @@ export function registerPreActionHooks(program: Command, programVersion: string)
177185
startupPolicy,
178186
allowInvalid: shouldAllowInvalidConfigForAction(actionCommand, commandPath),
179187
...(beforeStateMigrations ? { beforeStateMigrations } : {}),
188+
...(skipPristineStartupStateMigrations ? { skipPristineStartupStateMigrations: true } : {}),
189+
...(skipPristineCoreStateMigrations ? { skipPristineCoreStateMigrations: true } : {}),
180190
skipConfigGuard: shouldBypassConfigGuardForCommandPath(commandPath),
181191
});
182192
if (beforeStateMigrations) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ describe("pristine startup state", () => {
144144
skipAllStateMigrations: false,
145145
skipCoreStateMigrations: false,
146146
});
147+
expect(
148+
planPristineStartupConfigMigrations({ agents: { $include: "agents.json" } }, env),
149+
).toEqual({
150+
skipAllStateMigrations: false,
151+
skipCoreStateMigrations: false,
152+
});
147153
});
148154

149155
it("rejects enabled plugin entries and includes", () => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export function planPristineStartupConfigMigrations(
105105
config: unknown,
106106
env: NodeJS.ProcessEnv = process.env,
107107
): PristineStartupMigrationPlan {
108-
if (!isRecord(config) || Object.hasOwn(config, "$include")) {
108+
if (!isRecord(config) || containsObjectKey(config, "$include")) {
109109
return { skipAllStateMigrations: false, skipCoreStateMigrations: false };
110110
}
111111
const skipCoreStateMigrations = configIsPristineCoreStateSafe(config);

0 commit comments

Comments
 (0)