refactor: dedupe infra lowercase readers

This commit is contained in:
Peter Steinberger
2026-04-07 13:32:35 +01:00
parent ab4a6faf86
commit 62793e6027
4 changed files with 31 additions and 23 deletions

View File

@@ -1,4 +1,5 @@
import { createSubsystemLogger } from "../logging/subsystem.js";
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
let log: ReturnType<typeof createSubsystemLogger> | null = null;
const loggedEnv = new Set<string>();
@@ -55,7 +56,7 @@ export function isTruthyEnvValue(value?: string): boolean {
if (typeof value !== "string") {
return false;
}
switch (value.trim().toLowerCase()) {
switch (normalizeLowercaseStringOrEmpty(value)) {
case "1":
case "on":
case "true":

View File

@@ -1,8 +1,10 @@
import path from "node:path";
import { fileURLToPath, URL } from "node:url";
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
function isLocalFileUrlHost(hostname: string): boolean {
return hostname === "" || hostname.toLowerCase() === "localhost";
const normalized = normalizeLowercaseStringOrEmpty(hostname);
return normalized === "" || normalized === "localhost";
}
export function isWindowsNetworkPath(filePath: string): boolean {

View File

@@ -25,6 +25,10 @@ import {
normalizeMainKey,
parseAgentSessionKey,
} from "../routing/session-key.js";
import {
normalizeLowercaseStringOrEmpty,
normalizeOptionalLowercaseString,
} from "../shared/string-coerce.js";
import { expandHomePrefix } from "./home-dir.js";
import { isWithinDir } from "./path-safety.js";
import {
@@ -98,7 +102,7 @@ function isLegacyGroupKey(key: string): boolean {
if (!trimmed) {
return false;
}
const lower = trimmed.toLowerCase();
const lower = normalizeLowercaseStringOrEmpty(trimmed);
if (lower.startsWith("group:") || lower.startsWith("channel:")) {
return true;
}
@@ -151,8 +155,9 @@ function canonicalizeSessionKeyForAgent(params: {
if (!raw) {
return raw;
}
if (raw.toLowerCase() === "global" || raw.toLowerCase() === "unknown") {
return raw.toLowerCase();
const rawLower = normalizeLowercaseStringOrEmpty(raw);
if (rawLower === "global" || rawLower === "unknown") {
return rawLower;
}
// When shared-store guard is active, do not remap keys that belong to a
@@ -163,9 +168,8 @@ function canonicalizeSessionKeyForAgent(params: {
if (params.skipCrossAgentRemap) {
const parsed = parseAgentSessionKey(raw);
if (parsed && normalizeAgentId(parsed.agentId) !== agentId) {
return raw.toLowerCase();
return rawLower;
}
const rawLower = raw.toLowerCase();
if (
agentId !== DEFAULT_AGENT_ID &&
(rawLower === DEFAULT_MAIN_KEY || rawLower === params.mainKey)
@@ -180,7 +184,7 @@ function canonicalizeSessionKeyForAgent(params: {
sessionKey: raw,
});
if (canonicalMain !== raw) {
return canonicalMain.toLowerCase();
return normalizeLowercaseStringOrEmpty(canonicalMain);
}
// Handle cross-agent orphaned main-session keys: "agent:main:main" or
@@ -189,7 +193,6 @@ function canonicalizeSessionKeyForAgent(params: {
// (hooks, subagents, cron, per-sender) may be intentional cross-agent
// references and must not be touched (#29683).
const defaultPrefix = `agent:${DEFAULT_AGENT_ID}:`;
const rawLower = raw.toLowerCase();
if (
rawLower.startsWith(defaultPrefix) &&
agentId !== DEFAULT_AGENT_ID &&
@@ -204,14 +207,14 @@ function canonicalizeSessionKeyForAgent(params: {
agentId,
sessionKey: remapped,
});
return canonicalized.toLowerCase();
return normalizeLowercaseStringOrEmpty(canonicalized);
}
}
if (raw.toLowerCase().startsWith("agent:")) {
return raw.toLowerCase();
if (rawLower.startsWith("agent:")) {
return rawLower;
}
if (raw.toLowerCase().startsWith("subagent:")) {
if (rawLower.startsWith("subagent:")) {
const rest = raw.slice("subagent:".length);
return `agent:${agentId}:subagent:${rest}`.toLowerCase();
}
@@ -224,12 +227,12 @@ function canonicalizeSessionKeyForAgent(params: {
key: raw,
agentId,
});
if (typeof canonicalized === "string" && canonicalized.trim()) {
return canonicalized.trim().toLowerCase();
const normalizedCanonicalized = normalizeOptionalLowercaseString(canonicalized);
if (normalizedCanonicalized) {
return normalizedCanonicalized;
}
}
const lower = raw.toLowerCase();
if (lower.startsWith("group:") || lower.startsWith("channel:")) {
if (rawLower.startsWith("group:") || rawLower.startsWith("channel:")) {
return `agent:${agentId}:unknown:${raw}`.toLowerCase();
}
if (isSurfaceGroupKey(raw)) {
@@ -257,7 +260,7 @@ function pickLatestLegacyDirectEntry(
if (normalized.startsWith("agent:")) {
continue;
}
if (normalized.toLowerCase().startsWith("subagent:")) {
if (normalizeLowercaseStringOrEmpty(normalized).startsWith("subagent:")) {
continue;
}
if (isLegacyGroupKey(normalized) || isSurfaceGroupKey(normalized)) {

View File

@@ -1,5 +1,6 @@
import { readFileSync } from "node:fs";
import fs from "node:fs/promises";
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
let wslCached: boolean | null = null;
@@ -26,7 +27,7 @@ export function isWSLSync(): boolean {
return true;
}
try {
const release = readFileSync("/proc/version", "utf8").toLowerCase();
const release = normalizeLowercaseStringOrEmpty(readFileSync("/proc/version", "utf8"));
return release.includes("microsoft") || release.includes("wsl");
} catch {
return false;
@@ -41,7 +42,7 @@ export function isWSL2Sync(): boolean {
return false;
}
try {
const version = readFileSync("/proc/version", "utf8").toLowerCase();
const version = normalizeLowercaseStringOrEmpty(readFileSync("/proc/version", "utf8"));
return version.includes("wsl2") || version.includes("microsoft-standard");
} catch {
return false;
@@ -61,9 +62,10 @@ export async function isWSL(): Promise<boolean> {
return wslCached;
}
try {
const release = await fs.readFile("/proc/sys/kernel/osrelease", "utf8");
wslCached =
release.toLowerCase().includes("microsoft") || release.toLowerCase().includes("wsl");
const release = normalizeLowercaseStringOrEmpty(
await fs.readFile("/proc/sys/kernel/osrelease", "utf8"),
);
wslCached = release.includes("microsoft") || release.includes("wsl");
} catch {
wslCached = false;
}