Skip to content

Commit 091d44a

Browse files
committed
fix: keep lifecycle diagnostics backend-consistent
1 parent 33a54ed commit 091d44a

4 files changed

Lines changed: 45 additions & 18 deletions

File tree

‎src/codex/inject.ts‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,29 +163,36 @@ export function hasInjectedOpenaiBaseUrl(content: string): boolean {
163163

164164
export type CodexRoutingKind = "native" | "opencodex-local" | "custom-local" | "custom-remote" | "unknown";
165165

166+
function tomlStringPattern(key: string): RegExp {
167+
const escaped = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
168+
const keyToken = `(?:${escaped}|\"${escaped}\"|'${escaped}')`;
169+
return new RegExp(`^\\s*${keyToken}\\s*=\\s*[\"']([^\"']+)[\"']\\s*(?:#.*)?$`);
170+
}
171+
166172
function rootTomlString(content: string, key: string): string | null {
167173
const lines = content.split("\n");
168174
const firstTable = lines.findIndex(line => /^\s*\[/.test(line));
169175
const rootLines = lines.slice(0, firstTable === -1 ? lines.length : firstTable);
170-
const escaped = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
171-
const keyToken = `(?:${escaped}|\"${escaped}\"|'${escaped}')`;
172-
const pattern = new RegExp(`^\\s*${keyToken}\\s*=\\s*[\"']([^\"']+)[\"']\\s*(?:#.*)?$`);
176+
const pattern = tomlStringPattern(key);
173177
for (const line of rootLines) {
174178
const match = pattern.exec(line);
175179
if (match?.[1]) return match[1].trim();
176180
}
177181
return null;
178182
}
179183

180-
function providerTableString(content: string, provider: string, key: string): string | null {
181-
const lines = content.split("\n");
184+
function providerTableStart(lines: string[], provider: string): number {
182185
const escapedProvider = provider.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
183186
const providerToken = `(?:${escapedProvider}|\"${escapedProvider}\"|'${escapedProvider}')`;
184187
const header = new RegExp(`^\\s*\\[\\s*(?:model_providers|\"model_providers\"|'model_providers')\\s*\\.\\s*${providerToken}\\s*\\]\\s*(?:#.*)?$`);
185-
const start = lines.findIndex(line => header.test(line));
188+
return lines.findIndex(line => header.test(line));
189+
}
190+
191+
function providerTableString(content: string, provider: string, key: string): string | null {
192+
const lines = content.split("\n");
193+
const start = providerTableStart(lines, provider);
186194
if (start === -1) return null;
187-
const escaped = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
188-
const pattern = new RegExp(`^\\s*${escaped}\\s*=\\s*[\"']([^\"']+)[\"']\\s*(?:#.*)?$`);
195+
const pattern = tomlStringPattern(key);
189196
for (let index = start + 1; index < lines.length && !/^\s*\[/.test(lines[index]); index += 1) {
190197
const match = pattern.exec(lines[index]);
191198
if (match?.[1]) return match[1].trim();
@@ -226,14 +233,15 @@ export function classifyCodexRouting(content: string): CodexRoutingKind {
226233
}
227234
const rootProvider = rootTomlString(content, "model_provider");
228235
if (rootProvider) {
236+
const providerTableExists = providerTableStart(content.split("\n"), rootProvider) !== -1;
229237
const providerBaseUrl = providerTableString(content, rootProvider, "base_url");
230238
if (providerBaseUrl) {
231239
const endpoint = classifyRoutingEndpoint(providerBaseUrl);
232240
if (endpoint === "unknown") return "unknown";
233241
if (rootProvider === "opencodex") return "opencodex-local";
234242
return endpoint === "local" ? "custom-local" : "custom-remote";
235243
}
236-
if (rootProvider === "opencodex") return "unknown";
244+
if (rootProvider === "opencodex" || providerTableExists || rootProvider !== "openai") return "unknown";
237245
}
238246
return "native";
239247
}

‎src/service.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ export interface WindowsServiceDiagnosticInputs {
865865
schedulerEnabled: boolean;
866866
schedulerAssetsHealthy: boolean;
867867
nativeStatus: "started" | "stopped" | "nonexistent" | "unknown";
868-
nativeBackendRecorded: boolean;
868+
recordedBackend: ServiceBackend | null;
869869
staleBakedPaths: boolean;
870870
nativeRepairAssetsOnly: boolean;
871871
diagnostics: string;
@@ -874,9 +874,12 @@ export interface WindowsServiceDiagnosticInputs {
874874
export function deriveWindowsServiceDiagnostic(inputs: WindowsServiceDiagnosticInputs): ServiceDiagnostic {
875875
const nativeInstalled = inputs.nativeStatus !== "nonexistent";
876876
const conflict = inputs.schedulerInstalled && nativeInstalled;
877+
const backendStateMismatch = inputs.schedulerInstalled
878+
? inputs.recordedBackend !== "scheduler"
879+
: nativeInstalled && inputs.recordedBackend !== "native";
877880
const stale = inputs.staleBakedPaths
878881
|| (inputs.schedulerInstalled && !inputs.schedulerAssetsHealthy)
879-
|| (nativeInstalled && !inputs.nativeBackendRecorded)
882+
|| backendStateMismatch
880883
|| (inputs.nativeStatus === "nonexistent" && inputs.nativeRepairAssetsOnly);
881884
const backend = inputs.schedulerInstalled ? "scheduler" : nativeInstalled ? "native" : null;
882885
const enabled = inputs.schedulerInstalled ? inputs.schedulerEnabled : inputs.nativeStatus === "started";
@@ -937,13 +940,16 @@ export function diagnoseService(): ServiceDiagnostic {
937940
const schedulerAssets = [windowsServiceScriptPath(), windowsLauncherVbsPath(), windowsTaskXmlPath()].every(existsSync)
938941
&& windowsTaskRegistrationHealthy(schedulerXml);
939942
const nativeStatus = statusWinswRaw();
940-
const nativeBackendRecorded = readServiceInstallState()?.backend === "native";
943+
const installState = readServiceInstallState();
944+
const recordedBackend: ServiceBackend | null = !installState
945+
? null
946+
: installState.backend === "native" ? "native" : "scheduler";
941947
return deriveWindowsServiceDiagnostic({
942948
schedulerInstalled,
943949
schedulerEnabled,
944950
schedulerAssetsHealthy: schedulerAssets,
945951
nativeStatus,
946-
nativeBackendRecorded,
952+
recordedBackend,
947953
staleBakedPaths: bakedServicePathsDiagnostic() !== null,
948954
nativeRepairAssetsOnly: Boolean(winswStatusSummary()),
949955
diagnostics,

‎tests/autostart-health.test.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,16 @@ describe("Codex startup health", () => {
8585
expect(classifyCodexRouting('openai_base_url = "not-a-url"')).toBe("unknown");
8686
expect(classifyCodexRouting('openai_base_url = "https://gateway.example/v1"')).toBe("custom-remote");
8787
expect(classifyCodexRouting([
88-
'model_provider = "gateway"',
88+
'"model_provider" = "gateway"',
8989
'[model_providers."gateway"]',
90-
'base_url = "http://127.0.0.2:10100/v1"',
90+
'"base_url" = "http://127.0.0.2:10100/v1"',
9191
].join("\n"))).toBe("custom-local");
92+
expect(classifyCodexRouting([
93+
'model_provider = "gateway"',
94+
'[model_providers.gateway]',
95+
].join("\n"))).toBe("unknown");
96+
expect(classifyCodexRouting('model_provider = "missing-custom"')).toBe("unknown");
97+
expect(classifyCodexRouting('model_provider = "openai"')).toBe("native");
9298
expect(classifyCodexRouting([
9399
"[features]",
94100
'model_provider = "opencodex"',

‎tests/service.test.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,14 +496,14 @@ describe("service diagnostics", () => {
496496
schedulerEnabled: false,
497497
schedulerAssetsHealthy: true,
498498
nativeStatus: "nonexistent" as const,
499-
nativeBackendRecorded: false,
499+
recordedBackend: null,
500500
staleBakedPaths: false,
501501
nativeRepairAssetsOnly: false,
502502
diagnostics: "logs: test",
503503
};
504504

505505
test("fails closed for disabled, stale, conflicting, stopped, and ghost Windows services", () => {
506-
expect(deriveWindowsServiceDiagnostic({ ...base, schedulerInstalled: true, schedulerEnabled: true })).toMatchObject({ viable: true, backend: "scheduler" });
506+
expect(deriveWindowsServiceDiagnostic({ ...base, schedulerInstalled: true, schedulerEnabled: true, recordedBackend: "scheduler" })).toMatchObject({ viable: true, backend: "scheduler" });
507507
expect(deriveWindowsServiceDiagnostic({ ...base, schedulerInstalled: true })).toMatchObject({ viable: false, enabled: false });
508508
expect(deriveWindowsServiceDiagnostic({ ...base, schedulerInstalled: true, schedulerEnabled: true, staleBakedPaths: true })).toMatchObject({ viable: false, stale: true });
509509
expect(deriveWindowsServiceDiagnostic({ ...base, schedulerInstalled: true, schedulerEnabled: true, nativeStatus: "started" })).toMatchObject({ viable: false, conflict: true });
@@ -512,13 +512,20 @@ describe("service diagnostics", () => {
512512
});
513513

514514
test("a stopped healthy WinSW service remains startable from the tray", () => {
515-
const stoppedNative = deriveWindowsServiceDiagnostic({ ...base, nativeStatus: "stopped", nativeBackendRecorded: true });
515+
const stoppedNative = deriveWindowsServiceDiagnostic({ ...base, nativeStatus: "stopped", recordedBackend: "native" });
516516
expect(serviceStartableFromTray(stoppedNative)).toBe(true);
517517
expect(serviceStartableFromTray({ ...stoppedNative, stale: true })).toBe(false);
518518
expect(serviceStartableFromTray({ ...stoppedNative, conflict: true })).toBe(false);
519519
expect(serviceStartableFromTray(deriveWindowsServiceDiagnostic({ ...base, nativeStatus: "unknown" }))).toBe(false);
520520
const disabledScheduler = deriveWindowsServiceDiagnostic({ ...base, schedulerInstalled: true, schedulerEnabled: false });
521521
expect(serviceStartableFromTray(disabledScheduler)).toBe(false);
522+
const mismatchedScheduler = deriveWindowsServiceDiagnostic({
523+
...base,
524+
schedulerInstalled: true,
525+
schedulerEnabled: true,
526+
recordedBackend: "native",
527+
});
528+
expect(mismatchedScheduler).toMatchObject({ backend: "scheduler", stale: true, viable: false, startable: false });
522529
});
523530

524531
test("status summary exposes the service log path", () => {

0 commit comments

Comments
 (0)