From 962457d7fa7d342d282a64fc68ea4cd8755743fc Mon Sep 17 00:00:00 2001 From: Seth Troisi Date: Mon, 23 Nov 2026 18:33:38 -0800 Subject: [PATCH] fix(session): exit 1 when colab stop session does not exist Delegate missing-session handling in stop() to State.get_session(name) instead of passing ignore_missing_session=True and returning 0. This aligns colab stop -s with all other session-scoped subcommands while keeping colab stop --all exiting 0 when no sessions are active. See: https://github.com/googlecolab/google-colab-cli/discussions/251 --- docs/01_session_management.md | 3 +++ src/colab_cli/commands/session.py | 5 +---- tests/test_cli.py | 36 +++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/docs/01_session_management.md b/docs/01_session_management.md index e82756f..9ae719d 100644 --- a/docs/01_session_management.md +++ b/docs/01_session_management.md @@ -1,5 +1,6 @@ --- log: +2026-11-23: `colab stop -s ` now exits 1 when the named session does not exist (or has already vanished from the backend), delegating missing-session handling to `State.get_session` (#251). 2026-11-13: Documented `--auth {oauth2,adc}` (default `oauth2`) and required ADC `gcloud` scopes under Global Flags. 2026-11-04: `--gpu` and `--tpu` now reject unknown values instead of silently selecting `A100` (GPU) or `v6e1` (TPU). `resolve_runtime_options()` uses the new `GPU_ACCELERATORS` / `TPU_ACCELERATORS` maps and raises a `typer.BadParameter` that names the flag and the supported list; `--help` is generated from the same maps so it cannot drift. Validation runs before any assignment request, in both `colab new` and `colab run`. 2026-11-02: Added `_check_linux_keyring_locked` in `auth.py` to check if the active Linux `SecretService` keyring collection is locked via `secretstorage` before reading or writing OAuth tokens, logging and printing an immediate warning with the collection label, timeout, and `--auth=adc` hint while still attempting the keyring operation. @@ -117,6 +118,7 @@ end to end; mocked timeouts alone do not establish backend compatibility. - `authuser`: 0. - ``: The unique session identifier returned during assignment (e.g., `tpu-v5e1-s-kkb-...`). - **Cleanup**: Remove the session from the local state file upon successful 204 response. +- **Missing sessions**: Stopping a specific session (`colab stop` or `colab stop -s `) exits with code 1 if no matching active session exists. `colab stop --all` exits 0 when no sessions are active. ### 4. Session Listing (`colab sessions`) - **API**: `GET https://colab.research.google.com/tun/m/assignments` (based on `colab-agent` implementation). @@ -154,6 +156,7 @@ TDD is mandatory for all session management features. ### 1. Mock Assignment API - **Test Case**: Verify `colab new` correctly parses a `PostAssignmentResponse` and stores it in the local `StateStore`. - **Test Case**: Verify `colab stop` sends a `POST` request with the correct XSRF token to the unassign endpoint. +- **Test Case**: Verify `colab stop -s ` prints a not-found error and exits with status 1 without calling unassign. - **Test Case**: Verify that the path provided via `-c` is correctly passed to the authentication flow. - **Mocking**: Use `unittest.mock` to intercept `requests.Session.request` and return simulated XSSI-prefixed JSON payloads matching the HAR traces. diff --git a/src/colab_cli/commands/session.py b/src/colab_cli/commands/session.py index 2ee50b8..81ded12 100644 --- a/src/colab_cli/commands/session.py +++ b/src/colab_cli/commands/session.py @@ -383,10 +383,7 @@ def stop( return name = state.resolve_session(session) - s = state.get_session(name, ignore_missing_session=True) - if not s: - typer.echo(f"[colab] Session '{name}' not found.") - return + s = state.get_session(name) _stopOne(name, s) typer.echo("[colab] Session terminated.") diff --git a/tests/test_cli.py b/tests/test_cli.py index f08fdc1..508d372 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -345,6 +345,42 @@ def test_cli_stop(mock_client, mock_store, mock_common_state): mock_store.remove.assert_called_with("s1") +def test_cli_stop_missing_session(mock_client, mock_store, mock_common_state): + mock_store.get.return_value = None + mock_common_state.resolve_session.return_value = "no-such-session" + + result = runner.invoke(app, ["stop", "-s", "no-such-session"]) + + assert result.exit_code == 1 + assert "[colab] Session 'no-such-session' not found." in result.output + mock_client.unassign.assert_not_called() + mock_store.remove.assert_not_called() + + +def test_cli_stop_pruned_session_exits_nonzero(tmp_path, mocker): + from colab_cli.common import State + from colab_cli.state import SessionState, StateStore + + config_file = str(tmp_path / "sessions.json") + real_state = State() + real_state._client = MagicMock() + real_state._client.list_assignments.return_value = [] + real_state._history = MagicMock() + StateStore(config_file).add( + SessionState(name="stale", token="tok", url="https://url", endpoint="ep1") + ) + mocker.patch("colab_cli.common.state", real_state) + mocker.patch("colab_cli.cli.state", real_state) + mocker.patch("colab_cli.auto_update.run_background_check") + + result = runner.invoke(app, ["--config", config_file, "stop", "-s", "stale"]) + + assert result.exit_code == 1 + assert "[colab] Session 'stale' not found." in result.output + assert StateStore(config_file).get("stale") is None + real_state.client.unassign.assert_not_called() + + def test_cli_stop_all(mock_client, mock_store, mock_common_state): sessions = {"s1": MagicMock(endpoint="e1"), "s2": MagicMock(endpoint="e2")} mock_common_state.sync_sessions.return_value = (sessions, [])