# How i-have-adhd Maintains Persistence Across Claude Code Sessions

> Discover how i-have-adhd ensures persistence across Claude code sessions using session-manager and an optional flag file for automatic rule reloading.

- Repository: [Ayoub Ghriss/i-have-adhd](https://github.com/ayghri/i-have-adhd)
- Tags: internals
- Published: 2026-08-08

---

**The i-have-adhd extension maintains persistence across Claude Code sessions by combining session-manager state entries with an optional always-on flag file, ensuring ADHD-friendly rules automatically reload whenever a new session starts.**

The `ayghri/i-have-adhd` repository provides a Claude Code extension designed to inject ADHD-friendly interaction rules into conversations. Understanding how this tool maintains persistence across Claude Code sessions requires examining its dual-layer storage strategy, which bridges temporary session data with permanent filesystem flags.

## Session-Manager State Entries

The primary persistence mechanism relies on Claude Code's custom session entries. In [`extensions/i-have-adhd.ts`](https://github.com/ayghri/i-have-adhd/blob/main/extensions/i-have-adhd.ts), the extension stores the current enabled or disabled state using a dedicated session entry identified as `i-have-adhd-state`.

When a session initializes, the `restoreState` function queries the session-manager's branch via `getSavedState` to retrieve any existing entry. If no saved state exists, the system falls back to checking the default flag or the always-on file (lines 62-74). This entry is written immediately whenever the user toggles the mode through `setEnabled`, which calls `pi.appendEntry` to persist the change.

Because the session-manager preserves custom entries across session forks, resumptions, and compacted histories, this approach ensures your ADHD mode preference survives routine session lifecycle events without manual intervention.

```typescript
// Inside the extension, after a session starts
const saved = ctx.sessionManager.getBranch().find(
  e => e.type === "custom" && e.customType === "i-have-adhd-state"
);
console.log(saved?.data?.enabled); // true, false or undefined

```

## Always-On Flag File

For users requiring automatic activation in every new session, the extension implements a filesystem-based flag. A hidden file named `.i-have-adhd-always` placed in the Claude configuration directory (`$CLAUDE_CONFIG_DIR` or `~/.claude`) forces the extension to start enabled regardless of previous session states.

The `restoreState` function detects this flag using `existsSync(alwaysOnFlag)`. Additionally, the POSIX and PowerShell hooks located in [`hooks/always-on.sh`](https://github.com/ayghri/i-have-adhd/blob/main/hooks/always-on.sh) check for this file's presence before injecting the ruleset, exiting silently if the flag is absent (lines 12-14).

To enable permanent persistence across all future sessions:

```bash

# Create the flag file in the Claude config directory

touch ~/.claude/.i-have-adhd-always

# Restart Claude Code or start a new session – the ruleset is injected automatically

```

To disable the always-on behavior:

```bash
rm ~/.claude/.i-have-adhd-always   # removes the flag

# New sessions will start with ADHD mode off unless manually toggled

```

## Event Hooks and Lifecycle Integration

The extension registers listeners for three critical session events to maintain state consistency: `session_start`, `session_tree`, and `session_compact`.

The `session_start` and `session_tree` events trigger `restoreState` to re-inject the ruleset if the saved state indicates the mode is enabled. Meanwhile, the `session_compact` event calls `syncContext`, which verifies whether the rules are already present in the conversation context using `rulesAreInContext`, then injects or removes them accordingly (lines 22-26).

This hook-based architecture ensures that even when Claude Code compresses or transforms session history, the ADHD-friendly rules remain attached to the active context.

```text
/i-have-adhd          # toggles the current state

/i-have-adhd on       # forces the ruleset on

/i-have-adhd off      # disables the ruleset for this session

```

## Summary

- **Session-manager entries** provide temporary persistence across session forks and resumptions by storing state in `i-have-adhd-state` custom entries via `pi.appendEntry`.
- **Always-on flag files** offer permanent persistence by placing `.i-have-adhd-always` in the Claude config directory, triggering automatic rule injection at every `SessionStart`.
- **Event listeners** for `session_start`, `session_tree`, and `session_compact` ensure rules remain synchronized with the conversation context through `restoreState` and `syncContext` functions.
- The system falls back gracefully from saved session state to flag-file detection when determining whether to enable ADHD mode.

## Frequently Asked Questions

### What happens if I enable ADHD mode but don't create the always-on flag?

The extension stores your preference in the session-manager's custom entries, which persist across session forks and resumptions within the same conversation lineage. However, completely new sessions started from scratch will default to disabled unless you previously created the `.i-have-adhd-always` flag file.

### Where does the extension look for the always-on flag file?

According to the source code in [`extensions/i-have-adhd.ts`](https://github.com/ayghri/i-have-adhd/blob/main/extensions/i-have-adhd.ts), the extension checks for `.i-have-adhd-always` in the Claude configuration directory, referenced by the environment variable `$CLAUDE_CONFIG_DIR` or defaulting to `~/.claude`. The POSIX hook in [`hooks/always-on.sh`](https://github.com/ayghri/i-have-adhd/blob/main/hooks/always-on.sh) uses the same path resolution logic.

### Can I temporarily disable ADHD mode if the always-on flag is present?

Yes. Even with the flag file present, you can disable ADHD mode for the current session by running `/i-have-adhd off`. This updates the session-manager entry immediately via `setEnabled`, though the next new session will still start with the rules enabled due to the persistent flag file.

### How does the extension handle session compaction?

When Claude Code compacts session history, the extension's `session_compact` listener triggers `syncContext`, which calls `rulesAreInContext` to verify whether the ADHD ruleset exists in the current context window. If the saved state indicates the mode should be active but the rules are missing, the function re-injects them automatically.