Skip to content

Centralize EEGPrepSession workspace state updates #179

Closed

Description

@suraj-ranganath

🤖 Some GUI and console paths write EEGPrepSession fields directly instead of using session-owned transitions. That skips validation, history propagation, saved flags, CURRENTSTUDY behavior, and storedisk handling.

Relevant code:

  • src/eegprep/functions/guifunc/menu_actions.py:1048 - extension dataset state direct write
  • src/eegprep/functions/guifunc/menu_actions.py:1580 - pop_newset commit direct write
  • src/eegprep/functions/adminfunc/console.py:359 - console ALLEEG direct write
  • src/eegprep/functions/guifunc/session.py:175 - canonical store_current path

Acceptance criteria:

  • Dataset, study, and CURRENTSET workspace state is applied through one session-owned helper or existing session helpers.
  • GUI and console paths preserve history and notify exactly once.
  • Tests cover dataset-state results and stored/session synchronization.

Activity

  1. added
    bug
    on Jun 10, 2026
  2. claude commented on Jun 10, 2026

    @claude
    Contributor

    🤖 Plan:

    • Read AGENTS.md, fix-issue, pull-request skills
    • Duplicate-work preflight (no overlapping PR; related: Make history script workspace synchronization atomic #180 history sync)
    • Inventory all direct EEGPrepSession field writes
    • Post Research + Proposed approach (multi-module refactor → no auto-PR)

    Scope is multi-module (>100 lines once helper + 13 call-site rewrites + tests land), so per the agent workflow I am posting analysis for human review before implementation.

    Research

    The canonical commit path is EEGPrepSession.store_current (session.py#L174-L213) — it routes through eeg_store, normalizes CURRENTSET, appends per-dataset EEG.history, optionally marks saved="yes", appends ALLCOM/LASTCOM once, and fires notify_changed once. Every direct self.session.ALLEEG = … / self.session.EEG = … / self.session.CURRENTSET = … triple bypass skips: eeg_store validation/dataset-shape checks (dict vs list of dicts), _append_current_dataset_history per-dataset EEG.history updates, mark_saved propagation, and CURRENTSET normalization (the GUI sites use a local _currentset_list helper that does not de-duplicate or validate 1-based indices the way normalize_dataset_indices does). Direct STUDY writes also skip set_study's CURRENTSTUDY coupling and history wiring.

    Direct-write sites (all bypass session helpers):

    Proposed approach

    1. Extend EEGPrepSession with two session-owned helpers. Keep store_current / retrieve / set_study / delete_current as-is; add the missing primitive that the bypass sites actually need:

    def apply_dataset_state(
        self,
        alleeg: list[dict[str, Any]],
        eeg: dict[str, Any] | list[dict[str, Any]],
        currentset: int | list[int] | None,
        *,
        command: str = "",
        mark_saved: bool = False,
    ) -> None:
        """Replace ALLEEG/EEG/CURRENTSET atomically with one history + notify."""
        self.ALLEEG = list(alleeg)
        self.EEG = eeg
        self.CURRENTSET = normalize_dataset_indices(currentset, allow_empty=True)
        self._append_current_dataset_history(command)
        if mark_saved:
            self.mark_current_saved()
        self.add_history(command, notify=False)
        self.notify_changed()
    
    def clear_current_selection(self) -> None:
        """CURRENTSET = [] without losing ALLEEG; notify once."""
        if not self.CURRENTSET:
            return
        self.CURRENTSET = []
        self.EEG = eeg_emptyset()
        self.notify_changed()

    apply_dataset_state is the bulk-replace primitive — used when an external producer (extension, pop_newset commit, pop_copyset, console pop_* accept) returns a full (ALLEEG, EEG, CURRENTSET) tuple that should overwrite the session in one shot. It uses normalize_dataset_indices (not GUI's local _currentset_list), so console and GUI converge on the same validation.

    2. Extend set_study to accept an explicit CURRENTSTUDY override so console's CURRENTSTUDY = 0/1 assignment and _retrieve_dataset's CURRENTSTUDY = 0 no longer need direct field writes:

    def set_study(self, study, alleeg=None, *, command="", currentstudy=None):
        self.STUDY = study
        self.CURRENTSTUDY = currentstudy if currentstudy is not None else (1 if study else 0)
        …

    3. Replace every direct-write site with apply_dataset_state / set_study / clear_current_selection. Net per-site delta: 4–5 lines → 1 call. The _commit_processed_dataset_from_gui site additionally drops its duplicate add_history(command) (command was already appended before the pop_newset call) by passing command=newset_command to apply_dataset_state once.

    4. console.after_execute keeps its per-field detection, but when all of ALLEEG/EEG/CURRENTSET targets are present together (the common case for replayed [ALLEEG EEG CURRENTSET] = pop_newset(…) history), route through apply_dataset_state instead of three independent writes + a separate retrieve call.

    5. Tests (extend tests/test_session_contracts.py and tests/test_console_workspace.py):

    • apply_dataset_state rejects mismatched CURRENTSET / EEG-list lengths, normalizes scalar vs list CURRENTSET, fires notify_changed exactly once (counter on a registered listener), appends LASTCOM/ALLCOM exactly once.
    • GUI: _apply_extension_result, _commit_processed_dataset_from_gui, _copy_current_dataset each fire one notify + one history entry and produce a console namespace synced via assert_session_synced.
    • Console: replayed [ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, CURRENTSET, "retrieve", 1) produces the same end state as the GUI _retrieve_dataset path.
    • Saved-flag propagation: a triple write through apply_dataset_state(..., mark_saved=True) sets EEG["saved"] = "yes" and ALLEEG[i-1]["saved"] = "yes" for every entry in CURRENTSET.

    Open questions for reviewer

    1. Helper name: apply_dataset_state vs replace_dataset_state vs broadening store_current with a replace_alleeg=True flag. I lean toward a new method because store_current currently adds/updates one selected dataset in-place via eeg_store, whereas the bypass sites replace the entire ALLEEG with a producer-supplied list. Conflating the two would muddy store_current.
    2. Should apply_dataset_state also accept a mark_saved default of True for the _commit_processed_dataset_from_gui path (post-pop_newset "overwrite" semantics)? EEGLAB marks the committed dataset saved after overwrite; current GUI code does not.
    3. pop_runscript namespace re-import touches STUDY and dataset state together. Acceptable to route through apply_dataset_state + set_study back-to-back (two notifies), or do you want a combined apply_workspace_state(eeg=…, alleeg=…, currentset=…, study=…, currentstudy=…) that fires one notify?
    4. visual_capture.py:380 direct STUDY write is purely a fixture-builder. Leave or fold in?

    Once these are settled I will implement on agent/20260610-fix-179.

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

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions