Skip to content

core: two graph readers over the same tables with a stale comment explaining why #1354

Closed

Description

@schlessera

Part of #1341.

Context

packages/core/src/queries/graph.ts (492 lines, "Private UI-profile graph mechanics", used by ./queries) and packages/core/src/lib/graph/queries.ts (490 lines, used by brain graph) read the same tables and repeat constants (MAX_DISCOVERY_DEPTH = 8, DEFAULT_STALE_DAYS = 180). They differ in places: the UI profile clamps staleDays to 1..3650 (packages/core/src/queries/graph.ts:438), the CLI does not (packages/core/src/lib/graph/queries.ts:360). The CLI reader's header says ui-server keeps its own reader because it "deliberately does not depend on core" (packages/core/src/lib/graph/queries.ts:1-7), which is no longer true: both readers live in core.

Approved approach — 2026-10-09

A: one internal graph reader with explicit CLI/UI profiles. Consolidate common graph reading/traversal mechanics inside core. Keep the two existing surface adapters and public result types; each explicitly selects its internal profile. Profiles encode existing behavior, not a new extension interface or public configuration.

Preserve all current differences unless separately ruled: UI caps/truncation, ordering, precomputation/schema gates, missing-node errors, isolate defaults and staleDays clamping versus CLI defaults/degradation. The issue's two equal-size readers are not interchangeable; the implementation must inventory differences from source and use profile-specific tests before replacing either path. No behavior unification is authorized.

Scope

  • Implement one internal graph reader and explicit concrete CLI/UI profiles, with thin existing adapters retaining their result shapes and ownership. Define shared constants once; fix the header to explain the actual profile differences. Preserve the supported query API’s root-bound read snapshots/native connection lifetime and the CLI’s existing resolved-database use. No core-to-UI dependency, raw-handle public export or configurable reader seam.

Out of scope

  • Changing ./queries result shapes, errors, ordering, caps, defaults or brain graph --json behavior. Public graph semantics remain bound by docs/decisions/index-query-api.md and docs/content-index-queries.md.
  • Unifying profile behavior, exposing profile selection publicly, or changing graph computation/storage.

Acceptance criteria

  • Constants are defined once.
  • The header comment states the actual reason for any remaining split.
  • Both adapters call the one internal reader with the intended explicit profile; no duplicated complete traversal/reader remains under the old paths.
  • Inventory and independently assert profile differences on nonempty fictional fixtures: caps/truncation, ordering, isolate defaults, missing center/root, absent/old/uncomputed graph, and staleDays bounds/default. UI and CLI keep their former expected behavior; do not derive expected values from the new shared implementation.
  • Existing graph contracts and real index-query consumer tests pass unchanged, including snapshot/replacement/error lifetime coverage. A swapped-profile or removed profile guard mutation fails the intended distinction assertion; restore it afterward.
  • Published exports/types and package boundaries remain unchanged; normal consumer resolution still works.

Verification

bun run test packages/core

Contract impact

No supported contract change: this is an internal behavior-preserving consolidation. Add the normal refactor changeset. If the source inventory reveals a desired public behavior change, report it for a separate maintainer ruling rather than folding it into this refactor.

Triage recommendation — 2026-10-09

Recommend B: share established constants/primitives while keeping separate profiles until their differing behavior is explicitly reviewed. A reduces duplication further but can unintentionally collapse intentional surface differences. A ruling should name which differences are contractual and which may converge. This is a recommendation for the maintainer, not approval; keep the unresolved need.

2026-10-09 maintainer ruling: selected A, consolidated the explicit profile constraints and verification criteria, and removed the decision prerequisite. Preserve p3 and epic #1341; add agent-ready for implementation.

Activity

  1. added
    type: refactor
    area: core
    priority: p3
    needs: decision
    on Oct 9, 2026
  2. added
    agent-ready
    and removed
    needs: decision
    on Oct 9, 2026
  3. schlessera commented on Oct 9, 2026

    @schlessera
    OwnerAuthor

    Decision (maintainer, 2026-10-09): Choose A — one internal reader with explicit CLI/UI profiles.

    Consolidate common graph mechanics inside core while retaining the existing adapters, public result types and each profile’s behavior. Preserve caps, ordering, errors, precomputation/degradation, isolate defaults and staleDays policies; this is not approval to unify those contracts. Profiles are concrete internal choices, not a public extension seam. Correct the obsolete dependency comment.

    The body now requires a source-based difference inventory, independent profile tests and a profile mutation, alongside unchanged graph/query consumer contracts and snapshot ownership. No decision remains; remove needs: decision and add agent-ready, preserving p3 and #1341.

  4. schlessera commented on Oct 21, 2026

    @schlessera
    OwnerAuthor

    New evidence from a repository review on 2026-10-22, at main dcf5341aa, for the profile-difference inventory this issue requires:

    • The CLI profile never truncates, in any mode. Every return path in packages/core/src/lib/graph/queries.ts hard-codes truncated: false: lines 202, 223, 244, 271, 291 and 330. The explicit-root discovery walk (getDiscoveryGraph, packages/core/src/lib/graph/queries.ts:260-292) has no node budget at all. It BFS-es up to MAX_DISCOVERY_DEPTH hops and returns every reached node.
    • The UI profile caps the same walk at MAX_NODES = 5000 (packages/core/src/queries/graph.ts:6, bfs(..., MAX_NODES) at :376, applyCaps at :378) and reports truncated (:385).
    • brain graph export documents its output as {nodes, edges, truncated} (docs/cli.md:167). On the CLI profile, the truncated field cannot be true.

    Under the 2026-10-09 ruling this difference must be preserved by the consolidation, so this comment is not a scope change. It is one more row for the inventory test ("caps/truncation"). Whether the CLI should cap discovery output and report truncation is a separate public-behaviour question that needs its own maintainer ruling. Do not fold it into this refactor.

  5. self-assigned this
    on Nov 2, 2026
  6. schlessera commented on Nov 2, 2026

    @schlessera
    OwnerAuthor

    Claimed (claim-1341-1354-9b111c56) as part of the #1341 integration lane. Branch refactor/1354-graph-reader-profiles, targeting the epic integration branch epic/1341-shared-core-ops; the epic branch merges into main in one go once every ready child has landed there.

  7. added this to the 0.45.0 milestone on Nov 3, 2026
  8. schlessera commented on Nov 3, 2026

    @schlessera
    OwnerAuthor

    Delivered on main by the epic #1341 landing, #3505: merge commit 06473ef31, parents 559fe7b41 (main) and bd2decac7 (epic tip). This issue's change is #3295, squashed into the epic branch as 44be369b9 after passing ready proof on its exact head against the epic tip. The acceptance criteria and failing-first/mutation receipts are in that PR. Milestone 0.45.0. Closing manually: closing keywords do not fire for PRs that target a non-default branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

agent-readyarea: corepriority: p3type: refactor

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions