Skip to content

Commit 9d64d5d

Browse files
hkaltaiclaude
andcommitted
fix: an attach that would share nothing is not refused, and the CLI wording and keychain kill are tested
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ecf9ec7 commit 9d64d5d

3 files changed

Lines changed: 23 additions & 22 deletions

File tree

‎packages/opencode/src/altimate/integrations/core.ts‎

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -885,8 +885,7 @@ export function withVariant(id: ProviderId, variant: string | undefined, config:
885885
/** The workspace catalog may name the Server / Data Center variant differently; any id naming "server" is it. */
886886
export const isJiraServer = (variant: string | undefined) => /server/i.test(variant ?? "")
887887

888-
/** The connection config to upload for a provider: the saved credential when it was connected here, else what connect would read from this machine. Never printed. */
889-
/** `saved` when the config is the credential saved here rather than one detected on this machine. */
888+
/** The connection config to upload for a provider: the saved credential when it was connected here (`saved`), else what connect would read from this machine. Never printed. */
890889
export async function collectConfig(id: ProviderId, rec: StoredProvider | undefined): Promise<{ config: Record<string, unknown>; saved?: true } | { reason: string }> {
891890
if (rec?.registered && rec.variant) {
892891
const config: Record<string, unknown> = { ...rec.values }
@@ -1015,18 +1014,9 @@ async function attachToWorkspace(cfg: backend.BackendConfig, id: ProviderId, con
10151014
const first = await backend.getDatamate(cfg, datamateId)
10161015
if (first.is_owner === false)
10171016
throw new Error(`only the owner of workspace "${first.name}" can attach connections to it; your connection is saved to your account. Link a workspace you own, or sign in as its owner`)
1018-
// Attaching widens the connection to the workspace's audience, so only a workspace that says it is private takes it unasked.
1019-
// A connection the workspace already uses was shared before; keeping it attached widens nothing.
10201017
const conn = String(connectionId)
1021-
const allowed = (d: backend.DatamateRecord) => {
1022-
const shared = d.privacy !== "private"
1023-
const attached = d.integrations.some(
1024-
(e) => String(e.id) === integrationId && (String(e.connection_id ?? "") === conn || (Array.isArray(e.connection_ids) && e.connection_ids.map(String).includes(conn))),
1025-
)
1026-
if (shared && !share && !attached) throw new Error(SHARED_WORKSPACE_REFUSAL(d.name, id))
1027-
return shared
1028-
}
1029-
allowed(first)
1018+
const usesThis = (entries: backend.DatamateIntegrationEntry[]) =>
1019+
entries.some((e) => String(e.id) === integrationId && (String(e.connection_id ?? "") === conn || (Array.isArray(e.connection_ids) && e.connection_ids.map(String).includes(conn))))
10301020
let keys: string[] = []
10311021
if (!first.integrations.some((e) => String(e.id) === integrationId)) {
10321022
const catalog = await backend.integrationsCatalog(cfg)
@@ -1035,16 +1025,17 @@ async function attachToWorkspace(cfg: backend.BackendConfig, id: ProviderId, con
10351025
if (!found) throw new Error(`the workspace catalog has no entry for ${PROVIDER_NAMES[id]}, so the connection was not attached to workspace "${first.name}"`)
10361026
keys = (found.tools ?? []).map((t: any) => t?.key).filter((k: unknown): k is string => typeof k === "string")
10371027
}
1038-
// The backend replaces the whole list, so it is read again right before writing and only this entry changes. Its privacy is checked again: it may have changed since.
1028+
// The backend replaces the whole list, so it is read again right before writing and only this entry changes.
10391029
const datamate = await backend.getDatamate(cfg, datamateId)
1040-
const shared = allowed(datamate)
10411030
const live = liveIds ?? (await backend.listVisibleConnections(cfg)).map((c) => c.id)
10421031
const next = attachToIntegrations(datamate.integrations, integrationId, connectionId, keys, live)
1032+
// Attaching widens the connection to the workspace's audience, so a workspace that does not say it is private (in this latest read) only gets it
1033+
// when asked. Members can use it only when the entry ends up naming it: a connection already there widens nothing more, and neither does an entry kept on another one.
1034+
const shared = datamate.privacy !== "private" && usesThis(next)
1035+
if (shared && !share && !usesThis(datamate.integrations)) throw new Error(SHARED_WORKSPACE_REFUSAL(datamate.name, id))
10431036
await backend.updateDatamateIntegrations(cfg, datamateId, next)
10441037
const entry = next.find((e) => String(e.id) === integrationId)
1045-
const usesThis = String(entry?.connection_id ?? "") === conn || (Array.isArray(entry?.connection_ids) && entry.connection_ids.map(String).includes(conn))
1046-
// Members can use this connection only when the entry now names it; a kept entry on another connection widens nothing.
1047-
return { kept: entry?.connection_id != null && String(entry.connection_id) !== conn, shared: shared && usesThis }
1038+
return { kept: entry?.connection_id != null && String(entry.connection_id) !== conn, shared }
10481039
}
10491040

10501041
/** The integrations list without references to the connection; other entries pass through untouched. */

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,15 @@ const DetectCommand = cmd({
9898
},
9999
})
100100

101+
/** Who can use a synced connection: only the user, unless it was attached to a workspace that is not private. */
102+
export const syncedTo = (o: Pick<core.SyncOutcome, "shared">) => `synced to workspace${o.shared ? ", shared with its members" : " (private)"}`
103+
101104
function renderSync(o: core.SyncOutcome) {
102105
const name = core.PROVIDER_NAMES[o.id]
103106
if (o.result === "kept_local") return out(` ${S.TEXT_WARNING}⊘${S.TEXT_NORMAL} ${name}: kept local (${clean(o.reason)})`)
104107
if (o.result === "failed") return out(` ${S.TEXT_DANGER}✗${S.TEXT_NORMAL} ${name}: sync failed (${clean(o.reason)})`)
105108
if (o.result === "skipped") return out(` ${S.TEXT_DIM}- ${name}: skipped (${clean(o.reason)})${S.TEXT_NORMAL}`)
106-
out(` ${S.TEXT_SUCCESS}✓${S.TEXT_NORMAL} ${name} synced to workspace${o.shared ? ", shared with its members" : " (private)"}`)
109+
out(` ${S.TEXT_SUCCESS}✓${S.TEXT_NORMAL} ${name} ${syncedTo(o)}`)
107110
for (const st of o.steps) if (!st.ok && st.label.startsWith(core.LOCAL_CLEANUP_FAILED_PREFIX)) out(` ${S.TEXT_WARNING}!${S.TEXT_NORMAL} ${clean(st.label)}`)
108111
}
109112

‎packages/opencode/test/altimate/integrations.test.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { EventEmitter } from "node:events"
66
import { PassThrough } from "node:stream"
77
import path from "node:path"
88
import { IntegrationsCore as core } from "../../src/altimate/integrations/core"
9-
import { skipsSynced, clean } from "../../src/cli/cmd/integrations"
9+
import { skipsSynced, clean, syncedTo } from "../../src/cli/cmd/integrations"
1010
import { IntegrationsBackend as backend } from "../../src/altimate/integrations/backend"
1111
import { IntegrationsCredentials as creds } from "../../src/altimate/integrations/credentials"
1212
import { AltimateApi } from "../../src/altimate/api/client"
@@ -222,6 +222,7 @@ describe("keychain", () => {
222222
const realDir = process.env.ALTIMATE_INTEGRATIONS_DIR
223223
const platform = Object.getOwnPropertyDescriptor(process, "platform")!
224224
const calls: { cmd: string; args: string[]; input?: string }[] = []
225+
const kills: string[] = []
225226
let spawn: ReturnType<typeof spyOn> | undefined
226227
let hold: Promise<void> | undefined
227228
const on = (status = 0, stdout = "", stderr = "") => {
@@ -230,14 +231,15 @@ describe("keychain", () => {
230231
delete process.env.ALTIMATE_INTEGRATIONS_DIR
231232
Object.defineProperty(process, "platform", { value: "darwin" })
232233
calls.length = 0
234+
kills.length = 0
233235
spawn = spyOn(childProcess, "spawn").mockImplementation(((cmd: string, args: string[]) => {
234236
const child: any = new EventEmitter()
235237
const chunks: string[] = []
236238
child.stdout = new PassThrough()
237239
child.stderr = new PassThrough()
238240
child.stdin = new PassThrough()
239241
child.stdin.on("data", (d: Buffer) => chunks.push(d.toString()))
240-
child.kill = () => true
242+
child.kill = (signal: string) => kills.push(signal) > 0
241243
const entry: { cmd: string; args: string[]; input?: string } = { cmd, args }
242244
calls.push(entry)
243245
const finish = () => {
@@ -346,6 +348,7 @@ describe("keychain", () => {
346348
await expect(core.readSecret("cred:github:github_token")).rejects.toThrow("timed out")
347349
await expect(core.storeSecret("cred:github:github_token", "x")).rejects.toThrow("Could not save the credential to the macOS keychain")
348350
expect(calls).toHaveLength(2)
351+
expect(kills).toEqual(["SIGKILL", "SIGKILL"])
349352
} finally {
350353
core.keychainLimits.timeoutMs = limit
351354
}
@@ -578,7 +581,7 @@ describe("workspace connections", () => {
578581
expect((await core.syncProvider("github", undefined, 0, true)).result).toBe("kept_local")
579582
})
580583

581-
test("a saved credential that cannot be read fails that provider's sync with the reason, instead of stopping the run", async () => {
584+
test("a saved credential whose config cannot be collected fails that provider's sync with the reason, instead of stopping the run", async () => {
582585
await connected("github", null, { variant: "token", values: {}, secretFields: ["github_token"] })
583586
const unreadable = "Could not read a stored credential from the macOS keychain (exit 36). Unlock the keychain and try again."
584587
const catalog = spyOn(creds, "loadCatalog").mockRejectedValue(new Error(unreadable))
@@ -604,6 +607,7 @@ describe("workspace connections", () => {
604607
expect(update).not.toHaveBeenCalled()
605608
const shared = await core.syncProvider("github", undefined, 0, true, true)
606609
expect([shared.result, shared.shared]).toEqual(["synced", true])
610+
expect([syncedTo(shared), syncedTo(refused)]).toEqual(["synced to workspace, shared with its members", "synced to workspace (private)"])
607611
expect(update).toHaveBeenCalledTimes(1)
608612
} finally {
609613
for (const sp of [...spies, update]) sp.mockRestore()
@@ -2637,6 +2641,9 @@ describe("attach keeps another connection", () => {
26372641
getDatamate.mockResolvedValue({ id: "5", name: "WS", is_owner: true, privacy: "public", integrations: [{ id: "jira", tools: [], connection_id: "3" }] } as any)
26382642
expect(await core.attachToBoundWorkspace(cfg, "jira", 9, [9, 3], true)).toEqual({ attached: "WS", workspaceId: "5", kept: true })
26392643
expect(update).toHaveBeenCalledTimes(2)
2644+
// Without --share that kept entry is not refused either: the write changes nothing members can use.
2645+
expect(await core.attachToBoundWorkspace(cfg, "jira", 9, [9, 3])).toEqual({ attached: "WS", workspaceId: "5", kept: true })
2646+
expect(update).toHaveBeenCalledTimes(3)
26402647
getDatamate.mockResolvedValue({ id: "5", name: "WS", is_owner: true, privacy: "private", integrations: [{ id: "jira", tools: [], connection_id: null }] } as any)
26412648
expect(await core.attachToBoundWorkspace(cfg, "jira", 9)).toEqual({ attached: "WS", workspaceId: "5" })
26422649
update.mockClear()

0 commit comments

Comments
 (0)