How `latestMarkerIsActive` Tracks ADHD Mode State in i-have-adhd

latestMarkerIsActive is a utility in extensions/context-compat.ts that scans ordered context markers to return a boolean indicating whether ADHD-friendly rules are currently active based on the most recent custom marker.

The latestMarkerIsActive function is the core state-tracking mechanism in the ayghri/i-have-adhd repository. It determines whether the ADHD-friendly mode is enabled by evaluating custom context messages injected into the model's conversation history. This logic ensures that only the newest marker dictates the current mode across sessions.

How latestMarkerIsActive Works

In extensions/context-compat.ts, latestMarkerIsActive receives three arguments: messages, activeType, and disabledType. It iterates over the array of context messages and returns a boolean reflecting the latest state.

Function Signature and Parameters

The function accepts the following parameters:

  • messages: The array of context markers extracted from the session manager via contextMessages.
  • activeType: The custom type string that signals enabled rules ("i-have-adhd-rules").
  • disabledType: The custom type string that signals disabled rules ("i-have-adhd-disabled").

Step-by-Step Execution Logic

The implementation follows a simple sequential scan:

  1. Initialise a local flag active = false.
  2. Iterate over every message in messages.
  3. Filter only messages that are custom. A message qualifies if role === "custom" or type === "custom_message"; all other roles or types are ignored.
  4. When message.customType matches activeType, set active = true.
  5. When message.customType matches disabledType, set active = false.
  6. Because the loop processes messages in order, the final value of active reflects the most recent marker.
  7. Return the boolean active.

This ordered evaluation means that if an enabled marker appears early but a disabled marker appears later, the function returns false.

Mode State Tracking in the Extension

The extension relies on latestMarkerIsActive to keep the ADHD mode state consistent across sessions. It never guesses the state; it always asks the context.

The rulesAreInContext Helper

In extensions/i-have-adhd.ts, the helper rulesAreInContext delegates directly to latestMarkerIsActive:

function rulesAreInContext(ctx: ExtensionContext): boolean {
  return latestMarkerIsActive(
    contextMessages(ctx.sessionManager),
    RULES_MESSAGE_TYPE,
    DISABLED_MESSAGE_TYPE,
  );
}

Here, contextMessages(ctx.sessionManager) feeds the current session markers into the evaluator. The helper returns true only when the latest relevant marker is an active rule signal.

Keeping Session Context in Sync

The extension uses the returned boolean to inject or remove markers precisely:

  • When rulesAreInContext returns false while ADHD mode is enabled, the extension injects a new active marker.
  • When rulesAreInContext returns true while the mode is disabled, the extension injects a "i-have-adhd-disabled" marker to override the previous rules.

This push-pull mechanism guarantees that the model's context always contains a single source of truth for the current mode.

Runtime Compatibility Across Pi and OMP

The function also underpins the sanity-check script scripts/check_context_compat.ts. This script validates that marker ordering behaves correctly for both runtimes:

  • Pi uses type: "custom_message".
  • OMP uses role: "custom".

Because latestMarkerIsActive checks both fields, it remains compatible regardless of which runtime injected the marker.

Practical Code Examples

The following examples demonstrate how latestMarkerIsActive evaluates different message histories:

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

const ACTIVE = "i-have-adhd-rules";
const DISABLED = "i-have-adhd-disabled";

/* Example 1 – rules enabled */
const msgs1 = [
  { role: "custom", customType: ACTIVE },
];
console.log(latestMarkerIsActive(msgs1, ACTIVE, DISABLED)); // true

/* Example 2 – rules disabled after being enabled */
const msgs2 = [
  { role: "custom", customType: ACTIVE },
  { role: "custom", customType: DISABLED },
];
console.log(latestMarkerIsActive(msgs2, ACTIVE, DISABLED)); // false

/* Example 3 – unrelated messages are ignored */
const msgs3 = [
  { role: "user", content: "Hello!" },
  { type: "message", content: "How are you?" },
];
console.log(latestMarkerIsActive(msgs3, ACTIVE, DISABLED)); // false

These examples show that unrelated messages are filtered out and that the last matching marker always wins.

Summary

  • latestMarkerIsActive lives in extensions/context-compat.ts and evaluates ordered context markers to determine ADHD mode state.
  • It accepts messages, activeType, and disabledType, scanning only custom messages to find the latest marker.
  • The boolean result reflects whichever marker appeared last, ensuring the newest instruction always overrides earlier ones.
  • extensions/i-have-adhd.ts consumes this utility through rulesAreInContext to keep session context synchronized.
  • A compatibility script in scripts/check_context_compat.ts verifies correct behavior across Pi and OMP runtimes.

Frequently Asked Questions

What does latestMarkerIsActive return when no custom markers exist?

It returns false. The function initialises active = false and only flips the flag when it encounters a matching custom marker. If the message array contains no custom messages, or only unrelated roles and types, the flag remains false.

Why does the function check both role === "custom" and type === "custom_message"?

This dual check provides compatibility across different runtimes. OMP injects custom markers using role: "custom", while Pi uses type: "custom_message". Checking both fields ensures the extension works correctly regardless of the underlying platform.

How does the extension use the boolean from latestMarkerIsActive?

In extensions/i-have-adhd.ts, the rulesAreInContext helper passes session messages into latestMarkerIsActive. When the result is false but ADHD mode is enabled, the extension injects an active rule marker. When the result is true but the mode is disabled, it injects a disabled marker to cancel the previous rules.

Where is the marker-ordering logic tested?

The script scripts/check_context_compat.ts serves as a sanity check. It validates that latestMarkerIsActive correctly respects message ordering for both Pi and OMP formats, ensuring that later markers always override earlier ones.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →