|
| 1 | +/* |
| 2 | + * Copyright 2025-2026 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.alibaba.cloud.ai.graph.agent.tools; |
| 17 | + |
| 18 | +import com.alibaba.cloud.ai.graph.RunnableConfig; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.api.Timeout; |
| 22 | +import org.junit.jupiter.api.condition.DisabledOnOs; |
| 23 | +import org.junit.jupiter.api.condition.OS; |
| 24 | + |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests for {@link ShellSessionManager} command output collection. |
| 34 | + * |
| 35 | + * <p> |
| 36 | + * Reproduces #4740: a command whose output does not end with a newline (e.g. {@code cat} |
| 37 | + * of a file without a trailing newline) caused the completion marker to be merged onto the |
| 38 | + * last output line, so it was never detected and {@code collectOutput} hung until the |
| 39 | + * command timed out. |
| 40 | + */ |
| 41 | +@DisabledOnOs(OS.WINDOWS) |
| 42 | +class ShellSessionManagerTest { |
| 43 | + |
| 44 | + private ShellSessionManager newManager(Path workspace) { |
| 45 | + return ShellSessionManager.builder() |
| 46 | + .workspaceRoot(workspace) |
| 47 | + .commandTimeout(10000) |
| 48 | + .build(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + @Timeout(value = 20, unit = TimeUnit.SECONDS) |
| 53 | + void outputWithoutTrailingNewlineIsCapturedAndDoesNotHang() throws Exception { |
| 54 | + Path workspace = Files.createTempDirectory("shell_session_test_"); |
| 55 | + ShellSessionManager manager = newManager(workspace); |
| 56 | + RunnableConfig config = RunnableConfig.builder().threadId("test-thread").build(); |
| 57 | + manager.initialize(config); |
| 58 | + try { |
| 59 | + // printf without \n leaves no trailing newline, so the marker is merged onto |
| 60 | + // this line. Before the fix this hung until the command timed out. |
| 61 | + ShellSessionManager.CommandResult result = manager |
| 62 | + .executeCommand("printf 'hello-no-newline'", config); |
| 63 | + |
| 64 | + assertFalse(result.isTimedOut(), "command should not time out"); |
| 65 | + assertEquals("hello-no-newline", result.getOutput()); |
| 66 | + assertEquals(0, result.getExitCode()); |
| 67 | + } |
| 68 | + finally { |
| 69 | + manager.cleanup(config); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + @Timeout(value = 20, unit = TimeUnit.SECONDS) |
| 75 | + void exitCodeIsParsedWhenMergedWithOutput() throws Exception { |
| 76 | + Path workspace = Files.createTempDirectory("shell_session_test_"); |
| 77 | + ShellSessionManager manager = newManager(workspace); |
| 78 | + RunnableConfig config = RunnableConfig.builder().threadId("test-thread").build(); |
| 79 | + manager.initialize(config); |
| 80 | + try { |
| 81 | + // Output has no trailing newline AND the command fails: the non-zero exit code |
| 82 | + // must still be parsed from the line the marker was merged onto. |
| 83 | + ShellSessionManager.CommandResult result = manager |
| 84 | + .executeCommand("printf 'oops' && false", config); |
| 85 | + |
| 86 | + assertFalse(result.isTimedOut(), "command should not time out"); |
| 87 | + assertEquals("oops", result.getOutput()); |
| 88 | + assertEquals(1, result.getExitCode()); |
| 89 | + } |
| 90 | + finally { |
| 91 | + manager.cleanup(config); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + @Timeout(value = 20, unit = TimeUnit.SECONDS) |
| 97 | + void normalOutputWithTrailingNewlineStillWorks() throws Exception { |
| 98 | + Path workspace = Files.createTempDirectory("shell_session_test_"); |
| 99 | + ShellSessionManager manager = newManager(workspace); |
| 100 | + RunnableConfig config = RunnableConfig.builder().threadId("test-thread").build(); |
| 101 | + manager.initialize(config); |
| 102 | + try { |
| 103 | + ShellSessionManager.CommandResult result = manager.executeCommand("printf 'a\\nb\\n'", config); |
| 104 | + |
| 105 | + assertFalse(result.isTimedOut(), "command should not time out"); |
| 106 | + assertEquals("a\nb", result.getOutput()); |
| 107 | + assertEquals(0, result.getExitCode()); |
| 108 | + } |
| 109 | + finally { |
| 110 | + manager.cleanup(config); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments