-
Notifications
You must be signed in to change notification settings - Fork 3.1k
fix(onboard): detect messaging bot-token conflicts across sandboxes (#1953) #1979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6d2cede
fix(onboard): detect messaging bot-token conflicts across sandboxes (…
cjagwani c52c65a
refactor: drop duplicate providerExistsInGateway and unused export
cjagwani 5c9374d
fix: address CodeRabbit review on #1979
cjagwani a8d2760
Merge branch 'main' into fix/messaging-bridge-conflict-1953
cjagwani b15d79f
refactor: de-export internal-only interfaces in messaging-conflict
cjagwani 30c77d9
fix: address second CodeRabbit review on #1979
cjagwani cb8cef9
Merge branch 'main' into fix/messaging-bridge-conflict-1953
cjagwani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import type { SandboxEntry } from "./registry"; | ||
| import { | ||
| backfillMessagingChannels, | ||
| findAllOverlaps, | ||
| findChannelConflicts, | ||
| } from "./messaging-conflict"; | ||
|
|
||
| function makeRegistry(sandboxes: SandboxEntry[]) { | ||
| const store = new Map(sandboxes.map((s) => [s.name, { ...s }])); | ||
| return { | ||
| listSandboxes: () => ({ | ||
| sandboxes: Array.from(store.values()), | ||
| defaultSandbox: sandboxes[0]?.name ?? null, | ||
| }), | ||
| updateSandbox: vi.fn((name: string, updates: Partial<SandboxEntry>) => { | ||
| const entry = store.get(name); | ||
| if (!entry) return false; | ||
| Object.assign(entry, updates); | ||
| return true; | ||
| }), | ||
| }; | ||
| } | ||
|
|
||
| describe("findChannelConflicts", () => { | ||
| it("returns conflicts when another sandbox already has the channel", () => { | ||
| const registry = makeRegistry([ | ||
| { name: "alice", messagingChannels: ["telegram"] }, | ||
| { name: "bob", messagingChannels: [] }, | ||
| ]); | ||
| expect(findChannelConflicts("bob", ["telegram"], registry)).toEqual([ | ||
| { channel: "telegram", sandbox: "alice" }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("excludes the current sandbox from its own conflicts", () => { | ||
| const registry = makeRegistry([{ name: "alice", messagingChannels: ["telegram"] }]); | ||
| expect(findChannelConflicts("alice", ["telegram"], registry)).toEqual([]); | ||
| }); | ||
|
|
||
| it("skips entries with no messagingChannels field (pre-backfill)", () => { | ||
| const registry = makeRegistry([{ name: "alice" }, { name: "bob", messagingChannels: [] }]); | ||
| expect(findChannelConflicts("bob", ["telegram"], registry)).toEqual([]); | ||
| }); | ||
|
|
||
| it("returns empty when no channels are enabled", () => { | ||
| const registry = makeRegistry([{ name: "alice", messagingChannels: ["telegram"] }]); | ||
| expect(findChannelConflicts("bob", [], registry)).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("findAllOverlaps", () => { | ||
| it("reports each overlapping pair once", () => { | ||
| const registry = makeRegistry([ | ||
| { name: "alice", messagingChannels: ["telegram"] }, | ||
| { name: "bob", messagingChannels: ["telegram"] }, | ||
| { name: "carol", messagingChannels: ["discord"] }, | ||
| ]); | ||
| expect(findAllOverlaps(registry)).toEqual([ | ||
| { channel: "telegram", sandboxes: ["alice", "bob"] }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("reports all pairs when three sandboxes share a channel", () => { | ||
| const registry = makeRegistry([ | ||
| { name: "a", messagingChannels: ["telegram"] }, | ||
| { name: "b", messagingChannels: ["telegram"] }, | ||
| { name: "c", messagingChannels: ["telegram"] }, | ||
| ]); | ||
| expect(findAllOverlaps(registry)).toEqual([ | ||
| { channel: "telegram", sandboxes: ["a", "b"] }, | ||
| { channel: "telegram", sandboxes: ["a", "c"] }, | ||
| { channel: "telegram", sandboxes: ["b", "c"] }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("returns empty when channels do not overlap", () => { | ||
| const registry = makeRegistry([ | ||
| { name: "alice", messagingChannels: ["telegram"] }, | ||
| { name: "bob", messagingChannels: ["discord"] }, | ||
| ]); | ||
| expect(findAllOverlaps(registry)).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("backfillMessagingChannels", () => { | ||
| it("fills in missing messagingChannels by probing OpenShell", () => { | ||
| const registry = makeRegistry([{ name: "alice" }]); | ||
| const probe = { | ||
| providerExists: vi.fn((name: string) => | ||
| name === "alice-telegram-bridge" ? "present" : "absent", | ||
| ) as (name: string) => "present" | "absent" | "error", | ||
| }; | ||
| backfillMessagingChannels(registry, probe); | ||
| expect(registry.updateSandbox).toHaveBeenCalledWith("alice", { | ||
| messagingChannels: ["telegram"], | ||
| }); | ||
| expect(probe.providerExists).toHaveBeenCalledWith("alice-telegram-bridge"); | ||
| expect(probe.providerExists).toHaveBeenCalledWith("alice-discord-bridge"); | ||
| expect(probe.providerExists).toHaveBeenCalledWith("alice-slack-bridge"); | ||
| }); | ||
|
|
||
| it("leaves entries with existing messagingChannels alone", () => { | ||
| const registry = makeRegistry([ | ||
| { name: "alice", messagingChannels: ["telegram"] }, | ||
| ]); | ||
| const probe = { | ||
| providerExists: vi.fn(() => "present") as (name: string) => "present" | "absent" | "error", | ||
| }; | ||
| backfillMessagingChannels(registry, probe); | ||
| expect(registry.updateSandbox).not.toHaveBeenCalled(); | ||
| expect(probe.providerExists).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("writes an empty array when all probes return absent", () => { | ||
| const registry = makeRegistry([{ name: "alice" }]); | ||
| const probe = { | ||
| providerExists: vi.fn(() => "absent") as (name: string) => "present" | "absent" | "error", | ||
| }; | ||
| backfillMessagingChannels(registry, probe); | ||
| expect(registry.updateSandbox).toHaveBeenCalledWith("alice", { messagingChannels: [] }); | ||
| }); | ||
|
|
||
| it("does NOT persist when a probe returns error (retry on next call)", () => { | ||
| // "error" is distinct from "absent": a transient gateway failure must not | ||
| // be collapsed into "provider not attached" and persisted, because that | ||
| // would prevent all future backfill retries and hide real overlaps. | ||
| const registry = makeRegistry([{ name: "alice" }]); | ||
| const probe = { | ||
| providerExists: vi.fn((name: string) => { | ||
| if (name.endsWith("-telegram-bridge")) return "error"; | ||
| return name.endsWith("-discord-bridge") ? "present" : "absent"; | ||
| }) as (name: string) => "present" | "absent" | "error", | ||
| }; | ||
| backfillMessagingChannels(registry, probe); | ||
| expect(registry.updateSandbox).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("also treats a thrown probe as error (defensive; callers should return 'error' instead)", () => { | ||
| const registry = makeRegistry([{ name: "alice" }]); | ||
| const probe = { | ||
| providerExists: vi.fn(() => { | ||
| throw new Error("unexpected"); | ||
| }) as (name: string) => "present" | "absent" | "error", | ||
| }; | ||
| backfillMessagingChannels(registry, probe); | ||
| expect(registry.updateSandbox).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("re-attempts backfill on a subsequent call after a prior error", () => { | ||
| const registry = makeRegistry([{ name: "alice" }]); | ||
| let firstPass = true; | ||
| const probe = { | ||
| providerExists: vi.fn((name: string) => { | ||
| if (name.endsWith("-telegram-bridge") && firstPass) { | ||
| firstPass = false; | ||
| return "error"; | ||
| } | ||
| return name === "alice-telegram-bridge" ? "present" : "absent"; | ||
| }) as (name: string) => "present" | "absent" | "error", | ||
| }; | ||
| backfillMessagingChannels(registry, probe); | ||
| expect(registry.updateSandbox).not.toHaveBeenCalled(); | ||
| backfillMessagingChannels(registry, probe); | ||
| expect(registry.updateSandbox).toHaveBeenCalledWith("alice", { | ||
| messagingChannels: ["telegram"], | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.