toUserFriendlyPath() corrupts a sibling path that merely starts with the home directory name
Version
main @ c2ad42e3; multimodal/tarko/agent-cli/package.json is version 0.3.0.
Issue Description
toUserFriendlyPath() shortens a path by matching the home directory with a bare startsWith() and rewriting it with
replace(), with no check that the match ends at a path separator:
multimodal/tarko/agent-cli/src/utils/misc.ts:35-43
export function toUserFriendlyPath(absolutePath: string): string {
const homedir = os.homedir();
if (absolutePath.startsWith(homedir)) {
return absolutePath.replace(homedir, '~');
}
return absolutePath;
}So any path whose first component merely extends the home directory's name is shortened as if it were inside the home
directory, and the extra characters end up glued to the ~: with home C:\Users\28697, the path
C:\Users\286972\project\a.ts — a sibling of the home directory, not a descendant — is returned as ~2\project\a.ts.
That is not a "user-friendly path with ~ for home directory", which is what the function's own doc comment one line
above the if promises; it is a different, non-existent path with characters missing. And the inputs are not exotic: the --workspace flag (README.md:139) and the workspace config key
(README.md:122) accept an arbitrary directory, so the CLI never controls whether the
value collides with the home directory's name. A user with home /home/dev who points the workspace at
/home/dev-2/data — or /Users/john versus /Users/johnny/projects — gets a mangled path on every line listed below.
Measured
Both the base function and the fixed one were imported side by side and run in one Node process (--experimental-strip-types,
real os.homedir(), path.sep used to build the inputs), so every value below is actual output, not a hand-worked
example. This machine's home directory is C:\Users\28697.
| input | main @ c2ad42e3 |
with the boundary check |
|---|---|---|
C:\Users\28697 |
~ |
~ |
C:\Users\28697\project\a.ts |
~\project\a.ts |
~\project\a.ts |
C:\Users\28697\ |
~\ |
~\ |
D:\other\\\x.ts |
D:\other\\\x.ts |
D:\other\\\x.ts |
C:\Users\286972\project\a.ts |
~2\project\a.ts |
C:\Users\286972\project\a.ts |
C:\Users\28697ny\notes |
~ny\notes |
C:\Users\28697ny\notes |
Four rows are unchanged and two are corrected — the fix only ever turns a mangled string back into the path that was passed in, it cannot change a path that genuinely starts at the home directory, because the character after the home directory in such a path is a separator.
The same two rows have a POSIX shape that is easier to read: home /home/user, input /home/user2/project/a.ts →
~/2/project/a.ts. That one is not measured above, because this is a Windows box and path.sep is \ here — it is the
direct result of the same two expressions (startsWith then replace), and the test I am adding derives its inputs from
os.homedir() and path.sep rather than hardcoding a shape, so the sibling case fails on any OS before the fix and
passes after.
Where the mangled string is shown
src/core/commands/start.ts:66—toUserFriendlyPath(server.getCurrentWorkspace()), printed in the startup banner at:77asWorkspace: …aftertarko serve;src/config/display.ts:38-39—Failed to load config from ${displaySource}: ${error}, whose own doc comment says "always shown for user-provided paths", i.e. this is the message a user reads when the CLI could not open a file, and it now names a path that does not exist;src/config/display.ts:30and:83— the debug "Loaded config from …" and "Workspace: …" lines.
Both entry points accept an arbitrary directory, which is why the CLI cannot rule the collision out on its own.
Expected vs. Actual
Expected: C:\Users\286972\project\a.ts → unchanged (it is not under the home directory).
Actual: ~2\project\a.ts.
Suggested fix
Require the home directory to be either the whole path or followed by a separator before rewriting:
if (absolutePath === homedir) {
return '~';
}
const insideHome =
absolutePath.startsWith(homedir) &&
(homedir.endsWith(path.sep) || absolutePath[homedir.length] === path.sep);
return insideHome ? absolutePath.replace(homedir, '~') : absolutePath;Deliberately not addressed, so the diff stays a one-rule fix: a forward-slash path on Windows (C:/Users/28697/x)
still is not shortened, because the comparison is against path.sep. That is the behaviour on main today and this
change leaves it alone.
Additional context
- Of the 134 open pull requests at the time of scanning, the only one that touches
agent-cli/src/utils/misc.tsis #2008 — mine, fixing a different function in that file. OnmainresolveValueis at lines 15-30 whiletoUserFriendlyPathis at 35-43, and I kept the test in its own file (tests/user-friendly-path.test.ts) rather than extendingtests/misc.test.ts, so the two cannot conflict in source or tests. agent-clihas a working vitest setup, so the regression test ships with the change. For completeness: 4 other test files in that package fail to collect locally withFailed to resolve entry for package "@tarko/interface"and friends, which is unbuilt workspace dependencies, not a test failure — I ran the suite onmainbefore touching anything and it is the same 4 files, so this change neither causes nor fixes them.
Source: bytedance/UI-TARS-desktop