Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/01_session_management.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
log:
2026-11-23: `colab stop -s <name>` 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.
Expand Down Expand Up @@ -117,6 +118,7 @@ end to end; mocked timeouts alone do not establish backend compatibility.
- `authuser`: 0.
- `<endpoint>`: 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 <name>`) 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).
Expand Down Expand Up @@ -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 <missing>` 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.

Expand Down
5 changes: 1 addition & 4 deletions src/colab_cli/commands/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down
36 changes: 36 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, [])
Expand Down