Imports:
  - Types:
      - schema
    Usages:
      - schema-usage
    From: goga/schema
  - Types:
      - SchemaNode
    From: goga/schema/hooks
  - Types:
      - codegraph
    Usages:
      - codegraph-usage
    From: goga/codegraph
  - Types:
      - CodegraphNode
    From: goga/codegraph/facts
  - Types:
      - DriftHooks
      - DriftFilters
      - EntryFacts
      - ReportEntry
      - RunOutcome
      - RunCompletion
    Usages:
      - checkpoints
    From: goga/drift/hooks

Usages:
  convention: .goga/usages/conventions.md

Annotations: |
  The `convention` practice is used for:
  - Working with the codebase
  - Organizing the REPL development cycle
  - Debugging and testing
  - Organizing the test infrastructure
  - Understanding the general principles and rules of development and testing in the project

  This cell compares the declared contract view with the code-derived
  view of the project and reports their structural discrepancies.
  The declared view is produced and consumed per the `schema-usage`
  practice, the code-derived view per the `codegraph-usage` practice.
  Discrepancies are facts of the complete views — the filters scope
  the report only. The caller owns the serialization of the returned
  report.

  The caller uses the `checkpoints` practice to wrap the comparison
  pipeline with the run's start and completion moments, deliver the report
  amendment over the finished scoped report, and deliver the entry-facts
  amendment over the settled final list.

---

"DriftEntry(kind: str, cell: str, types: list[str] | None, tools: dict[str, dict[str, object]] = {})":
  location: compare.py
  annotations: |
    One flat entry of the drift report.

    `kind`: the direction of the discrepancy — schema_only when the
            subject exists only in the declared view, codegraph_only
            when it exists only in the code-derived view
    `cell`: the normalized cell path of the discrepancy subject
    `types`: the sorted type names of the cell missing from the other
             view; None marks a cell-level discrepancy
    `tools`: the committed tools overlay of the entry — empty when no
             tool contributed

    Apply the `convention` practice for the data-model rules.

    Requirements:
    - Immutable data class per `convention`
    - The `tools` field carries the committed per-tool facts overlay;
      it is empty iff no tool wrote at least one fact on the entry
    - The comparison never populates `tools` — it stays empty until
      the entry-facts amendment places a non-empty overlay
  properties:
    "kind -> str": |
      The direction of the discrepancy — schema_only or codegraph_only.
    "cell -> str": |
      The normalized cell path of the discrepancy subject.
    "types -> list[str] | None": |
      Sorted type names missing from the other view; None marks a
      cell-level discrepancy.
    "tools -> dict[str, dict[str, object]]": |
      The committed tools overlay of the entry; empty when no tool
      contributed.

"compare_views(schema_tree: list[SchemaNode], codegraph_tree: list[CodegraphNode]) -> entries: list[DriftEntry]":
  location: compare.py
  annotations: |
    Compare the two complete views and produce every structural
    discrepancy entry.

    `schema_tree`: the complete declared view
    `codegraph_tree`: the complete code-derived view
    `entries`: every discrepancy of the two views, in the stable order

    Algorithm:
    1. Index each view — cell path to the cell's type names: the
       `SchemaNode`.path with `SchemaNode`.types on one side, the
       `CodegraphNode`.cell with `CodegraphNode`.types on the other
    2. Emit one cell-level entry for every cell path present in
       exactly one view — schema_only when only the declared view
       carries the path, codegraph_only otherwise — with no type list
    3. For every cell path present in both views, compare the two
       type-name sets: emit one schema_only entry carrying the sorted
       names declared but absent from the code, and one
       codegraph_only entry carrying the sorted names present in the
       code but undeclared — a direction with no missing names emits
       nothing
    4. Order the entries by cell path, then by kind

    Requirements:
    - Pure — no view is modified, no file is read or written
    - Identity is name-level — the normalized cell path and the type
      name; no signature or annotation semantics
    - A cell-level entry never coexists with type-level entries of
      the same cell
    - A cell with type drift in both directions yields two entries
    - An entry's type list is sorted and duplicate-free
    - Views that agree produce an empty list

    Constraints:
    - Do not apply filters here — the entries are facts of the views
      passed in

"scope_entries(entries: list[DriftEntry], schema_tree: list[SchemaNode], codegraph_tree: list[CodegraphNode]) -> report: list[DriftEntry]":
  location: compare.py
  annotations: |
    Scope the computed entries to the filtered views.

    `entries`: the computed discrepancies of the complete views
    `schema_tree`: the filtered declared view
    `codegraph_tree`: the filtered code-derived view
    `report`: the surviving entries, order preserved

    Algorithm:
    1. Collect the surviving cell paths — every `SchemaNode`.path of
       `schema_tree` and every `CodegraphNode`.cell of
       `codegraph_tree`
    2. Keep an entry iff its cell appears among the surviving paths

    Requirements:
    - An entry survives iff its cell survives the filters in at least
      one of the two filtered views — never both required
    - The output order is the input order
    - Pure — the inputs stay unmutated

    Constraints:
    - Do not fabricate, alter, or reorder entries — survival is the
      only effect

"drift(cells: list[str], max_depth: int | None = None, depends_on: list[str] = []) -> report: list[DriftEntry]":
  location: drift.py
  annotations: |
    Produce the drift report of the project — the structural
    discrepancies between the declared contracts and the actual code,
    observed and adjusted by the installed tool packages.

    `cells`: cell paths to scope the report
    `max_depth`: maximum nesting depth to scope the report; None —
                 no limit
    `depends_on`: scope the report to cells depending on the given
                  paths
    `report`: the final post-amendment discrepancy entries in the
              stable order; an empty list when the final report is
              empty

    Use the `checkpoints` practice for the moments and the amendments
    of the run.

    Algorithm:
    1. Emit the start moment via the `DriftHooks` entry — its
       emit_run_started checkpoint — with `DriftFilters` mirroring the
       requested filters
    2. Wrap every following stage: resolve the exclusion list — the
       lint ignore entries of the project config — once and deliver it
       to both `schema` calls; produce the complete views — call
       `schema` and `codegraph` with no filters; produce the filtered
       views — call `schema` and `codegraph` with `cells`, `max_depth`,
       `depends_on`
    3. Compare the complete views with `compare_views`; scope the
       entries with `scope_entries` over the filtered views — the
       scoped report is computed
    4. Deliver the report amendment: project the scoped entries to
       `EntryFacts` and hand them to the `DriftHooks` entry, its
       amend_report checkpoint — the settled final list returns
    5. Deliver the entry-facts amendment: for every entry of the
       settled list — comparison-produced and tool-added alike —
       project it to `EntryFacts`, deliver via the amend_entry
       checkpoint, and place the returned tools area on the entry iff
       non-empty; an empty settled list fires nothing
    6. Emit the completion moment via the emit_run_completed checkpoint
       with a `RunCompletion` carrying the final report projected to
       `ReportEntry` records, the `RunOutcome` finished marker, an empty
       reason; return the final report — the `DriftEntry` records with
       their tools overlays

    Failure paths inside the wrap:
    - A producer or comparison failure (steps 2-3): emit the completion
      moment with no report stage, the `RunOutcome` crashed marker, and
      a credential-free reason derived from the propagated error;
      re-raise — the moment never masks the failure
    - A hard failure of the report amendment (step 4): emit the
      completion moment with the scoped pre-amendment report, the
      `RunOutcome` crashed marker, a credential-free reason; re-raise
    - A hard failure of the entry-facts amendment (step 5): emit the
      completion moment with the scoped pre-amendment report, the
      `RunOutcome` crashed marker, a credential-free reason; re-raise
    - A failing moment hook is soft — a warning in the log; it never
      changes the report or the exit code

    Requirements:
    - Run ordering is fixed: start moment → view production,
      comparison, scoping → report amendment → entry facts amendment →
      completion moment
    - The completion moment fires on every terminal state of a started
      run — finished, producer-failed, and amendment-failed alike —
      carrying the last computed report stage: the final report (list
      settled, facts areas included) on finished runs, the scoped
      pre-amendment report when a later stage failed, nothing when the
      run failed before the report was computed
    - The comparison always runs on the complete views; the filters
      scope the report only
    - The declared and code-derived views of one run cover the same
      directory universe — the lint ignore exclusion of the project
      config scopes the declared view's AST walk exactly as it scopes
      the code-derived view's source discovery; ignored directories
      contribute no cells, no errors, and no entries
    - The depends_on asymmetry — declared dependencies in the
      declared view, resolved imports in the code-derived view —
      never surfaces as fabricated cell-level entries
    - The amendments operate solely on the finished scoped report and
      never feed back into the views or the comparison
    - The returned report is the settled post-amendment list — a
      tool-added or tool-removed entry changes what the caller sees
      and how the caller grades the run
    - No adjustment provenance appears anywhere in the returned report
    - Facts areas never carry failure semantics — a raising or
      malformed amendment hook fails the run rather than altering
      entries
    - Deterministic and idempotent — identical project state,
      configuration, and installed tools produce identical reports
    - Read-only — no file is created or modified
    - Every error of a view producer propagates unchanged — the
      comparison adds no failures of its own
    - With no subscriptions — or no tool packages installed — the
      report, its entries, and the byte-level output are identical to
      the run without the extension; the moments are unobservable

    Constraints:
    - Do not read manifests or sources — the views arrive fully
      assembled from `schema` and `codegraph`; the project config read
      resolves the exclusion list only
    - Do not apply filters inside the comparison
    - Do not emit provenance markers — the tools areas are the
      contribution idiom, not provenance

---

Author: Goga
CreatedAt: 14/10/26

Description: |
  Structural drift between the declared contracts and the actual code.

