Skip to content

feat(user): confirm creation of unknown user profiles - #127

Merged
ehennestad merged 10 commits into
devfrom
claude/nansen-user-session-confirmation-f49a71
Nov 6, 2026
Merged

ehennestad merged 10 commits into
devfrom
claude/nansen-user-session-confirmation-f49a71

Conversation

@ehennestad

@ehennestad ehennestad commented Sep 10, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

NansenUserSession.instance(userName) creates a user profile for any name it is given. A name that does not exist yet is indistinguishable from a name that does, so a typo silently produces a new profile and its preference directory. Repeated typos accumulate profiles that nobody asked for.

Change

instance now asks the user to confirm before a profile is created for a name that does not exist, and lists the profiles that already exist so a misspelled name sits next to the name it was meant to be:

No NANSEN user profile named 'jhon' exists.
Existing user profiles: anon_user, default, john
Do you want to create a new user profile named 'jhon'?
(y/N):

Declining, or pressing Enter, throws NANSEN:UserSession:UserProfileCreationAborted and creates nothing.

The check runs before the singleton is inspected or torn down. Placing it later would mean an active session is closed by the mode == "check" branch first, so declining the typo would cost the user the session they already had.

It is skipped in four cases: the default profile, which is created implicitly on first run; a name whose profile already exists; the nocreate and reset modes, which never construct a session; and non-interactive sessions, where input errors rather than blocks. In a non-interactive session the profile is created without a question.

Name matching is case sensitive

isExistingUser compares against the profile names returned by listUserNames rather than testing the preference directory with isfolder. The file system matches names case insensitively on macOS and Windows, so isfolder accepts John as the existing profile john, and the session then runs under a name that does not match the directory it reads and writes. A name that differs from an existing profile only by case is refused with NANSEN:UserSession:UserNameCaseMismatch, which names the existing profile, also under matlab -batch. On macOS and Windows a profile for such a name would share the existing profile's directory.

Declining stops the caller

nansen printed an extended error report with a stack trace for a declined prompt, because the new identifier fell through to the catch-all branch of its switch. nansen.setup downgraded the error to a warning and then ran the setup wizard anyway, so declining had no effect on what setup did. Both now report the message and stop.

Shared yes/no prompt

The prompt asking whether to close an active session was a second hand-rolled input block accepting only exact lowercase y/n and erroring without an identifier. Both questions now go through one askYesNo helper, so they accept the same answers (y, yes, n, no in any case; an empty answer counts as no) and report unexpected input the same way. Its commented out dead code is dropped.

getPrefdir and listUserNames share getUserRootDirectory instead of each spelling out the directory layout.

Opting out

Programmatic callers pass ConfirmNewUser=false. nansen.fixture.ProjectFixture uses this for the throwaway test profiles it creates, which must not block on input.

batchStartupOptionUsed covers matlab -batch. It does not cover matlab -nodisplay -r, where input reads from stdin; automation using that form needs ConfirmNewUser=false explicitly. Gating on ~usejava('desktop') instead was rejected because it would remove the prompt for legitimate -nodesktop terminal use.

Tests

tests/+nansen/+unittest/+user/NansenUserSessionTest.m covers listUserNames (membership, sort order, exclusion of the . and .. entries dir returns), isExistingUser including the case sensitive lookup, the refusal of a name that differs from a profile only by case, and the instance paths that must not prompt: ConfirmNewUser=false creates the profile, and nocreate returns empty without leaving a directory behind or checking the name.

The interactive branches are not covered. input cannot be substituted from a test without moving the read behind an injectable handle, which is a design change worth making separately if that coverage is wanted.

Profile directories in the tests are created under MATLAB's real preference directory, because getPrefdir derives its path from prefdir, which cannot be redirected from a running session. Names end with a uuid, so test processes that start at the same time do not share a profile, and teardown tolerates a missing directory so a test that fails early does not report a second, unrelated failure.

Documentation

The class header and the instance help both stated that user profiles were not supported yet. The instance help said so three lines above the paragraph describing the new name-value argument. Both now describe what the user name does.

🤖 Generated with Claude Code

Starting a user session for a name that does not exist silently created
a new user profile, so a typo in the name left an unwanted profile
behind.

NansenUserSession.instance now asks the user to confirm before a profile
is created for an unknown name, and lists the profiles that already
exist so a misspelled name is easy to spot. Declining throws
NANSEN:UserSession:UserProfileCreationAborted and creates nothing.

The check runs before an active session is closed, so declining leaves
the current session untouched. It is skipped for the default profile,
for the nocreate and reset modes, and in non-interactive sessions, where
input is unavailable. Programmatic callers opt out with the new
ConfirmNewUser name-value argument, which ProjectFixture uses for the
throwaway profiles it creates.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

444 tests   444 ✅  54s ⏱️
 34 suites    0 💤
  1 files      0 ❌

Results for commit 7b069a5.

♻️ This comment has been updated with latest results.

github-actions Bot and others added 3 commits September 10, 2026 12:17
Address review findings on the confirmation guard.

A declined prompt is a user decision, but neither entry point treated it
as one. nansen printed an extended error report with a stack trace,
because the new identifier fell through to the catch-all branch.
nansen.setup downgraded the error to a warning and then ran the setup
wizard anyway, so declining had no effect. Both now report the message
and stop.

isExistingUser tested for the preference directory with isfolder, which
matches names case insensitively on macOS and Windows. A capitalisation
typo was accepted as the existing profile and the session then ran under
a name that did not match its own directory. Profile names are now
compared directly, and a name that differs from an existing profile only
by capitalisation is called out in the prompt.

The yes/no prompt asking whether to close an active session is replaced
by the shared askYesNo helper, so both questions in this class accept the
same answers and report unexpected input with an identifier. Its
commented out dead code is dropped.

getPrefdir and listUserNames now share getUserRootDirectory instead of
each spelling out the directory layout.

Tests: cover the case sensitive lookup, assert names are present before
comparing their positions, and tolerate a missing directory during
teardown so a failing test does not report a second, unrelated failure.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
ehennestad and others added 3 commits November 5, 2026 11:26
Resolves the conflict on the generated badge files by taking the
versions from dev. No code files conflicted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
confirmNewUserProfile asked whether to create a profile for a name that
differs from an existing profile only by capitalisation. On macOS and
Windows the answer yes created nothing: the session ran under the new
name on the existing profile's directory, and listUserNames kept
returning the old name, so the question came back on every call. Such a
name now throws NANSEN:UserSession:UserNameCaseMismatch, which names
the existing profile. The check runs before the -batch exemption, so
scripts get the error too, and nansen and nansen.setup print it without
a stack trace.

askYesNo counts an empty answer as no and shows (y/N), so pressing
Enter declines instead of raising UnexpectedInput.

The ConfirmNewUser help lists when no question is asked. The tests build
profile names from a uuid, because test processes that start together
share prefdir and the same random number seed; two tests cover the case
refusal and its exemption in 'nocreate' mode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ehennestad

Copy link
Copy Markdown
Collaborator Author

Review

Verdict: ready to merge once CI passes on 020a29e.

The guard sits in the right place: it runs before the singleton is inspected, so declining leaves an active session untouched. nocreate, reset, the default profile and existing names never prompt.

