SleepMode_toggle / enter(false). Auto: when ConsciousnessFinCyclesTotal > 100, SleepMode_tryAutoAfterFinCycles triggers SleepMode_enterAuto → enter(true) (the button hint marks auto-entry). After entering — update vitals, contexts, stimuli, and SleepDreams_onSleepEntered.
SleepMode_refreshStimulusButtonVisuals on every pulse and on enter/exit: not-allowed buttons are faded and non-clickable. Only pairs from the whitelist are enabled (in code: “Punishment” motivation; negative: Howl, Siren, Growl, Fire). Clicking an allowed stimulus can exit sleep.
EpisodicMemory_isEmptyFrameForSleep: there is a stimulus and an action-image (OD); there is no response stimulus (responseStimulusImageId) and no numeric effect. Indices of such frames are collected by EpisodicMemory_collectEmptyFrameIndicesNewestFirst — a dreams queue (newest → oldest).
puls.js, the dream machine SleepDreams_onPulse is called before dispetchConsciousnessThinking, so that in the same pulse, after onPulse, the passive sleep main cycle and the info_fantasizming steps run.
sleep_dream_process: for a frame taken from the queue, the old main cycle is pushed onto the interruption stack; a new cycle is created with thinkingMode: 'passive' and context sleepDream. For the passive cycle, levels 3–4 do not run — only info_fantasizming (an associative chain from EM, fantasizmArr, fantazmingPlot; the main cycle writes the story into the info-picture). After the per-frame pulse limit — reconstruct the frame into Episodes; after the per-session frame limit — SleepMode_exit(true) and remove background FO cycles.
Threshold details: MAX_DREAM_FRAMES, MAX_PULSES_PER_FANTASY in sleep_dream_process.js. While the dream fantasy runs, SleepDreams_paleActivityBeast softly highlights the “Beast Activity” block.