-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix(status): surface Telegram 409 conflicts by passing sandbox name via --name flag (Fixes #2018) #2034
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
Closed
sanketsh4h
wants to merge
5
commits into
NVIDIA:main
from
sanketsh4h:fix/2018-messaging-bridge-health-exec-args
+175
−22
Closed
fix(status): surface Telegram 409 conflicts by passing sandbox name via --name flag (Fixes #2018) #2034
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f17bd33
fix(status): surface Telegram 409 conflicts by passing sandbox name v…
sanketsh4h 202b1d1
fix(status): address CodeRabbit review — fix readGatewayLog argv and …
sanketsh4h 9088b02
fix(nemoclaw): guard readGatewayLog against spawn failures
sanketsh4h 8b754e1
refactor(test): collapse spawnSync mocks into a helper
sanketsh4h 3aeb8e6
refactor(types): reuse MessagingBridgeHealth instead of duplicate Bri…
sanketsh4h 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| vi.mock("./resolve-openshell.js", () => ({ | ||
| resolveOpenshell: vi.fn(() => "/usr/local/bin/openshell"), | ||
| })); | ||
|
|
||
| vi.mock("node:child_process", async (importOriginal) => { | ||
| const actual = await importOriginal<typeof import("node:child_process")>(); | ||
| return { ...actual, spawnSync: vi.fn() }; | ||
| }); | ||
|
|
||
| import { checkMessagingBridgeHealth } from "./messaging-bridge-health.js"; | ||
| import { spawnSync } from "node:child_process"; | ||
| import { resolveOpenshell } from "./resolve-openshell.js"; | ||
|
|
||
| const spawnSyncMock = vi.mocked(spawnSync); | ||
| const resolveOpenshellMock = vi.mocked(resolveOpenshell); | ||
|
|
||
| type SpawnResult = ReturnType<typeof spawnSync>; | ||
| function mockSpawn(overrides: Partial<SpawnResult> = {}): void { | ||
| spawnSyncMock.mockReturnValue({ | ||
| pid: 0, | ||
| output: [], | ||
| stdout: "", | ||
| stderr: "", | ||
| status: 0, | ||
| signal: null, | ||
| ...overrides, | ||
| } as unknown as SpawnResult); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| resolveOpenshellMock.mockReturnValue("/usr/local/bin/openshell"); | ||
| }); | ||
|
|
||
| describe("checkMessagingBridgeHealth", () => { | ||
| it("returns empty and does not spawn when channels does not include telegram", () => { | ||
| const result = checkMessagingBridgeHealth("alpha", ["discord", "slack"]); | ||
| expect(result).toEqual([]); | ||
| expect(spawnSyncMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns empty and does not spawn when channels is null/undefined", () => { | ||
| expect(checkMessagingBridgeHealth("alpha", null)).toEqual([]); | ||
| expect(checkMessagingBridgeHealth("alpha", undefined)).toEqual([]); | ||
| expect(spawnSyncMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns empty when openshell binary cannot be resolved", () => { | ||
| resolveOpenshellMock.mockReturnValue(null); | ||
| const result = checkMessagingBridgeHealth("alpha", ["telegram"]); | ||
| expect(result).toEqual([]); | ||
| expect(spawnSyncMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns a conflict entry when gateway log reports N conflicts", () => { | ||
| mockSpawn({ stdout: "32\n" }); | ||
| expect(checkMessagingBridgeHealth("alpha", ["telegram"])).toEqual([ | ||
| { channel: "telegram", conflicts: 32 }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("returns empty when the conflict count is zero", () => { | ||
| mockSpawn({ stdout: "0\n" }); | ||
| expect(checkMessagingBridgeHealth("alpha", ["telegram"])).toEqual([]); | ||
| }); | ||
|
|
||
| it("returns empty when the count is non-numeric", () => { | ||
| mockSpawn({ stdout: "\n" }); | ||
| expect(checkMessagingBridgeHealth("alpha", ["telegram"])).toEqual([]); | ||
| }); | ||
|
|
||
| it("returns empty when spawnSync throws", () => { | ||
| spawnSyncMock.mockImplementation(() => { | ||
| throw new Error("spawn EPIPE"); | ||
| }); | ||
| expect(checkMessagingBridgeHealth("alpha", ["telegram"])).toEqual([]); | ||
| }); | ||
|
|
||
| it("returns empty when spawnSync reports a non-zero exit status (exec failed)", () => { | ||
| mockSpawn({ stderr: "/bin/bash: alpha: command not found\n", status: 127 }); | ||
| expect(checkMessagingBridgeHealth("alpha", ["telegram"])).toEqual([]); | ||
| }); | ||
|
|
||
| it("returns empty when spawnSync returns an error object (e.g. timeout)", () => { | ||
| mockSpawn({ | ||
| stdout: null as unknown as string, | ||
| stderr: null as unknown as string, | ||
| status: null, | ||
| signal: "SIGTERM", | ||
| error: new Error("spawnSync timed out"), | ||
| }); | ||
| expect(checkMessagingBridgeHealth("alpha", ["telegram"])).toEqual([]); | ||
| }); | ||
|
|
||
| // Regression for #2018: `openshell sandbox exec` requires the --name/-n | ||
| // flag. Passing the sandbox name as a positional causes the name to be | ||
| // interpreted as the first word of the command and fails with exit 127. | ||
| describe("argv shape (#2018 regression)", () => { | ||
| beforeEach(() => mockSpawn({ stdout: "5\n" })); | ||
|
|
||
| it("passes the sandbox name via --name/-n, not as a positional", () => { | ||
| checkMessagingBridgeHealth("tele-brev", ["telegram"]); | ||
|
|
||
| expect(spawnSyncMock).toHaveBeenCalledTimes(1); | ||
| const [binary, args] = spawnSyncMock.mock.calls[0]; | ||
| expect(binary).toBe("/usr/local/bin/openshell"); | ||
|
|
||
| const argsArray = args as string[]; | ||
| const nameFlagIdx = argsArray.indexOf("-n"); | ||
| expect(nameFlagIdx).toBeGreaterThan(-1); | ||
| expect(argsArray[nameFlagIdx + 1]).toBe("tele-brev"); | ||
|
|
||
| // Pre-fix, args were ["sandbox", "exec", "<name>", "sh", "-c", <script>]. | ||
| // Ensure the sandbox name is not directly adjacent to "exec" (positional). | ||
| const execIdx = argsArray.indexOf("exec"); | ||
| expect(argsArray[execIdx + 1]).not.toBe("tele-brev"); | ||
| }); | ||
|
|
||
| it("separates the command from flags with `--`", () => { | ||
| checkMessagingBridgeHealth("tele-brev", ["telegram"]); | ||
| const [, args] = spawnSyncMock.mock.calls[0]; | ||
| const argsArray = args as string[]; | ||
| const sepIdx = argsArray.indexOf("--"); | ||
| expect(sepIdx).toBeGreaterThan(-1); | ||
| expect(argsArray[sepIdx + 1]).toBe("sh"); | ||
| expect(argsArray[sepIdx + 2]).toBe("-c"); | ||
| }); | ||
| }); | ||
| }); |
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,42 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { spawnSync } from "node:child_process"; | ||
| import { resolveOpenshell } from "./resolve-openshell.js"; | ||
|
|
||
| export interface BridgeConflict { | ||
| channel: string; | ||
| conflicts: number; | ||
| } | ||
|
|
||
| const CONFLICT_SCRIPT = | ||
| 'tail -n 200 /tmp/gateway.log 2>/dev/null | grep -cE "getUpdates conflict|409[[:space:]:]+Conflict" || true'; | ||
|
|
||
| export function checkMessagingBridgeHealth( | ||
| sandboxName: string, | ||
| channels: readonly string[] | null | undefined, | ||
| ): BridgeConflict[] { | ||
| if (!Array.isArray(channels) || !channels.includes("telegram")) return []; | ||
|
|
||
| const binary = resolveOpenshell(); | ||
| if (!binary) return []; | ||
|
|
||
| // `openshell sandbox exec` requires --name/-n for the sandbox name; a | ||
| // positional there gets parsed as the first word of the command and | ||
| // fails with exit 127 (#2018). | ||
| const args = ["sandbox", "exec", "-n", sandboxName, "--", "sh", "-c", CONFLICT_SCRIPT]; | ||
|
|
||
| try { | ||
| const result = spawnSync(binary, args, { | ||
| encoding: "utf-8", | ||
| timeout: 3000, | ||
| stdio: ["ignore", "pipe", "pipe"], | ||
| }); | ||
| if (result.error || result.status !== 0) return []; | ||
| const count = Number.parseInt((result.stdout || "").trim(), 10); | ||
| if (!Number.isFinite(count) || count === 0) return []; | ||
| return [{ channel: "telegram", conflicts: count }]; | ||
| } catch { | ||
| return []; | ||
| } | ||
| } | ||
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
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.