Fixed on the branch:

  • The PR conflicted with dev on .github/badges/tests.svg, so no CI ran. 150524c merges dev and takes its badge files. CI passed on that commit.
  • 020a29e, case mismatch: with a profile john, nansen("John") asked whether to create a new profile John. On macOS and Windows, answering yes created nothing. Preferences.buildFilePath found prefdir/Nansen/John as the existing folder john, so the session ran as John on john's directory. listUserNames kept returning john, so the same question came back on every later call. Such a name now throws NANSEN:UserSession:UserNameCaseMismatch, which names the existing profile. The check runs before the -batch exemption, so scripts get the error too. nansen and nansen.setup print it without a stack trace, like the other two identifiers.
  • 020a29e, tests: buildUserName used randi. A fresh MATLAB process starts from the same random number seed, so two test processes that run at the same time would create and delete the same unittest_user_<n> folder in the shared prefdir, the same problem test(fixture): derive the ProjectFixture profile name from a uuid #161 fixes in ProjectFixture. Names now end with a uuid. Two new tests cover the case refusal and its exemption in nocreate mode. The case refusal test also runs in CI, where batchStartupOptionUsed is true.
  • 020a29e, smaller changes: askYesNo counts an empty answer as no and shows (y/N). Before, pressing Enter raised UnexpectedInput after the prompt had closed. The ConfirmNewUser help now lists when no question is asked, including that -batch creates a profile without asking. A test comment that described the PR's own first commit now states the constraint instead. Two char literals in the tests are now strings.
  • The description is updated to match: the example prompt, the case refusal, and the empty answer.

All 10 tests in NansenUserSessionTest pass in an R2025b batch run.

Not changed:

  • Under matlab -batch an unknown name still creates a profile without asking, as the description says. Refusing unknown names in batch mode would break scripts that create a profile on purpose.
  • The interactive branches are not tested, because input cannot be substituted from a test.

Out of scope (exists on dev):

  • The active-session question (mode check) has no non-interactive check. Under -batch, input raises MATLAB:services:MissingRequiredCapability.
  • instance(name, "nocreate") returns the active session even when it belongs to another user.

Merge notes:

🤖 Generated with Claude Code

@ehennestad
ehennestad merged commit f039c7b into dev Nov 6, 2026
ehennestad added a commit that referenced this pull request Nov 6, 2026
Conflicts:
- .github/badges/tests.svg: dev's version.
- Preferences.m: dev's fromStruct (#172). It skips saved preferences
  the class does not declare, as this branch's version did.

NansenUserSession.m merges without a conflict, but the merge puts
getUserDataDirectory and getDefaultUserDataDirectory after #127's
confirmNewUserProfile, inside its methods (Static, Access = private)
block. They are moved back to the public static block, because
nansen.userdatadir, clear_tutorial_data, ProjectFixture and
UserDataDirectoryTest call them from outside the class.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ehennestad added a commit that referenced this pull request Nov 6, 2026
listUserNames, from #127, listed the folders in prefdir/Nansen, which
belongs to one MATLAB release. A profile created in another release
has only its user data directory, <userpath>/Nansen/profiles/<name>,
in this one. The confirmation prompt then called it unknown, and the
capitalisation check could not see it: nansen("John") could create a
profile next to an existing "john" from another release, and on macOS
and Windows the two used the same user data directory.

listUserNames now also lists the default user data root. The path of
that root moves into getDefaultUserDataRoot, which
getDefaultUserDataDirectory also uses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ehennestad added a commit that referenced this pull request Nov 8, 2026
listUserNames, from #127, listed the folders in prefdir/Nansen, which
belongs to one MATLAB release. A profile created in another release
has only its user data directory, <userpath>/Nansen/profiles/<name>,
in this one. The confirmation prompt then called it unknown, and the
capitalisation check could not see it: nansen("John") could create a
profile next to an existing "john" from another release, and on macOS
and Windows the two used the same user data directory.

listUserNames now also lists the default user data root. The path of
that root moves into getDefaultUserDataRoot, which
getDefaultUserDataDirectory also uses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ehennestad
ehennestad deleted the claude/nansen-user-session-confirmation-f49a71 branch November 8, 2026 16:38
ehennestad added a commit that referenced this pull request Nov 9, 2026
listUserNames, from #127, listed the folders in prefdir/Nansen, which
belongs to one MATLAB release. A profile created in another release
has only its user data directory, <userpath>/Nansen/profiles/<name>,
in this one. The confirmation prompt then called it unknown, and the
capitalisation check could not see it: nansen("John") could create a
profile next to an existing "john" from another release, and on macOS
and Windows the two used the same user data directory.

listUserNames now also lists the default user data root. The path of
that root moves into getDefaultUserDataRoot, which
getDefaultUserDataDirectory also uses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant