knowns icon indicating copy to clipboard operation
knowns copied to clipboard

[Bug]: Windows workspace registry keys by exact path string, so one folder registers as multiple workspaces

Open TheLandsharkLabs opened this issue 3 months ago • 2 comments

Disclosure: this report was written by an AI coding agent working in my environment while diagnosing and fixing this problem on my own machine. The registry contents and reproduction steps are measurements from my actual system, not generated examples. I have read the report and stand behind it. Where something was inferred rather than measured, it is flagged as a lead rather than a finding. Happy to run further diagnostics, capture logs, or test a patch on Windows.

Bug Description

On Windows, ~/.knowns/registry.json keys workspaces by the exact path string. Windows paths are case-insensitive, so C:\Users\<user>\Projects\foo and C:\Users\<user>\projects\foo are the same folder but produce two separate workspace rows. Over time the workspace switcher fills with duplicates of the same project.

On my machine this accumulated to 11 registry entries for 6 real projects — five duplicate pairs, each differing only in the casing of the Projects path segment.

The duplicate rows can also disagree about the project name. The same folder was listed once as MyProject (the name in its own .knowns/config.json) and once as myproject (the directory name), depending on which casing was used at registration time.

Why this is easy to hit. The two casings tend to come from different places in normal use, so a user does not have to do anything unusual:

  • A launcher or scheduled task that starts knowns browser --project "%USERPROFILE%\projects\..."
  • Shell history, scripts, or editor integrations that use a different casing
  • Tab-completion, which may produce either casing
  • Any automation or agent that constructs the path itself

Because nothing surfaces the mismatch, the result looks like a UI bug rather than two distinct registry keys.

Additional factor: workspace auto-scan (lead, not confirmed). The Web UI issues POST /api/workspaces/auto-scan when the workspace switcher opens, and in my environment this correlated with duplicates reappearing after cleanup. I have not instrumented the endpoint, so please treat this as a lead. What I can confirm is that the string auto-scan`,{method:"POST"} is present in the shipped 0.32.0 binary, and that suppressing the frontend's call to it stopped new duplicates appearing. If that endpoint probes a candidate folder list containing more than one casing variant (for example both projects and Projects), it would register both spellings on every switcher open, which matches what I saw.

Suggested fix. Any one of these would resolve the root cause:

  1. Canonicalize the path at registration. On Windows, resolve the true on-disk casing (via GetFinalPathNameByHandle, or by walking the path and matching each component against the parent's directory listing) before storing it.
  2. Compare case-insensitively on Windows. Use a case-folded key for registry lookup and dedupe on that platform, while still displaying the stored path.
  3. Key by project identity instead of path. .knowns/config.json already carries a stable id field. Keying the registry on that would make it immune to path spelling entirely, and would also fix the divergent-name symptom above.

A dedupe-on-read pass would additionally clean up registries that have already accumulated duplicates, without requiring users to hand-edit registry.json.

Documentation gap. I could not find auto-scan, workspace scanning behavior, or any path-casing guidance in docs/en/guides/web-ui.md, docs/en/reference/configuration.md, or docs/en/reference/commands.md, nor a config key, environment variable, or CLI flag that controls the workspace scan. If one exists, documenting it would give users a supported way to avoid this.

Steps to Reproduce

  1. Have a project at C:\Users\<user>\Projects\myproject, noting the real on-disk casing of the Projects segment.
  2. From that directory, run any Knowns command, for example knowns doctor.
  3. Reach the same folder through a differently-cased path that Windows resolves identically, for example cd C:\Users\<user>\projects\myproject (lowercase p), and run knowns doctor again.
  4. Inspect ~/.knowns/registry.json, or open the workspace switcher in the Web UI.

To confirm the two paths really are the same folder, note that directory enumeration returns only one entry:

Get-ChildItem 'C:\Users\<user>' -Directory | Where-Object { $_.Name -ieq 'projects' }
# -> a single directory; its .Name shows the true on-disk casing

Get-Item 'C:\Users\<user>\projects' is not a reliable check here — it echoes back the casing you passed in rather than the casing on disk.

Expected Behavior

One workspace entry per folder. Both spellings refer to the same directory on disk, so they should resolve to a single registry row and appear once in the workspace switcher.

Actual Behavior

Two entries for one folder, identical except for path casing, both shown as separate projects in the switcher:

{
  "id": "a1b2c3",
  "name": "MyProject",
  "path": "C:\Users\<user>\projects\myproject"
},
{
  "id": "...",
  "name": "myproject",
  "path": "C:\Users\<user>\Projects\myproject"
}

Duplicates accumulate over time. Cleaning registry.json by hand fixes it only until the next command is run with the other spelling.

Knowns CLI Version

0.32.0 (also observed on 0.30.0 and 0.31.0)

Operating System

Windows

Node.js Version

v24.19.0

Additional Context

Installed via the standalone PowerShell installer (knowns.sh/script/install.ps1). Windows 11, build 26200.

Current workaround, for anyone hitting this before a fix lands: stop all knowns.exe processes, back up ~/.knowns/registry.json, collapse entries whose paths differ only by case (keeping the one matching the true on-disk casing, and preferring the row whose name matches the project's own .knowns/config.json), then make every launcher, script, and shortcut use the real on-disk casing consistently. That last step matters as much as the cleanup.

TheLandsharkLabs avatar Aug 28 '26 14:08 TheLandsharkLabs

Thanks for the unusually careful report, and for flagging the inferred parts separately. That distinction saved time, so here is what we found reading the code.

Confirmed. internal/registry/registry.go compares paths as plain strings everywhere it matters: FindByPath uses p.Path == absPath, the dedupe pass in Load keys on p.Path verbatim, and Add only runs filepath.Abs with no casing resolution. Nothing folds case or resolves the true on-disk spelling, so your diagnosis of the root cause is exactly right.

Your "lead" is actually a finding. The auto-scan candidate list in internal/server/routes/workspace.go hardcodes both casings side by side:

filepath.Join(home, "Projects"),
filepath.Join(home, "projects"),
...
filepath.Join(home, "Code"),  filepath.Join(home, "code"),
filepath.Join(home, "repos"), filepath.Join(home, "Repos"),

On a case-insensitive filesystem os.Stat succeeds for both spellings, so both land in the scan list, Scan walks the same folder twice, FindByPath does not recognize the second pass as a duplicate, and two rows get written. That means the bug does not require the user to type anything unusual at all, which lines up with what you observed.

The recurrence after cleanup is confirmed too: ui/src/components/organisms/WorkspacePicker.tsx calls autoScan() on mount, so simply opening the switcher re-runs it. Suppressing that call stopping new duplicates is consistent with the code path.

Not Windows-only. macOS APFS is case-insensitive by default, so ~/Projects and ~/projects behave identically there. We are treating this as a case-insensitive-filesystem bug rather than a Windows bug.

One detail we could not reproduce. You noted the duplicate rows disagreeing about name (MyProject vs myproject), with one taken from .knowns/config.json. In the current code Add always derives the name from filepath.Base(absPath) and never reads config.json, so both rows should carry the folder name. The most likely explanation is that the final path segment itself differed in casing between the two registrations. If you still have the original registry.json backup and the two path values differ only in the Projects segment while name differs, that would point at something we have not found yet, and we would like to see it.

Fix we are taking, close to your option 1:

  1. Canonicalize the path at registration by walking each component and matching it against the parent's directory listing, so the stored path always carries the true on-disk casing. Doing it by directory listing rather than by case folding keeps it correct on case-sensitive filesystems with no platform branch, since two genuinely distinct directories differing only by case must stay two workspaces on Linux.
  2. Collapse the auto-scan candidate list to one entry per real directory before scanning. Auto-scan itself stays, since it is locked behavior in the spec that introduced it.
  3. Dedupe on read, so registries that already accumulated duplicates collapse without hand-editing. Paths that no longer resolve on disk are left alone rather than merged blindly.

Option 3 in your report, keying on the id from .knowns/config.json, is the more robust design and we agree, but it changes the registry format and needs a migration, so it is a separate piece of work rather than the fix for this.

Documentation gap confirmed. There is no mention of auto-scan anywhere under docs/, and no config key, environment variable, or flag that controls it. Documenting the scan and the directories it covers is part of this fix.

The fix is implemented on develop and covered by tests. Because the assertions for case-sensitive filesystems skip on a normal macOS machine, they were also run against a case-sensitive APFS volume mounted for the purpose, so both halves of the behavior are exercised.

We will take you up on the offer to test on Windows once there is a build, if that still works for you. Your reproduction steps and the note about Get-Item echoing back the casing you pass were both useful, so thank you for those.

howznguyen avatar Sep 03 '26 10:09 howznguyen

Thanks, but I can't take credit. My AI friend that is helping me gets the kudos... Hopefully this response helps. Just know the first person references are not actually "me" ha!

Disclosure: as with the original report, this reply is AI-generated. I pasted your comment into the coding agent I have been using on this machine, and it did the analysis below and wrote this response. The registry inspection, the four-of-five breakdown, and the caveat about the non-random id are all its work, not mine — I do not want to take credit for reasoning I did not do.

What is mine: the machine, the actual registry.json data it read, and a review of this before posting. The offer to test a Windows build is a commitment I am making personally.

Good to know the auto-scan part was a real finding rather than a lead. That also explains something on my side: a previous workaround here suppressed the frontend's autoScan() call, and the reason that worked now makes sense against WorkspacePicker.tsx.

I still have the pre-cleanup registry.json, so here is the data on the name divergence.

It is one pair out of five, and the final path segment is identical. All names below are placeholders, but the structure and casing are exactly as recorded:

id=r4k2xp  name=ProjectB           path=C:\Users\<user>\Projects\ProjectB
id=m8vq1t  name=ProjectB           path=C:\Users\<user>\projects\ProjectB

id=c7hz3w  name=ProjectC           path=C:\Users\<user>\Projects\ProjectC
id=b2ndk9  name=ProjectC           path=C:\Users\<user>\projects\ProjectC

id=myproj  name=MyProject          path=C:\Users\<user>\projects\myproject     <-- diverges
id=w9mkeq  name=myproject          path=C:\Users\<user>\Projects\myproject     <-- diverges

id=q3lz8y  name=ProjectD           path=C:\Users\<user>\projects\ProjectD
id=s1gf1n  name=ProjectD           path=C:\Users\<user>\Projects\ProjectD

id=e4va0l  name=ProjectE           path=C:\Users\<user>\Projects\ProjectE
id=7sa70s  name=ProjectE           path=C:\Users\<user>\projects\ProjectE

So four of the five pairs behave exactly as your filepath.Base explanation predicts — both rows carry the folder basename. Only one pair diverges, and in that pair the two paths differ only in the Projects segment. The final segment is myproject in both, which rules out the differing-final-segment explanation.

In that pair, the divergent row's name (MyProject) is the name field from that project's .knowns/config.json, while the other row's name (myproject) is the folder basename.

One caveat before you go looking, because it may be a red herring. That same row is anomalous in a second way: its id is myproj, which looks derived from the project name, whereas every other id in the file is a random six-character string (r4k2xp, m8vq1t, c7hz3w, …). It is also not the id from that project's config.json, which is a different value again.

So there are two readings, and I cannot distinguish them from the data I have:

  1. There is a second registration path that reads config.json — which would fit an older note in my own setup claiming the CLI/MCP registers a row differently from the scanner.
  2. That row is simply old, hand-created, or written by a much earlier Knowns version, and is not evidence of a live code path at all.

Given the non-random id, I would not rule out (2), and I would not want to send you chasing something that turns out to be legacy residue. If it helps, the row predates my current install; I do not have a reliable creation timestamp for it.

Happy to test a Windows build whenever one is ready. On this machine the filesystem is NTFS, case-insensitive, with the real on-disk spelling being C:\Users\<user>\Projects, and both spellings reaching it. I can also verify the dedupe-on-read path against a registry that already contains duplicates, since I still have the original file.

TheLandsharkLabs avatar Sep 04 '26 15:09 TheLandsharkLabs