# StackTrail

CLASS in `@skmtc/core 0.63.4`

A position in a run, as a list of frames from the outermost to the
innermost. While the parser reads a schema, they can be `components`,
`schemas` and `User`. `toString` joins them:
`components:schemas:User`.

`append` and `remove` change the trail. To keep a position, use
`clone`. To add frames only while a function runs, use `trace`,
which gives the function its own trail.

## Signature

```ts
import { StackTrail } from '@skmtc/core'

class StackTrail
```

## Constructor

### constructor

```ts
constructor(stack?: readonly string[])
```

## Properties

- `stackTrail` (readonly string[]): The frames of the trail as a new read-only array, built on each read. Change the trail with `append`, `remove` or `trace`, not through this array. The trail itself is live: `trace` removes its frame after `fn` returns, so read the frames inside `fn` to keep them.

## Methods

### StackTrail.empty

```ts
StackTrail.empty(): StackTrail
```

The canonical empty trail - used for nodes that were synthesized
programmatically rather than parsed from a source document (they
have no position). Distinct from any parsed position: no parsed
`OasBase` node ever sits at the empty trail.

### isEmpty

```ts
isEmpty(): boolean
```

True when the trail holds no frames (a synthetic / positionless node).

### clone

```ts
clone(): StackTrail
```

Creates an independent trail with the same frames. The frames are
shared, not copied.

Returns: A new StackTrail instance with the same frames

### append

```ts
append(frame: string | string[]): StackTrail
```

Appends frame(s) to the end of the trail.

- `frame`: Single frame string or array of frames to append

Returns: This StackTrail instance for chaining

Throws:

- `Error`: When frame is not a string or string array

### trace

```ts
trace<T>(key: string | string[], fn: (st: StackTrail) => T): T
```

Runs `fn` on a copy of the trail with `key` appended: one frame for a
string, one frame per element for an array.

When `fn` returns, `key` is removed and must be the last frame. When
`fn` throws, the copy goes back to the frames it started with and the
error from `fn` is rethrown: a frame check there would replace it.

### traceAsync

```ts
traceAsync<T>(
  key: string | string[],
  fn: (st: StackTrail) => Promise<T>
): Promise<T>
```

Async form of `trace`.

### toJsonPointer

```ts
toJsonPointer(): JsonPointer
```

Converts the trail to an RFC 6901 JSON Pointer (URI fragment form).

Unlike `toStackRef`, this works for *any* visitor path - not
just components-rooted references. Used by the gen-maps system to
attach a `location` field to every parsed OAS schema, recording
exactly where in the document the schema lives.

Special characters in stack frames (`/`, `~`) are escaped per
RFC 6901.

Returns: A JSON Pointer string starting with `#/`

### toSchemaPointer

```ts
toSchemaPointer(): JsonPointer
```

The **document-relative** JSON Pointer for a parsed node, with the
run's operational prefix stripped - the resolvable form used as a
schema pointer in gen-maps.

A parsed node's trail is seeded with operational frames before any
document traversal: the worker pushes `[traceId, spanId]` and
`CoreContext.toArtifacts` wraps each phase in `.trace('parse', …)` /
`.trace('generate', …)` etc. The document path
(`components/schemas/Pet/properties/name`) is appended on top. Unlike
`toJsonPointer` - which serializes the *whole* trail and so does
NOT resolve against the input document - this drops everything up to
and including the phase frame, leaving only the document-relative path.

The remaining frames are the input document's own keys (`components`,
`paths`, …), never an `oas:` scheme.

Falls back to the full trail when no phase frame is present (a trail
seeded empty - e.g. in tests - is already document-relative). Returns
`#/` (document root) for the empty trail.

### toStackRef

```ts
toStackRef(): string | undefined
```

### remove

```ts
remove(frame: string | string[]): StackTrail
```

Removes frame(s) from the end of the trail.

- `frame`: Single frame string or array of frames to remove

Returns: This StackTrail instance for chaining

Throws:

- `Error`: When trying to remove a frame that doesn't match the last frame
- `Error`: When frame is not a string or string array

### toJSON

```ts
toJSON(): string
```

Converts the trail to JSON representation.

Used for serialization in JSON contexts. Returns the same
as toString() method.

Returns: String representation of the trail

### toString

```ts
toString(): string
```

Converts the trail to a colon-separated string.

Inside a frame, `%` is escaped as `%25` and `:` as `%3A`, so
`StackTrail.fromString` reads the same frames back.

### StackTrail.fromString

```ts
StackTrail.fromString(value: string): StackTrail
```

Reads a string written by `StackTrail.toString` back into a trail.

Splits on `:` and decodes `%25` and `%3A` in each frame. The empty
string gives the empty trail, so a trail of one empty frame is the one
trail that does not round-trip.
