# latestMarkerIsActive Function in i-have-adhd: Purpose and Cross-Runtime Significance

> Understand the purpose of the latestMarkerIsActive function. It checks custom feature marker states across i-have-adhd runtimes for cross-runtime significance.

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

---

**The `latestMarkerIsActive` function determines whether a custom feature marker is currently enabled or disabled by scanning context messages and returning the state of the most recent matching marker.**

This utility enables runtime-agnostic feature toggling in the `ayghri/i-have-adhd` repository, allowing the library to adapt its behavior across diverse AI runtimes without hardcoding assumptions about any single platform's API.

## Where latestMarkerIsActive Is Defined

The `latestMarkerIsActive` function lives in **[[`extensions/context-compat.ts`](https://github.com/ayghri/i-have-adhd/blob/main/extensions/context-compat.ts)](https://github.com/ayghri/i-have-adhd/blob/main/extensions/context-compat.ts#L41-L61)** alongside its companion helper `contextMessages`. Together these utilities form the compatibility layer that abstracts away runtime differences.

## How latestMarkerIsActive Works

The function implements a **state-tracking scan** with four sequential steps:

1. **Iterate through all messages** — processes the array in order from first to last
2. **Filter to custom markers** — only examines messages where `role === "custom"` **and** `type === "custom_message"`
3. **Track the latest state** — updates an `active` boolean based on marker matches:
   - `customType === activeType` → `active = true`
   - `customType === disabledType` → `active = false`
4. **Return final state** — the last matching marker wins, giving the *latest* state

This **last-write-wins** semantics ensures user preference changes in conversation history are respected immediately.

## Code Example: Basic Usage

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

const messages = [
  { role: "custom", type: "custom_message", customType: "adhd_enabled" },
  { role: "assistant", type: "text", content: "Hello!" },
  { role: "custom", type: "custom_message", customType: "adhd_disabled" },
];

const adhdIsOn = latestMarkerIsActive(
  messages,
  "adhd_enabled",   // activeType
  "adhd_disabled",  // disabledType
);

console.log(adhdIsOn); // → false (later disabled marker wins)

```

## Architectural Significance of latestMarkerIsActive

### Runtime-Agnostic Feature Toggling

Modern AI runtimes (Claude, Codex, Pi, OMP, etc.) expose session-manager APIs with varying context message formats. The `latestMarkerIsActive` abstraction lets the library detect feature states **without runtime-specific code**, scanning standardized message structures instead.

### Graceful Degradation

When runtimes change APIs or fail to provide context, surrounding code falls back to a no-marker state. This prevents crashes and maintains stability across version migrations.

### User-Controlled Preferences

End-users embed markers like `adhd_enabled` or `adhd_disabled` directly in conversations. The engine respects these preferences automatically without external configuration.

## Where latestMarkerIsActive Is Used

| Location | Purpose |
|----------|---------|
| **[[`scripts/check_context_compat.ts`](https://github.com/ayghri/i-have-adhd/blob/main/scripts/check_context_compat.ts)](https://github.com/ayghri/i-have-adhd/blob/main/scripts/check_context_compat.ts)** | Guards compatibility checks; aborts when a marker indicates intentional disable |
| **[[`extensions/i-have-adhd.ts`](https://github.com/ayghri/i-have-adhd/blob/main/extensions/i-have-adhd.ts)](https://github.com/ayghri/i-have-adhd/blob/main/extensions/i-have-adhd.ts)** | Decides whether to inject ADHD-friendly response rules into the session |

## Integration Example: Compatibility Check Guard

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

function shouldRunCompatibilityCheck(sessionManager: unknown): boolean {
  const msgs = contextMessages(sessionManager);
  
  // Skip check if user explicitly disabled it
  if (!latestMarkerIsActive(msgs, "compat_check_enabled", "compat_check_disabled")) {
    return false;
  }
  
  return true; // proceed with check
}

```

## Integration Example: ADHD Skill Injection

```typescript
// extensions/i-have-adhd.ts
export function maybeInjectSkill(sessionManager: unknown): void {
  const msgs = contextMessages(sessionManager);
  
  if (latestMarkerIsActive(msgs, "adhd_enabled", "adhd_disabled")) {
    // Inject ADHD-friendly formatting: structured lists,
    // clear headings, progress indicators, etc.
  }
}

```

## Key Files for latestMarkerIsActive

- **[[`extensions/context-compat.ts`](https://github.com/ayghri/i-have-adhd/blob/main/extensions/context-compat.ts)](https://github.com/ayghri/i-have-adhd/blob/main/extensions/context-compat.ts)** — core implementation and `contextMessages` helper
- **[[`scripts/check_context_compat.ts`](https://github.com/ayghri/i-have-adhd/blob/main/scripts/check_context_compat.ts)](https://github.com/ayghri/i-have-adhd/blob/main/scripts/check_context_compat.ts)** — real-world usage pattern with early-exit logic
- **[[`extensions/i-have-adhd.ts`](https://github.com/ayghri/i-have-adhd/blob/main/extensions/i-have-adhd.ts)](https://github.com/ayghri/i-have-adhd/blob/main/extensions/i-have-adhd.ts)** — connection between marker logic and main feature

## Summary

- `latestMarkerIsActive` provides **single-source-of-truth** state detection for custom features across the entire codebase
- Uses **last-write-wins semantics** on context message arrays to respect the most recent user preference
- Enables **cross-runtime compatibility** by abstracting over platform-specific session APIs
- Powers **user-controlled toggles** embedded directly in conversation context
- Guards **critical code paths** in compatibility checks and skill injection decisions

## Frequently Asked Questions

### What happens if no matching markers are found?

The function returns `false` by default. The `active` variable initializes to `false` and only flips to `true` when an explicit `activeType` marker appears.

### Why compare both activeType and disabledType instead of just checking presence?

Explicit paired markers prevent ambiguity. A user might enable a feature early in conversation then disable it later—tracking both types ensures the **latest intentional state** wins regardless of marker order complexity.

### Can this pattern work with runtimes that don't use context messages?

Yes. The `contextMessages` helper in the same file normalizes access patterns. For runtimes without native context support, the helper returns an empty array, causing `latestMarkerIsActive` to return `false` and trigger graceful fallback behavior.

### How does this differ from simple environment variable configuration?

Environment variables are **static** per process. Context message markers are **dynamic** within a single session, allowing real-time toggling without restarts and per-conversation customization rather than global settings.