Skip to content

Commit c7c18d2

Browse files
fix(workspace): confirm a namesake in the setup dialog; preselect only my own
The post-scan setup dialog created a same-named workspace on Enter. Both of its create actions now confirm first, and it offers the caller's own namesake. Pickers preselect only a namesake the caller owns and label a colleague's. Names compare under a collator pinned to `en`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_0172qrhMa5TQgETASi5hxMqD
1 parent 77716eb commit c7c18d2

6 files changed

Lines changed: 516 additions & 95 deletions

File tree

‎packages/opencode/src/altimate/workspace/workspace-name.ts‎

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// The one piece of workspace text handling that BOTH realms need: the session
44
// code renders the name into the system prompt, and the TUI plugin renders it
5-
// into a dialog header. Kept free of imports and module state on purpose —
5+
// into a dialog header. Kept free of imports and mutable module state on purpose —
66
// `precedence.ts`, where this lived, is server-side only (see its header), and
77
// a plugin importing it would load a second copy of that module's state into
88
// the plugin realm.
@@ -61,20 +61,74 @@ function comparableName(name: string): string {
6161
return oneLine(name).replace(/[ßẞ]/g, "ss")
6262
}
6363

64-
/** The first listed workspace already named what a quick create would call this
65-
* project, or undefined. The link pickers open on it instead of on "create", and
66-
* creating a second workspace with that name takes a confirmation: two workspaces
67-
* with one name split a team's skills and memory without anyone noticing.
64+
/** The bidi controls (ALM, LRM/RLM, LRE..RLO, LRI..PDI): harmless in a link, but they
65+
* can visually reverse or reorder a displayed name. */
66+
const BIDI_CONTROLS = /[\u061c\u200e\u200f\u202a-\u202e\u2066-\u2069]/g
67+
export function stripBidiControls(text: string): string {
68+
return text.replace(BIDI_CONTROLS, "")
69+
}
70+
71+
/** A workspace name as a dialog shows it: inert, and with no bidi controls. */
72+
export function displayWorkspaceName(name: string): string {
73+
return stripBidiControls(inertWorkspaceName(name))
74+
}
75+
76+
/** One collator for every comparison, pinned to `en`: an unpinned one follows the host
77+
* locale, and under Turkish `I` no longer pairs with `i`, under Danish `aa` equals `å`. */
78+
export const NAME_COLLATOR = new Intl.Collator("en", { sensitivity: "accent", usage: "search" })
79+
80+
export interface Namesakes<T> {
81+
/** Every listed workspace with the name a quick create would use, in list order. */
82+
all: T[]
83+
/** The first of them the caller owns, or undefined. The only one a picker opens on: a plain
84+
* Enter on a namesake links to it and sends this machine's memory there, which must not
85+
* happen to a colleague's workspace by accident. */
86+
own: T | undefined
87+
}
88+
89+
/** The listed workspaces already named what a quick create would call this project.
6890
*
69-
* Names compare by Unicode collation, ignoring case but not accents: `Straße`
70-
* matches `STRASSE` and `ΟΔΟΣ` matches `οδος`, while `ı` and `i` or `café` and
71-
* `cafe` stay different. (codex) */
72-
export function sameNamedWorkspace<T extends { name: string }>(
91+
* Names compare by Unicode collation, ignoring case but not accents: `Straße` matches
92+
* `STRASSE`, `ΟΔΟΣ` matches `οδος` (final sigma) and `finance` matches `FINANCE` (ligature),
93+
* all through collation, while `ı` and `i` or `café` and `cafe` stay different.
94+
* Characters collation ignores, such as zero-width and bidi controls, do not make a name
95+
* different. Ownership counts only when both the owner and the caller are known. */
96+
export function findNamesakes<T extends { name: string; ownerId?: number }>(
7397
list: readonly T[],
7498
proposedName: string,
75-
): T | undefined {
99+
userId: number | undefined,
100+
): Namesakes<T> {
76101
const target = comparableName(proposedName)
77-
if (!target) return undefined
78-
const collator = new Intl.Collator("und", { sensitivity: "accent", usage: "search" })
79-
return list.find((workspace) => collator.compare(comparableName(workspace.name), target) === 0)
102+
if (!target) return { all: [], own: undefined }
103+
const all = list.filter((workspace) => NAME_COLLATOR.compare(comparableName(workspace.name), target) === 0)
104+
const own = userId === undefined ? undefined : all.find((workspace) => workspace.ownerId === userId)
105+
return { all, own }
106+
}
107+
108+
/** The hint a picker shows on a namesake's row, or undefined for any other row. */
109+
export function namesakeHint<T extends { name: string; ownerId?: number }>(
110+
workspace: T,
111+
namesakes: Namesakes<T>,
112+
userId: number | undefined,
113+
): string | undefined {
114+
if (!namesakes.all.includes(workspace)) return undefined
115+
const someoneElses = userId !== undefined && workspace.ownerId !== undefined && workspace.ownerId !== userId
116+
return someoneElses ? "same name, owned by someone else" : "same name as this project"
117+
}
118+
119+
/** Where a link picker opens: the current link, else the caller's own namesake, else create. */
120+
export function linkPickerOpensOn<T extends { id: number; name: string; ownerId?: number }>(
121+
currentId: number | undefined,
122+
namesakes: Namesakes<T>,
123+
): number | "create" {
124+
return currentId ?? namesakes.own?.id ?? "create"
125+
}
126+
127+
/** Whether a choice in a link picker or the setup dialog must confirm first: both create
128+
* paths start from the project's name, so either one would make a second namesake. */
129+
export function confirmsNamesake(
130+
choice: "create" | "browser" | "workspace",
131+
namesakes: Namesakes<unknown>,
132+
): boolean {
133+
return choice !== "workspace" && namesakes.all.length > 0
80134
}

‎packages/opencode/src/cli/cmd/link.ts‎

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ import {
4343
} from "@/altimate/workspace/browser-handoff"
4444
import { accountDigest, recordApprovedBinding } from "@/altimate/workspace/state"
4545
import type { SeedOutcome } from "@/altimate/workspace/memory-backfill"
46-
import { inertWorkspaceName, sameNamedWorkspace } from "@/altimate/workspace/workspace-name"
46+
import {
47+
confirmsNamesake,
48+
displayWorkspaceName,
49+
findNamesakes,
50+
linkPickerOpensOn,
51+
namesakeHint,
52+
stripBidiControls,
53+
} from "@/altimate/workspace/workspace-name"
4754

4855
const CREATE_NEW_SENTINEL = "__create_new__"
4956
const SET_UP_IN_BROWSER_SENTINEL = "__browser_handoff__"
@@ -64,7 +71,14 @@ export function stripControlChars(text: string): string {
6471
// hyperlink (the href is always `buildManageUrl`, never the name) but can visually reverse or
6572
// reorder the displayed name in the picker. (v0.11.2 release review.)
6673
// eslint-disable-next-line no-control-regex
67-
return text.replace(/[\x00-\x1f\x7f-\x9f\u061c\u200e\u200f\u202a-\u202e\u2066-\u2069]/g, "")
74+
return stripBidiControls(text.replace(/[\x00-\x1f\x7f-\x9f]/g, ""))
75+
}
76+
77+
/** What a pick in the link picker does: one of the two create paths, or an existing workspace. */
78+
export function linkPickKind(pick: string): "create" | "browser" | "workspace" {
79+
if (pick === CREATE_NEW_SENTINEL) return "create"
80+
if (pick === SET_UP_IN_BROWSER_SENTINEL) return "browser"
81+
return "workspace"
6882
}
6983

7084
/** Sanitized display name for a ``ConflictError``'s existing-binding name,
@@ -246,9 +260,11 @@ export const LinkCommand = cmd({
246260
? projectNameFromRemote(identifier.repoRemote)
247261
: projectNameFromPath(identifier.projectPath)
248262
const currentId = existing?.datamate.id
249-
// A listed workspace already named what a quick create would use. The picker
250-
// opens on it rather than on create, and creating a namesake is confirmed.
251-
const namesake = sameNamedWorkspace(list, autoName)
263+
// Listed workspaces already named what a quick create would use. The picker opens on
264+
// the caller's own (never a colleague's), and creating another namesake is confirmed.
265+
// Without a user id nothing is preselected; the confirmation still applies.
266+
const userId = await WorkspaceApi.whoami().catch(() => undefined)
267+
const namesakes = findNamesakes(list, autoName, userId)
252268
// Sanitized once here so every downstream display (the picker message,
253269
// the "Kept" outro, hyperlink()'s own text) is covered — hyperlink()
254270
// only sanitized its own `text` param, not the raw name reaching
@@ -316,12 +332,7 @@ export const LinkCommand = cmd({
316332
return {
317333
value: String(dm.id),
318334
label: dm.id === currentId ? `● ${hyperlink(safeDmName, currentManageUrl)}` : ` ${safeDmName}`,
319-
hint:
320-
dm.id === currentId
321-
? "currently linked here"
322-
: dm.id === namesake?.id
323-
? "same name as this project"
324-
: undefined,
335+
hint: dm.id === currentId ? "currently linked here" : namesakeHint(dm, namesakes, userId),
325336
}
326337
}),
327338
]
@@ -331,7 +342,9 @@ export const LinkCommand = cmd({
331342
? `Currently linked to "${hyperlink(currentName!, currentManageUrl)}". Pick a workspace (or create a new one):`
332343
: "Pick a workspace to link (or create a new one):",
333344
options,
334-
initialValue: currentId !== undefined ? String(currentId) : namesake ? String(namesake.id) : CREATE_NEW_SENTINEL,
345+
initialValue: ((at) => (at === "create" ? CREATE_NEW_SENTINEL : String(at)))(
346+
linkPickerOpensOn(currentId, namesakes),
347+
),
335348
})
336349

337350
if (prompts.isCancel(pick)) {
@@ -340,9 +353,10 @@ export const LinkCommand = cmd({
340353
}
341354

342355
// Both create paths start from the project's name, so both confirm a namesake.
343-
if ((pick === SET_UP_IN_BROWSER_SENTINEL || pick === CREATE_NEW_SENTINEL) && namesake) {
356+
const twin = namesakes.own ?? namesakes.all[0]
357+
if (twin && confirmsNamesake(linkPickKind(pick), namesakes)) {
344358
const again = await prompts.confirm({
345-
message: `A workspace named "${stripControlChars(inertWorkspaceName(namesake.name))}" already exists. Create another one with the same name?`,
359+
message: `A workspace named "${stripControlChars(displayWorkspaceName(twin.name))}" already exists. Create another one with the same name?`,
346360
initialValue: false,
347361
})
348362
if (prompts.isCancel(again) || !again) {

0 commit comments

Comments
 (0)