Repository navigation
feat(sessions): track attached sessions in sessions.json - #230
wh1ter4bb1t-js wants to merge 7 commits into
Conversation
| - **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. |
There was a problem hiding this comment.
drop ", where ... is refused."
maybe change to or
There was a problem hiding this comment.
Done, moved attached sessions into sessions.json so .attached.json and the <stem> path rules are gone from docs/01_session_management.md.
| f"[colab] Forgot the attached record for '{name}';" | ||
| " its runtime keeps running." |
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
Done. colab stop (including --all) and colab ssh --rm both print that message now via attached_not_stopped_message.
| another store's sessions file, or the reverse. | ||
| """ | ||
| if not path: | ||
| path = os.path.expanduser("~/.config/colab-cli/sessions.json") |
There was a problem hiding this comment.
Maybe get this path from State.state.settings_store.path
There was a problem hiding this comment.
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.
| resolved = os.path.realpath(attached_path).lower() | ||
| if not resolved.endswith(_ATTACHED_SUFFIX): |
There was a problem hiding this comment.
Why do you care if it hard links to another file?
There was a problem hiding this comment.
Good point, that complexity isn't needed anymore. Dropped the .attached.json path and link checks completely now that everything lives in sessions.json.
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.
c53e747 to
17e13c3
Compare
|
Small note on the
Could you add a regression test for it, for example I've also posted this as a discussion with a minimal repro, so it can be tracked apart from this feature: #271 |
Thanks for catching that, @mzyas. Added |
why?
colab ssh --attachmarks external runtimesSessionState.attachedsoClient.unassignrefuses them,stop/ssh --rmremove them locally, andruncleanup skips them; config paths derive fromState.settings_store.path._LockedFileStore.__init__calls_ensure_dir()before openingfilelock.ReadWriteLock, fixing store init for nonexistent nested--configdirectories (#271).Tested:
pytest(607 passed, 19 skipped atd12b562),ruff check, andruff format --check(on changed files) pass.