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
17 changes: 17 additions & 0 deletions packages/cli/src/__tests__/pi-transcript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
makaPiToolPresentationStatus,
retireCancelledTransientMessages,
replaceTranscriptWithStoredMessages,
shortenCwd,
submitCompactToTranscript,
toggleAllThinkingExpansion,
toggleAllToolExpansion,
Expand Down Expand Up @@ -383,6 +384,22 @@ describe('Maka Pi TUI transcript', () => {
assert.doesNotMatch(line, /·\s*$/);
});

test('shortens cwd under home with either path separator', () => {
assert.equal(
shortenCwd('/Users/alice/workspace/project', '/Users/alice'),
'~/workspace/project',
);
assert.equal(
shortenCwd('C:\\Users\\alice\\Videos\\project', 'C:\\Users\\alice'),
'~\\Videos\\project',
);
assert.equal(shortenCwd('C:\\Users\\alice', 'C:\\Users\\alice'), '~');
assert.equal(
shortenCwd('C:\\Users\\alice2\\project', 'C:\\Users\\alice'),
'C:\\Users\\alice2\\project',
);
});

test('status line never drops mode, model, goal, or ctx at narrow widths (#3421)', () => {
const line = stripAnsi(
renderMakaPiStatusLine(
Expand Down
10 changes: 7 additions & 3 deletions packages/cli/src/pi-transcript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1922,10 +1922,14 @@ function firstLinePreview(text: string): string {
* `/Users/alice/workspace/project` → `~/workspace/project`.
* Falls back to the original path if it is not under the home directory.
*/
function shortenCwd(cwd: string, homeDir?: string): string {
export function shortenCwd(cwd: string, homeDir?: string): string {
const home = homeDir ?? homedir();
if (home && cwd.startsWith(home + '/')) return `~${cwd.slice(home.length)}`;
if (home && cwd === home) return '~';
const normalizedHome = home.replace(/\\/g, '/');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve platform path semantics when checking containment. Replacing every backslash before comparison is unsafe for both supported path families. On this exact commit, shortenCwd('C:\\Users\\Alice\\work', 'c:\\users\\alice') returns the full absolute path even though Windows treats it as inside the home directory. Conversely, shortenCwd('/home/alice\\sibling/project', '/home/alice') returns ~\\sibling/project, although on POSIX the backslash is an ordinary filename character and that path is a sibling, not a descendant. This therefore still misses valid Windows paths and introduces a false home-relative display on POSIX. Please make containment platform-aware (including Windows case handling) and add regressions for both cases.

const normalizedCwd = cwd.replace(/\\/g, '/');
if (home && normalizedCwd.startsWith(normalizedHome + '/')) {
return `~${cwd.slice(home.length)}`;
}
if (home && normalizedCwd === normalizedHome) return '~';
return cwd;
}

Expand Down