Skip to content

LocalStorage#178

Closed
BayerC wants to merge 1 commit into
mainfrom
poc/session_storage_key
Closed

LocalStorage#178
BayerC wants to merge 1 commit into
mainfrom
poc/session_storage_key

Conversation

@BayerC

@BayerC BayerC commented May 29, 2026

Copy link
Copy Markdown
Owner

new test

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • Consider passing a LocalStorage instance into load_or_create_session_id (or SessionState) rather than constructing it inside the function to make it easier to swap implementations or use alternative storage strategies without monkeypatching.
  • The fake_browser_session_id fixture is autouse and globally patches load_or_create_session_id; if any tests need to exercise the real browser-backed behavior (e.g., integration tests), you may want to narrow its scope or provide an opt-out mechanism.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider passing a `LocalStorage` instance into `load_or_create_session_id` (or `SessionState`) rather than constructing it inside the function to make it easier to swap implementations or use alternative storage strategies without monkeypatching.
- The `fake_browser_session_id` fixture is `autouse` and globally patches `load_or_create_session_id`; if any tests need to exercise the real browser-backed behavior (e.g., integration tests), you may want to narrow its scope or provide an opt-out mechanism.

## Individual Comments

### Comment 1
<location path="src/open_cups/session_state.py" line_range="22-23" />
<code_context>
-            existing = st.query_params.get("session_id")
-            st.session_state.session_id = existing or str(uuid.uuid4())
-        st.query_params["session_id"] = st.session_state.session_id
+        if SESSION_ID_STORAGE_KEY not in st.session_state:
+            st.session_state.session_id = load_or_create_session_id()

     @property
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Use the shared key constant consistently for `st.session_state` to avoid divergence.

You’re checking for `SESSION_ID_STORAGE_KEY` in `st.session_state` but assigning via the `session_id` attribute. If the constant changes, the check and assignment will diverge. Please write to `st.session_state[SESSION_ID_STORAGE_KEY] = load_or_create_session_id()` and have the `session_id` property read from that key so there’s a single source of truth.

Suggested implementation:

```python
    def __init__(self) -> None:
        if SESSION_ID_STORAGE_KEY not in st.session_state:
            st.session_state[SESSION_ID_STORAGE_KEY] = load_or_create_session_id()

    @property
    def session_id(self) -> str:
        return str(st.session_state[SESSION_ID_STORAGE_KEY])

```

You should also search the rest of the codebase for any direct uses of `st.session_state.session_id` and update them to use `st.session_state[SESSION_ID_STORAGE_KEY]` or this `session_id` property, to ensure everything consistently uses the shared key constant.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +22 to +23
if SESSION_ID_STORAGE_KEY not in st.session_state:
st.session_state.session_id = load_or_create_session_id()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion (bug_risk): Use the shared key constant consistently for st.session_state to avoid divergence.

You’re checking for SESSION_ID_STORAGE_KEY in st.session_state but assigning via the session_id attribute. If the constant changes, the check and assignment will diverge. Please write to st.session_state[SESSION_ID_STORAGE_KEY] = load_or_create_session_id() and have the session_id property read from that key so there’s a single source of truth.

Suggested implementation:

    def __init__(self) -> None:
        if SESSION_ID_STORAGE_KEY not in st.session_state:
            st.session_state[SESSION_ID_STORAGE_KEY] = load_or_create_session_id()

    @property
    def session_id(self) -> str:
        return str(st.session_state[SESSION_ID_STORAGE_KEY])

You should also search the rest of the codebase for any direct uses of st.session_state.session_id and update them to use st.session_state[SESSION_ID_STORAGE_KEY] or this session_id property, to ensure everything consistently uses the shared key constant.

@BayerC

BayerC commented May 29, 2026

Copy link
Copy Markdown
Owner Author

still doenst work!

@BayerC BayerC closed this May 31, 2026
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.

1 participant