Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/memory/codex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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<PlatformPath, 'relative' | 'sep'> = { relative, sep },
): string {
return pathImpl.relative(codexDir, summaryPath).split(pathImpl.sep).join('/');
}

/**
* Update index.md
*
Expand All @@ -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',
Expand Down
34 changes: 34 additions & 0 deletions src/memory/codexIndexLink.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
Loading