refactor: dedupe runtime and helper flows

This commit is contained in:
Peter Steinberger
2026-03-02 12:53:19 +00:00
parent 5d3f066bbd
commit b02b94673f
17 changed files with 819 additions and 610 deletions

View File

@@ -106,13 +106,24 @@ function resolveSiblingAgentSessionsDir(
return path.join(rootDir, "agents", normalizeAgentId(agentId), "sessions");
}
function extractAgentIdFromAbsoluteSessionPath(candidateAbsPath: string): string | undefined {
function resolveAgentSessionsPathParts(
candidateAbsPath: string,
): { parts: string[]; sessionsIndex: number } | null {
const normalized = path.normalize(path.resolve(candidateAbsPath));
const parts = normalized.split(path.sep).filter(Boolean);
const sessionsIndex = parts.lastIndexOf("sessions");
if (sessionsIndex < 2 || parts[sessionsIndex - 2] !== "agents") {
return null;
}
return { parts, sessionsIndex };
}
function extractAgentIdFromAbsoluteSessionPath(candidateAbsPath: string): string | undefined {
const parsed = resolveAgentSessionsPathParts(candidateAbsPath);
if (!parsed) {
return undefined;
}
const { parts, sessionsIndex } = parsed;
const agentId = parts[sessionsIndex - 1];
return agentId || undefined;
}
@@ -121,12 +132,11 @@ function resolveStructuralSessionFallbackPath(
candidateAbsPath: string,
expectedAgentId: string,
): string | undefined {
const normalized = path.normalize(path.resolve(candidateAbsPath));
const parts = normalized.split(path.sep).filter(Boolean);
const sessionsIndex = parts.lastIndexOf("sessions");
if (sessionsIndex < 2 || parts[sessionsIndex - 2] !== "agents") {
const parsed = resolveAgentSessionsPathParts(candidateAbsPath);
if (!parsed) {
return undefined;
}
const { parts, sessionsIndex } = parsed;
const agentIdPart = parts[sessionsIndex - 1];
if (!agentIdPart) {
return undefined;
@@ -147,7 +157,7 @@ function resolveStructuralSessionFallbackPath(
if (!fileName || fileName === "." || fileName === "..") {
return undefined;
}
return normalized;
return path.normalize(path.resolve(candidateAbsPath));
}
function safeRealpathSync(filePath: string): string | undefined {