# tools/core/analyzers/snapshot.analyzer.ts

> 90 lines of code and 23 definitions.

Tree: Coordination tree
Language: typescript
Layer: processing
Canonical: https://banes-lab.com/anatomy/coordination#file-coordination-tools-core-analyzers-snapshot-analyzer-ts
Source text: https://banes-lab.com/source/coordination/tools/core/analyzers/snapshot.analyzer.ts.txt

Listed in [tools/core/analyzers](https://banes-lab.com/api/source/coordination/tools/core/analyzers.md), after [tools/core/analyzers/marker.analyzer.ts](https://banes-lab.com/source/coordination/tools/core/analyzers/marker.analyzer.ts.md) and before [tools/core/analyzers/source.analyzer.ts](https://banes-lab.com/source/coordination/tools/core/analyzers/source.analyzer.ts.md).

## Definitions

- `markOf` (lexical_declaration, line 19)
- `isFrozen` (lexical_declaration, line 80)
- `lifetimeLabel` (lexical_declaration, line 84)
- `measured` (lexical_declaration, line 88, exported)
- `anchorsOf` (lexical_declaration, line 39)
- `MARKDOWN` (lexical_declaration, line 7)
- `RECORD_ANCHOR` (lexical_declaration, line 9)
- `ANCHOR_KINDS` (lexical_declaration, line 11, exported)
- `MARK_SEED` (lexical_declaration, line 13)
- `MARK_SHIFT` (lexical_declaration, line 15)
- `MARK_MODULUS` (lexical_declaration, line 17)
- `held` (lexical_declaration, line 20)
- `sameKinds` (lexical_declaration, line 27, exported)
- `out` (lexical_declaration, line 44)
- `markedElsewhere` (lexical_declaration, line 55, exported)
- `forbidsRemoval` (lexical_declaration, line 76)
- `current` (lexical_declaration, line 89, exported)
- `frozen` (lexical_declaration, line 90, exported)
- `frozenAnchors` (lexical_declaration, line 91, exported)
- `governed` (lexical_declaration, line 93, exported)
- `declared` (lexical_declaration, line 94, exported)
- `source` (lexical_declaration, line 99, exported)
- `anchors` (lexical_declaration, line 100, exported)

## Uses

- [tools/core/analyzers/fence.analyzer.ts](https://banes-lab.com/source/coordination/tools/core/analyzers/fence.analyzer.ts.md)

## Source

```typescript
import type { Extent, Measured } from "../types/snapshot.types.ts";
import { type Lifetime, lifetimeOf } from "../../../config/surface.config.ts";
import { delimitersIn } from "./fence.analyzer.ts";
import { readSource } from "../iterators/file.iterator.ts";
import { resolve } from "node:path";

const MARKDOWN = ".md";

const RECORD_ANCHOR = "record";

export const ANCHOR_KINDS: readonly string[] = [RECORD_ANCHOR];

const MARK_SEED = 5381;

const MARK_SHIFT = 33;

const MARK_MODULUS = 4_294_967_296;

const markOf = function markOf(source: string): string {
    let held = MARK_SEED;
    for (const character of source) {
        held = (held * MARK_SHIFT + (character.codePointAt(0) ?? 0)) % MARK_MODULUS;
    }
    return String(held);
};

export const sameKinds = function sameKinds(held: readonly string[]): boolean {
    if (held.length !== ANCHOR_KINDS.length) {
        return false;
    }
    for (const kind of ANCHOR_KINDS) {
        if (!held.includes(kind)) {
            return false;
        }
    }
    return true;
};

const anchorsOf = function anchorsOf(path: string, source: string): string[] {
    if (!path.endsWith(MARKDOWN)) {
        return [];
    }

    const out: string[] = [];
    for (const delimiter of delimitersIn(source)) {
        if (!delimiter.open) {
            continue;
        }
        out.push(`${RECORD_ANCHOR}:${delimiter.agent}`);
    }

    return out;
};

export const markedElsewhere = function markedElsewhere(
    mark: string,
    prior: Readonly<Record<string, Extent>>,
    current: ReadonlyMap<string, Extent>,
): string | null {
    if (mark.length === 0) {
        return null;
    }

    for (const [path, extent] of current) {
        if (prior[path] !== undefined) {
            continue;
        }
        if (extent.mark === mark) {
            return path;
        }
    }

    return null;
};

const forbidsRemoval = function forbidsRemoval(declared: Lifetime): boolean {
    return declared.removal === "none";
};

const isFrozen = function isFrozen(declared: Lifetime): boolean {
    return declared.mutability === "frozen";
};

const lifetimeLabel = function lifetimeLabel(declared: Lifetime): string {
    return `${declared.retention} · ${declared.mutability} · ${declared.removal}`;
};

export const measured = function measured(repoRoot: string, paths: readonly string[]): Measured {
    const current = new Map<string, Extent>();
    const frozen = new Set<string>();
    const frozenAnchors = new Set<string>();

    const governed = paths.flatMap((path) => {
        const declared = lifetimeOf(path);
        return declared !== null && (forbidsRemoval(declared) || isFrozen(declared)) ? [{ declared, path }] : [];
    });

    for (const { declared, path } of governed) {
        const source = readSource(resolve(repoRoot, path));
        const anchors = anchorsOf(path, source);
        current.set(path, { anchors, lifetime: lifetimeLabel(declared), mark: markOf(source) });
        if (isFrozen(declared)) {
            frozen.add(path);
            anchors.forEach((anchor) => {
                frozenAnchors.add(anchor);
            });
        }
    }

    return { current, frozen, frozenAnchors };
};
```
