Skip to content

feat(sessions): track attached sessions in sessions.json - #230

Open
wh1ter4bb1t-js wants to merge 7 commits into
mainfrom
pm/1029-1807/g17-ssh-attach.pr1
Open

wh1ter4bb1t-js wants to merge 7 commits into
mainfrom
pm/1029-1807/g17-ssh-attach.pr1

Conversation

@wh1ter4bb1t-js

@wh1ter4bb1t-js wh1ter4bb1t-js commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

why? colab ssh --attach marks external runtimes SessionState.attached so Client.unassign refuses them, stop/ssh --rm remove them locally, and run cleanup skips them; config paths derive from State.settings_store.path. _LockedFileStore.__init__ calls _ensure_dir() before opening filelock.ReadWriteLock, fixing store init for nonexistent nested --config directories (#271).

Tested: pytest (607 passed, 19 skipped at d12b562), ruff check, and ruff format --check (on changed files) pass.

Comment thread docs/01_session_management.md Outdated
- **Global Flags**:
- `-c`, `--client-oauth-config`: Path to the client secrets JSON file (default: `~/.colab-cli-oauth-config.json`).
- `--config`: Path to the session state JSON file (default: `~/.config/colab-cli/sessions.json`).
- `--config`: Path to the session state JSON file (default: `~/.config/colab-cli/sessions.json`). Attached sessions and their tokens are kept beside it in `<stem>.attached.json`, where `<stem>` is the path without its extension; a path ending in `.attached.json` is refused.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop ", where ... is refused."

maybe change to or

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, moved attached sessions into sessions.json so .attached.json and the <stem> path rules are gone from docs/01_session_management.md.

Comment thread src/colab_cli/commands/session.py Outdated
Comment on lines +368 to +369
f"[colab] Forgot the attached record for '{name}';"
" its runtime keeps running."

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't very clear. What about "[colab] {name!r} wasn't created by the cli and isn't stopped, only removed from sessions list"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. colab stop (including --all) and colab ssh --rm both print that message now via attached_not_stopped_message.

Comment thread src/colab_cli/state.py Outdated
another store's sessions file, or the reverse.
"""
if not path:
path = os.path.expanduser("~/.config/colab-cli/sessions.json")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe get this path from State.state.settings_store.path

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. State.store now derives the default sessions.json path from State.settings_store.path, and SettingsStore and StateStore share _DEFAULT_CONFIG_DIR in state.py so StateStore() still works standalone.

Comment thread src/colab_cli/state.py Outdated
Comment on lines +166 to +167
resolved = os.path.realpath(attached_path).lower()
if not resolved.endswith(_ATTACHED_SUFFIX):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you care if it hard links to another file?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, that complexity isn't needed anymore. Dropped the .attached.json path and link checks completely now that everything lives in sessions.json.

Comment thread src/colab_cli/state.py Outdated
class StateStore(_LockedFileStore):
"""Session records: CLI-created ones in `path`, attached ones beside it.

Attached records live in their own file so that older CLI versions, which

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really a problem? they would have to have the newer version of the CLI to run attach so only becomes a problem if they downgrade?

It feels nicer to have all the sessions in one file.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, one file is much cleaner. Switched to a single sessions.json with SessionState.attached: bool = False, dropped sessions.attached.json and the duplicate-session handling, and moved the Client.unassign, colab stop / colab ssh --rm, and colab run teardown guards into this PR.

Drake Aiman added 6 commits November 19, 2026 19:56
Add `SessionState.attached` (default `False`) and
`StateStore.is_attached_endpoint` so attached runtimes are stored in
`sessions.json` instead of a second file. Derive the default
`sessions.json` and `colab.log` directory from `SettingsStore.path`, and
ensure the store directory exists before opening its `ReadWriteLock`.
Remove attached sessions (and records sharing an attached endpoint) from
`sessions.json` without stopping their kernel or unassigning their
runtime, printing that the session was not created by the CLI and was
only removed from the sessions list. Check the local record before
`State.get_session` so removing an attached session makes no HTTP calls.
Raise `AttachedRuntimeError` in `Client.unassign` when `is_attached`
reports an attached endpoint, wired from `StateStore.is_attached_endpoint`
in `State.client`. Guard `colab ssh --rm` and `colab run` teardown
against stopping or removing attached runtimes, and keep the session
record with a `colab stop -s` retry hint when `unassign` fails.
Collect attached endpoints before removing records in colab stop --all so a shared CLI record cannot unassign an attached runtime after the attached record is removed. Also use store keys in sessions and status output, read the active store dynamically in State.client, and add missing docstrings.
Exercise public CLI invocations via CliRunner in ssh --rm and run teardown tests instead of calling private helpers directly, wrap new changelog and session management doc lines to 80 columns, and update run command teardown docs.
Extract stop_session in commands/session.py and call it from ssh._stop_session so attached and shared-endpoint teardown logic is not duplicated. Shorten _stopOne's docstring summary line and remove internal symbol names from the sessions CHANGELOG entry.
@wh1ter4bb1t-js
wh1ter4bb1t-js force-pushed the pm/1029-1807/g17-ssh-attach.pr1 branch from c53e747 to 17e13c3 Compare November 23, 2026 02:10
@wh1ter4bb1t-js wh1ter4bb1t-js changed the title feat(state): keep attached sessions in their own store file feat(sessions): track attached sessions in sessions.json Nov 23, 2026
@mzyas

mzyas commented Nov 23, 2026 •

Copy link
Copy Markdown

Small note on the state.py change: moving self._ensure_dir() above filelock.ReadWriteLock(...) in _LockedFileStore.__init__ fixes a real failure that I think deserves a test.

ReadWriteLock.__init__ opens the lock database immediately. So with --config pointing into a directory that does not exist yet, the constructor itself raises (FileNotFoundError on filelock 4.x, sqlite3.OperationalError on 3.29-3.32) before _ensure_dir() has had a chance to run. main still has the old order, so a fresh --config /new/dir/sessions.json fails there.

Could you add a regression test for it, for example StateStore(str(tmp_path / "new" / "dir" / "sessions.json")) and the same for SettingsStore, and mention it in the PR description or changelog? The new test_default_store_paths_share_config_dir only checks the default paths, so nothing currently stops a later refactor from moving the two lines back.

I've also posted this as a discussion with a minimal repro, so it can be tracked apart from this feature: #271

@wh1ter4bb1t-js

Copy link
Copy Markdown
Member Author

Small note on the state.py change: moving self._ensure_dir() above filelock.ReadWriteLock(...) in _LockedFileStore.__init__ fixes a real failure that I think deserves a test.

Thanks for catching that, @mzyas. Added test_state_store_creates_missing_parent_dirs and test_settings_store_creates_missing_parent_dirs in tests/test_state.py (d12b562) covering StateStore and SettingsStore on nonexistent nested paths, and noted the _ensure_dir() fix (#271) in the PR description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants