From 4b90e57e595ce597b8512b9f9295d891c24d0617 Mon Sep 17 00:00:00 2001 From: Heewon Oh Date: Sun, 15 Nov 2026 07:27:54 +0900 Subject: [PATCH] =?UTF-8?q?fix(codex):=20index=C2=B7=EC=83=81=EC=84=B8=20?= =?UTF-8?q?=EB=A7=81=ED=81=AC=20=EA=B2=BD=EB=A1=9C=EB=A5=BC=20=ED=94=8C?= =?UTF-8?q?=EB=9E=AB=ED=8F=BC=20=EB=AC=B4=EA=B4=80=ED=95=9C=20POSIX=20?= =?UTF-8?q?=EC=83=81=EB=8C=80=EA=B2=BD=EB=A1=9C=EB=A1=9C=20(AGT-4878)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/memory/codex.ts | 21 ++++++++++++++++--- src/memory/codexIndexLink.test.ts | 34 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/memory/codexIndexLink.test.ts diff --git a/src/memory/codex.ts b/src/memory/codex.ts index 8f1a9a36..a09021ff 100644 --- a/src/memory/codex.ts +++ b/src/memory/codex.ts @@ -12,7 +12,7 @@ */ import { promises as fs } from 'fs'; -import { resolve, basename, join } from 'path'; +import { resolve, basename, join, posix, relative, sep, type PlatformPath } from 'path'; import { getDateLocale } from '../locale/index.js'; import { homedir } from 'os'; import { createHash } from 'crypto'; @@ -169,7 +169,8 @@ function generateSummary(session: CodexSession, detailPath: string): string { ? formatDuration(session.startedAt, session.endedAt) : 'ongoing'; - const relativeDetailPath = join('..', '.sessions', basename(detailPath)); + // Markdown links always use '/', whatever the platform separator is. + const relativeDetailPath = posix.join('..', '.sessions', basename(detailPath)); let md = `# ${session.title} > ${dateStr} | Duration: ~${duration} | [Detail Record](${relativeDetailPath}) @@ -307,6 +308,20 @@ export async function saveSession( return { summaryPath, detailPath }; } +/** + * The index.md link to a session summary: relative to the codex dir, with '/' + * separators. String-stripping `CODEX_DIR + '/'` left the absolute path in the + * link whenever the separator was '\' (Windows) or the dir had a trailing slash. + * `pathImpl` is injectable so the Windows shape can be tested on any platform. + */ +export function indexLinkPath( + codexDir: string, + summaryPath: string, + pathImpl: Pick = { relative, sep }, +): string { + return pathImpl.relative(codexDir, summaryPath).split(pathImpl.sep).join('/'); +} + /** * Update index.md * @@ -320,7 +335,7 @@ async function updateIndex(session: CodexSession, summaryPath: string): Promise< await withFileLock(indexPath + '.lock', async () => { let content = await fs.readFile(indexPath, 'utf-8'); - const relativePath = summaryPath.replace(CODEX_DIR + '/', ''); + const relativePath = indexLinkPath(CODEX_DIR, summaryPath); const date = new Date(session.startedAt); const dateStr = date.toLocaleDateString('en-US', { month: '2-digit', diff --git a/src/memory/codexIndexLink.test.ts b/src/memory/codexIndexLink.test.ts new file mode 100644 index 00000000..d221262a --- /dev/null +++ b/src/memory/codexIndexLink.test.ts @@ -0,0 +1,34 @@ +// ============================================ +// OpenSwarm - codex index link path tests +// ============================================ +// +// index.md links to session summaries used to be built with +// `summaryPath.replace(CODEX_DIR + '/', '')`, which left the absolute path in +// the link when the separator was '\' or CODEX_DIR had a trailing slash. + +import { posix, win32 } from 'path'; +import { describe, expect, it } from 'vitest'; +import { indexLinkPath } from './codex.js'; + +describe('indexLinkPath', () => { + it('gives a POSIX relative link for a Windows-shaped path', () => { + const dir = 'C:\\Users\\me\\.openswarm\\codex'; + const summary = 'C:\\Users\\me\\.openswarm\\codex\\2026-11\\15-0730-title-abc.md'; + expect(indexLinkPath(dir, summary, win32)).toBe('2026-11/15-0730-title-abc.md'); + }); + + it('gives a relative link when the codex dir has a trailing slash', () => { + const dir = '/home/me/.openswarm/codex/'; + const summary = '/home/me/.openswarm/codex/2026-11/15-0730-title-abc.md'; + expect(indexLinkPath(dir, summary, posix)).toBe('2026-11/15-0730-title-abc.md'); + }); + + it('handles a Windows codex dir with a trailing backslash', () => { + const dir = 'C:\\codex\\'; + expect(indexLinkPath(dir, 'C:\\codex\\sessions\\a.md', win32)).toBe('sessions/a.md'); + }); + + it('uses the native path module by default', () => { + expect(indexLinkPath('/a/codex', '/a/codex/2026-11/x.md')).toBe('2026-11/x.md'); + }); +});