# How the i-have-adhd Skill Maintains Conversation Context Across Multiple Turns

> Discover how the i-have-adhd skill maintains conversation context with typed markers and synchronization loops. Learn about its innovative approach to multi-turn dialogue.

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

---

**The i-have-adhd skill persists conversation state by injecting typed context markers into the runtime's session manager and reconciling them on every turn through a continuous synchronization loop.**

The `ayghri/i-have-adhd` repository implements a robust mechanism for maintaining conversation context across multiple turns in AI-assisted workflows. By storing explicit state markers directly within the session manager's context tree, the skill ensures that ADHD-friendly rules remain active—or properly disabled—throughout extended conversations, even when the underlying session structure is compacted or rebuilt.

## Injecting Typed Context Markers

The foundation of context persistence lies in **context markers**: specially typed messages that the skill injects into the conversation tree. When the skill activates ADHD rules or issues a "disabled" notice, it sends messages with distinct `customType` identifiers that become permanent entries in the session manager's context.

In [`extensions/i-have-adhd.ts`](https://github.com/ayghri/i-have-adhd/blob/main/extensions/i-have-adhd.ts), the skill injects markers using the `pi.sendMessage()` method:

```typescript
// Injecting the active rules marker (lines 140-144)
pi.sendMessage({
  customType: RULES_MESSAGE_TYPE,  // "i-have-adhd-rules"
  content: `${RULES_HEADER}\n\n${rules}`,
  display: false,
});

// Injecting the disabled marker when mode is turned off (lines 150-154)
pi.sendMessage({
  customType: DISABLED_MESSAGE_TYPE,  // "i-have-adhd-disabled"
  content: "ADHD mode is now disabled.",
  display: true,
});

```

These markers remain in the session tree as explicit context entries, allowing the skill to query its own state on subsequent turns.

## Extracting Context from the Session Manager

To read the injected markers, the skill uses the `contextMessages` helper function exported from [`extensions/context-compat.ts`](https://github.com/ayghri/i-have-adhd/blob/main/extensions/context-compat.ts) (lines 12-39). This utility safely abstracts differences in session manager APIs by checking for both `buildSessionContext()` and `buildContextEntries()` methods.

```typescript
import { contextMessages } from "./context-compat";

function rulesAreInContext(ctx: ExtensionContext): boolean {
  const msgs = contextMessages(ctx.sessionManager);
  // Returns array of marker objects with type, role, and customType fields
  return latestMarkerIsActive(
    msgs,
    "i-have-adhd-rules",
    "i-have-adhd-disabled"
  );
}

```

The `contextMessages` function normalizes the session data into a consistent format, ensuring the skill can evaluate context regardless of runtime version or session manager implementation details.

## Evaluating Active State Across Turns

Determining whether ADHD rules are currently active requires more than simply finding a marker. The `latestMarkerIsActive` function (lines 41-61 in [`extensions/context-compat.ts`](https://github.com/ayghri/i-have-adhd/blob/main/extensions/context-compat.ts)) implements a precedence algorithm that walks the marker array from oldest to newest, returning `true` only if the most recent relevant marker is of type `i-have-adhd-rules`.

This logic handles the case where a user toggles the mode multiple times within a session. If a "disabled" marker appears after a "rules" marker, the function correctly reports that the skill is inactive, ensuring the conversation context reflects the latest user intent.

## The Synchronization Loop

The skill maintains consistency through a **sync function** that runs on every relevant session event. Registered to trigger on `session_start`, `session_tree`, and `session_compact` events, the `syncContext` function in [`extensions/i-have-adhd.ts`](https://github.com/ayghri/i-have-adhd/blob/main/extensions/i-have-adhd.ts) (lines 34-59) continuously reconciles the actual context markers with the desired state.

The sync logic follows two critical rules:

- **If enabled but missing**: When ADHD mode is on but `rulesAreInContext()` returns false, the skill re-injects the rules marker immediately.
- **If disabled but present**: When mode is off but a rules marker exists, the skill injects a disabled notice to overwrite the previous state.

This approach guarantees that context compaction or tree reconstruction never strips away the skill's state, as the markers are automatically restored on the next sync cycle.

## Persisting State Across Session Resumes

Beyond immediate context markers, the skill preserves configuration through the session manager's saved state mechanism. On initialization, the extension calls `getSavedState` to retrieve any previous `i-have-adhd-state` entries (lines 81-95 in [`extensions/i-have-adhd.ts`](https://github.com/ayghri/i-have-adhd/blob/main/extensions/i-have-adhd.ts)).

The skill merges this persisted state with configuration flags and optional "always-on" file settings to restore the correct enabled status when a session resumes. This dual-layer persistence—context markers for immediate turn-to-turn state and saved entries for long-term configuration—ensures seamless continuity across session interruptions.

## Summary

- **Context markers** with `customType` fields (`i-have-adhd-rules`, `i-have-adhd-disabled`) are stored as permanent entries in the session manager's context tree.
- The `contextMessages` helper safely extracts these markers across different runtime API versions.
- `latestMarkerIsActive` determines the current skill state by evaluating the most recent marker in the chronology.
- The `syncContext` function runs on every session event to reconcile markers with the actual enabled/disabled configuration.
- Saved state entries (`i-have-adhd-state`) restore settings when sessions resume after interruption.

## Frequently Asked Questions

### What are context markers in the i-have-adhd skill?

Context markers are specialized messages injected into the conversation session using `pi.sendMessage()` with a `customType` field. These markers—specifically `i-have-adhd-rules` and `i-have-adhd-disabled`—become part of the session tree's permanent context, allowing the skill to query its own activation history on subsequent turns.

### How does the skill determine whether to show rules or a disabled notice?

The skill uses the `latestMarkerIsActive` function to analyze the chronology of context markers. It walks the marker array from oldest to newest and returns `true` only if the most recent marker of interest is the rules type and no subsequent disabled marker exists. If the latest marker indicates disabled status, or if no markers exist, the skill evaluates the current configuration to decide whether to inject new content.

### What happens to the context when the session is compacted or rebuilt?

The skill registers `syncContext` to trigger on `session_compact` and `session_tree` events. When these events fire, the function checks the current context via `rulesAreInContext()` and re-injects the appropriate marker if the context was lost during compaction. This ensures the ADHD mode state survives memory optimizations and tree reconstructions.

### How is the ADHD mode state preserved between separate sessions?

The skill persists configuration through the session manager's `getSavedState` mechanism, specifically looking for entries tagged `i-have-adhd-state`. When a session resumes, the extension merges this saved state with current configuration flags and filesystem checks (such as the "always-on" file) to restore the exact enabled status from the previous session.