Skip to content

feat(core): validate maxHistory option and document hierarchyDepth: 0 behaviour #254

@vamgan

Description

@vamgan

Summary

Two small API hardening improvements surfaced during a code audit:

1. maxHistory accepts invalid values silently

createAskableContext({ maxHistory: -1 }) stores -1 without error. The history cap check then behaves oddly (history.length > -1 is always true, so every push trims the array immediately). Passing NaN falls through to the default of 50 silently.

Fix: Add a runtime guard in the constructor:

if (options?.maxHistory !== undefined && (options.maxHistory < 0 || !Number.isInteger(options.maxHistory))) {
  throw new RangeError('maxHistory must be a non-negative integer');
}

2. hierarchyDepth: 0 is undocumented

Passing hierarchyDepth: 0 to any serialization method returns an empty ancestors array (all ancestor context is suppressed). This is intentional and the implementation is correct, but it is not documented in the type definition or API docs, so users may be confused when their ancestors disappear.

Fix: Add a JSDoc note to AskablePromptContextOptions.hierarchyDepth:

/**
 * Maximum number of ancestor segments to include.
 * Pass `0` to suppress all ancestor context.
 * Defaults to unlimited.
 */
hierarchyDepth?: number;

Acceptance criteria

  • createAskableContext({ maxHistory: -1 }) throws a RangeError
  • createAskableContext({ maxHistory: NaN }) throws a RangeError
  • hierarchyDepth JSDoc clarifies that 0 suppresses all ancestors
  • Existing tests pass; new tests cover the invalid-maxHistory cases

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreCore package and cross-package runtime behaviorenhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions