Skip to content

Commit 891f264

Browse files
fix(workspace): /integrations claims its dialog before reading, and closes it before the picker
A dialog the user opens while the link and engine are read is no longer replaced, and Link closes the dialog first so a repeated Enter cannot start a second picker, as in `/workspace`. If no install offer opens after all, the Status view shows instead of nothing. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01HRUhVhg6XwxSKJs1iTufiA
1 parent 4317ece commit 891f264

2 files changed

Lines changed: 101 additions & 36 deletions

File tree

‎packages/opencode/src/plugin/tui/altimate/workspace.tsx‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,8 +2439,10 @@ async function readLink(directory: string) {
24392439
return { report, bound }
24402440
}
24412441

2442-
/** `/integrations` in an unlinked project: integrations come from a link, so offer one. */
2443-
function showNotLinked(api: TuiPluginApi, directory: string): void {
2442+
/** `/integrations` in an unlinked project: integrations come from a link, so
2443+
* offer one. Shown only while `from`, the dialog this run opened, is still up. */
2444+
function showNotLinked(api: TuiPluginApi, directory: string, from: symbol): void {
2445+
if (ownedDialog !== from) return
24442446
replaceOwnedDialog(api, () => (
24452447
<api.ui.DialogSelect
24462448
title="Integrations come from a linked workspace"
@@ -2451,6 +2453,8 @@ function showNotLinked(api: TuiPluginApi, directory: string): void {
24512453
current="link"
24522454
onSelect={(option) => {
24532455
if (option.value === "link") {
2456+
// Closed first so repeated Enter while the pre-check is slow cannot start more pickers.
2457+
api.ui.dialog.clear()
24542458
runOnDemandPicker(api, directory).catch((err) => reportFlowFailure(api, err))
24552459
return
24562460
}
@@ -2465,22 +2469,28 @@ function showNotLinked(api: TuiPluginApi, directory: string): void {
24652469
* opens the install offer, which says which, instead of the last attach: that
24662470
* attach no longer describes what a session here gets. */
24672471
export async function runIntegrations(api: TuiPluginApi, directory: string): Promise<void> {
2468-
if (!(await readLink(directory)).bound) return showNotLinked(api, directory)
2469-
if (await describeOffer(directory)) {
2470-
await showEngineInstallOffer(api, { onDemand: true })
2471-
return
2472-
}
2473-
// The engine check can take seconds: read the link again, so a relink or an
2474-
// account switch meanwhile cannot put the previous link's attach on screen.
2475-
const { bound } = await readLink(directory)
2476-
if (!bound) return showNotLinked(api, directory)
2472+
// Claimed before the first read, so a dialog the user opens while the reads
2473+
// run is theirs: each step below shows something only while this one is up.
24772474
const reading = replaceOwnedDialog(api, () => (
24782475
<api.ui.DialogSelect
24792476
title="Integrations"
2480-
options={[{ title: "Reading the last attach…", value: "done", description: "" }]}
2477+
options={[{ title: "Reading the link and the last attach…", value: "done", description: "" }]}
24812478
onSelect={() => api.ui.dialog.clear()}
24822479
/>
24832480
))
2481+
if (!(await readLink(directory)).bound) return showNotLinked(api, directory, reading)
2482+
if (await describeOffer(directory)) {
2483+
if (ownedDialog !== reading) return
2484+
await showEngineInstallOffer(api, { onDemand: true })
2485+
// An offer that opened replaced this dialog. Still up means none did: the
2486+
// engine became usable meanwhile, an install is running, or an earlier
2487+
// raise holds the offer. Show the Status view rather than nothing.
2488+
if (ownedDialog !== reading) return
2489+
}
2490+
// The engine check can take seconds: read the link again, so a relink or an
2491+
// account switch meanwhile cannot put the previous link's attach on screen.
2492+
const { bound } = await readLink(directory)
2493+
if (!bound) return showNotLinked(api, directory, reading)
24842494
await showWorkspaceStatus(api, directory, bound, reading)
24852495
}
24862496

‎packages/opencode/test/altimate/plugin/workspace.test.ts‎

Lines changed: 79 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,12 +1063,19 @@ describe("link dialogs: a workspace that already has this project's name", () =>
10631063

10641064
describe("/integrations", () => {
10651065
// The command opens the Status view directly, or says why it has nothing to
1066-
// show. The link and Status dialogs are rendered and read; the install offer
1067-
// is not (it needs the TUI's clipboard), so that case counts what reached it.
1066+
// show. The stand-in dialog stack closes the dialog it replaces, like the
1067+
// real one, and renders the first `render` dialogs; the install offer is not
1068+
// rendered (it needs the TUI's clipboard), so those cases count replacements.
10681069
const binding = { datamateId: 42, datamateName: "analytics" }
1069-
type Select = { title: string; options: { title: string; value: unknown; description?: string }[]; current?: unknown }
1070-
function harness(render = true) {
1071-
const h = { selects: [] as Select[], replaced: 0, toasts: [] as string[] }
1070+
type Select = { title: string; options: { title: string; value: unknown; description?: string }[]; current?: unknown; onSelect: (o: { value: unknown }) => void }
1071+
function harness(render = Number.POSITIVE_INFINITY) {
1072+
const h = { selects: [] as Select[], replaced: 0, cleared: 0, toasts: [] as string[] }
1073+
let onClose: (() => void) | undefined
1074+
const close = () => {
1075+
const previous = onClose
1076+
onClose = undefined
1077+
previous?.()
1078+
}
10721079
const tui = {
10731080
state: { path: { directory: os.tmpdir() } },
10741081
kv: { ...makeKv(), ready: true },
@@ -1081,11 +1088,16 @@ describe("/integrations", () => {
10811088
h.toasts.push(t.message)
10821089
},
10831090
dialog: {
1084-
replace: (factory: () => unknown) => {
1091+
replace: (factory: () => unknown, closed?: () => void) => {
1092+
close()
1093+
onClose = closed
10851094
h.replaced += 1
1086-
if (render) createRoot(() => factory())
1095+
if (h.replaced <= render) createRoot(() => factory())
1096+
},
1097+
clear: () => {
1098+
close()
1099+
h.cleared += 1
10871100
},
1088-
clear: () => {},
10891101
},
10901102
},
10911103
} as any
@@ -1122,9 +1134,31 @@ describe("/integrations", () => {
11221134
linked(null)
11231135
const { tui, h } = harness()
11241136
await runIntegrations(tui, os.tmpdir())
1125-
expect(h.selects).toHaveLength(1)
1126-
expect(h.selects[0]!.options.map((o) => o.value)).toEqual(["link", "done"])
1127-
expect(h.selects[0]!.current).toBe("link")
1137+
expect(h.selects.map((s) => s.title)).toEqual(["Integrations", "Integrations come from a linked workspace"])
1138+
expect(h.selects[1]!.options.map((o) => o.value)).toEqual(["link", "done"])
1139+
expect(h.selects[1]!.current).toBe("link")
1140+
})
1141+
1142+
test("Link closes the dialog before the picker starts, so a second Enter cannot start another", async () => {
1143+
linked(null)
1144+
spies.push(spyOn(WorkspaceApi, "getBindingForProject").mockResolvedValue(null as any))
1145+
const { tui, h } = harness(2)
1146+
await runIntegrations(tui, os.tmpdir())
1147+
h.selects[1]!.onSelect({ value: "link" })
1148+
expect(h.cleared).toBe(1)
1149+
})
1150+
1151+
test("a dialog opened while the link is read is left alone", async () => {
1152+
const { tui, h } = harness()
1153+
spies.push(
1154+
spyOn(Manage, "status").mockImplementation(async () => {
1155+
tui.ui.dialog.replace(() => null) // the user opens something else meanwhile
1156+
return { binding: null } as any
1157+
}),
1158+
)
1159+
await runIntegrations(tui, os.tmpdir())
1160+
expect(h.replaced).toBe(2)
1161+
expect(h.selects.map((s) => s.title)).toEqual(["Integrations"])
11281162
})
11291163

11301164
test.each([
@@ -1134,10 +1168,10 @@ describe("/integrations", () => {
11341168
linked(binding)
11351169
engine(version)
11361170
const read = statusRead()
1137-
const { tui, h } = harness(false)
1171+
const { tui, h } = harness(1)
11381172
recordEngineSkip(tui, "42", { tenant: "acme", apiUrl: "https://api.acme.example.com" }, Date.now())
11391173
await runIntegrations(tui, os.tmpdir())
1140-
expect(h.replaced).toBe(1)
1174+
expect(h.replaced).toBe(2)
11411175
expect(engineOfferInternals.visible).toBe(true)
11421176
expect(read).not.toHaveBeenCalled()
11431177
})
@@ -1146,37 +1180,58 @@ describe("/integrations", () => {
11461180
linked(binding)
11471181
engine(null)
11481182
statusRead()
1149-
const { tui, h } = harness(false)
1183+
const { tui, h } = harness(2)
11501184
let ready = false
11511185
Object.defineProperty(tui.kv, "ready", { get: () => ready })
11521186
recordEngineSkip(tui, "42", { tenant: "acme", apiUrl: "https://api.acme.example.com" }, Date.now())
1153-
// The turn's raise holds the slot while the store hydrates; the request finds it taken.
1187+
// The turn's raise holds the slot while the store hydrates; the request finds it taken and
1188+
// shows Status meanwhile.
11541189
const automatic = showEngineInstallOffer(tui)
11551190
await runIntegrations(tui, os.tmpdir())
1156-
expect(h.replaced).toBe(0)
1191+
expect(h.selects.map((s) => s.title)).toEqual(["Integrations", "Workspace status"])
11571192
ready = true
11581193
await automatic
1159-
expect(h.replaced).toBe(1)
1194+
expect(h.replaced).toBe(3)
1195+
expect(engineOfferInternals.visible).toBe(true)
11601196
})
11611197

1162-
test("an install still running is reported, not offered again", async () => {
1198+
test("an install still running is reported, and the Status view shown, not a second offer", async () => {
11631199
linked(binding)
11641200
engine(null)
1201+
statusRead()
11651202
engineOfferInternals.set({ inFlight: true })
1166-
const { tui, h } = harness(false)
1203+
const { tui, h } = harness()
11671204
await runIntegrations(tui, os.tmpdir())
1168-
expect(h.replaced).toBe(0)
11691205
expect(h.toasts.join(" ")).toContain("still installing")
1206+
expect(h.selects.map((s) => s.title)).toEqual(["Integrations", "Workspace status"])
1207+
})
1208+
1209+
test("an engine that turns usable before the offer opens gets the Status view, not nothing", async () => {
1210+
linked(binding)
1211+
engine(null)
1212+
statusRead()
1213+
// The first check finds no engine; by the offer's own check, another process has installed it.
1214+
let checks = 0
1215+
syncInternals.which = () => (++checks === 1 ? null : "/usr/local/bin/datamate")
1216+
syncInternals.versionOf = async () => "0.7.3"
1217+
const { tui, h } = harness()
1218+
await runIntegrations(tui, os.tmpdir())
1219+
expect(h.selects.map((s) => s.title)).toEqual(["Integrations", "Workspace status"])
11701220
})
11711221

11721222
test("a relink during the engine check shows the new link's attach, not the old one's", async () => {
1173-
// The first read sees workspace 42; by the time the engine check is done the project is linked to 43.
1223+
// The project is relinked to workspace 43 while the version probe runs.
1224+
let relinked = false
11741225
spies.push(
1175-
spyOn(Manage, "status")
1176-
.mockResolvedValueOnce({ binding } as any)
1177-
.mockResolvedValue({ binding: { datamateId: 43, datamateName: "marketing" } } as any),
1226+
spyOn(Manage, "status").mockImplementation(
1227+
async () => ({ binding: relinked ? { datamateId: 43, datamateName: "marketing" } : binding }) as any,
1228+
),
11781229
)
11791230
engine("0.7.3")
1231+
syncInternals.versionOf = async () => {
1232+
relinked = true
1233+
return "0.7.3"
1234+
}
11801235
const read = statusRead()
11811236
const { tui } = harness()
11821237
await runIntegrations(tui, os.tmpdir())

0 commit comments

Comments
 (0)