Skip to content

feat(roadmap): one shared citation module for gate and brain #6440

Done

Description

@dan-petty

Problem or Motivation

Item #4479 closed on 2026-11-12 without the scope that its comment of 2026-11-12T04:11Z added. That comment asks for one citation module with public functions to parse, verify and re-anchor a citation. The stage gate, the question resolver (#4808), brain add (#4671) and brain repair (#4687) must all use it. Without one module, 4 copies of the same logic drift apart.

PR #5040 (created 2026-11-12T11:17Z, merged 13:12Z) delivered only the stage-gate rule:

  • _anchored_line is private in src/devops_cli/roadmap/stage/stage_change.py:1749. The citation patterns _LINE_CITATION and _SYMBOL_CITATION are private at stage_change.py:207-212.
  • git grep on src/ finds no span_hash, no citation module and no permalink function (origin/main 0d90de5, 2026-11-19).
  • No code parses the structured citation of ADR 0025 (docs/adr/0025-generated-documents-stay-in-git-and-the-project-brain-cites-them.md:53-61): {path, commit, lines, span_hash, doc_class, generated_from?, adr_status?, as_of}.

The cause: stage and implement got no item comments until #6182 (for #6167) merged at 2026-11-17T16:26Z. #6182 fixes future runs, but not this delivered item.

Effects:

Proposed Solution & Architectural Design

Build the module that the scope comment of #4479 describes:

  1. Parse a path:N:text citation, a path:def name citation and the structured citation of ADR 0025 into one typed model.
  2. Render a permalink at a commit.
  3. Verify a citation at a SHA with git show <sha>:<path> (0 GitHub requests), else with 1 contents GET.
  4. Compute the span hash: SHA-256, the first CONST_KB_SHA_DIGEST_LENGTH (16) hex characters, as src/devops_cli/ai/kb.py:614 does.
  5. Re-anchor a span that matches exactly one place. Refuse 0 matches and 2 or more matches.
  6. Move the stage gate to the module: _line_citation, _anchored_line and the two patterns of stage_change.py call the public functions. The behavior of the gate does not change.

Target Subsystem

SDLC, Git & PR Governance (the stage citation gate in src/devops_cli/roadmap/stage/stage_change.py, and a new shared citation module that the brain items #4671, #4687 and #4808 import).

Existing Tool Considered

git (git show <sha>:<path>) gives the text at a commit, and hashlib gives the span hash. No open tool parses this project's citation forms. The alternatives check of #4479 (2026-11-11) found none. The module is project code on top of git and the standard library. Cost: about 150 lines plus tests, about 60k tokens and 2 h (estimate).

Standards Compliance

  • I confirm this proposal aligns with AGENTS.md engineering principles.
  • I am willing to submit a pull request implementing this feature with accompanying tests.

Acceptance Criteria

Dependencies

None. The structured citation form is in ADR 0025 (#4884, closed).

Alternatives check of 2026-11-19: none.
Tool: none found.
Reason: Searches found no maintained tool that parses this project's line, symbol and ADR 0025 structured citation forms, verifies them at a commit or re-anchors a span at a unique match; git and hashlib only give the raw text and hash.

Lacks:

  • Parsing of path:N:text and path:def name citations
  • Typed model of the ADR 0025 structured citation
  • Verification at a SHA with git show and one contents fallback
  • Span hash of 16 hex characters from the span text
  • Re-anchoring only when the span text occurs exactly once
  • Permalink rendering with commit, path and line range

Proposed design

Problem statement

PR #5040 delivered only the stage-gate citation rule. The anchor logic and the two citation patterns stay private in src/devops_cli/roadmap/stage/stage_change.py. No code parses the structured citation of ADR 0025. The stage gate, the question resolver (#4808), brain add (#4671) and brain repair (#4687) need one shared module. Without that module, four copies of the same logic drift apart.

Acceptance criteria

Requirement: One parse function for three citation forms

The citation module SHALL give one public function that parses a line citation, a symbol citation and a structured citation into one typed model.

Scenario: Parse each of the three forms

  • WHEN A test parses the line citation src/devops_cli/ai/kb.py:614:sha = hashlib.sha256(...), the symbol citation src/devops_cli/ai/kb.py:def load_matched_kb_manuals and the structured citation object of ADR 0025
  • THEN The test asserts the path, the line or the symbol, the commit, the line range, the span hash and the document class of each result.

Requirement: Span hash matches the knowledge base hash

The module MUST compute a span hash as the first CONST_KB_SHA_DIGEST_LENGTH hexadecimal characters of the SHA-256 of the span text.

Scenario: The module and the knowledge base give the same hash

  • WHEN A test computes the span hash of one text with the module and with hashlib.sha256(text.encode("utf-8")).hexdigest()[:CONST_KB_SHA_DIGEST_LENGTH]
  • THEN The test asserts two equal values of 16 hexadecimal characters.

Requirement: Verification at a local commit makes no GitHub request

The module SHALL verify a citation at a commit with git show <sha>:<path> and MUST make no GitHub request when the local repository holds the commit.

Scenario: A local commit verifies with no request

  • WHEN A test verifies a citation at a commit of the local repository, with the contents client replaced by a double that raises on any call
  • THEN The test asserts the verification result and that the double recorded no call.

Requirement: A commit outside the clone costs one contents GET

The module MUST ask for one contents GET only when the local repository does not hold the cited commit.

Scenario: A commit outside the clone costs one request

  • WHEN A test verifies a citation at a commit that the local repository does not hold, with a contents function that returns the file text
  • THEN The test asserts the verification result and that the contents function was called exactly once.

Requirement: Re-anchor only a unique span

The module SHALL re-anchor a stale citation to the one place where its span text occurs, and MUST refuse a span text with zero matches or two or more matches.

Scenario: One match moves; zero and two matches refuse

  • WHEN A test re-anchors a span whose text occurs once at a new line range, then one whose text occurs zero times, then one whose text occurs twice
  • THEN The test asserts the new line range for the one match and a refusal with its reason for the zero-match case and the two-match case.

Requirement: Permalink at a commit

The module SHALL render a permalink that names the cited commit, the path and the first and the last line of the span.

Scenario: The permalink holds the commit and the line range

  • WHEN A test renders the permalink of a citation with a commit, a path and a line range
  • THEN The test asserts that the URL holds the commit, the path and the first and the last line.

Requirement: The stage gate delegates to the module

src/devops_cli/roadmap/stage/stage_change.py SHALL call the public citation functions and MUST NOT define its own citation patterns or anchor logic.

Scenario: The gate module holds no private citation code

  • WHEN A test imports devops_cli.roadmap.stage.stage_change, replaces the public parse and re-anchor functions of the citation module with doubles, and runs one stage citation check
  • THEN The test asserts that the gate module has no attribute _LINE_CITATION, _SYMBOL_CITATION, _anchored_line or _line_citation, and that the doubles were called.

Requirement: The stage citation gate keeps its results

The stage citation gate MUST give the same result and the same message for each citation case after the move.

Scenario: The existing gate tests pass with no edit

  • WHEN The existing test suite of the stage citation gate runs with no edit to its files
  • THEN The test run passes and each case gives its earlier result and message.

Existing Tool Considered

The alternatives check of 2026-11-19 gives the verdict none with no tool, as the alternatives block in the issue body records it. The block holds the searches and the list of what no tool covers. No open, maintained tool covers this need, so the change builds project code on git show <sha>:<path> and hashlib. Written cost: about 150 lines of module code plus tests, about 60k tokens and 2 hours to build, keep and fix.

Key questions

  • Where does the shared citation module live?: A top-level module src/devops_cli/citation.py. Both devops_cli.roadmap.stage and devops_cli.ai import it, with no import from ai to roadmap or the reverse. (best practice) [sources: src/devops_cli/roadmap/stage/stage_change.py:1749, src/devops_cli/ai/kb.py:614, CONTEXT.md]
    • Chosen: Top-level module src/devops_cli/citation.py. About 150 lines fit one module, and both roadmap.stage and ai import it with no cycle between them.
    • Rejected: Package src/devops_cli/citations/. One module holds the code, and a package adds files that no item needs.
    • Rejected: Keep it in src/devops_cli/roadmap/stage/. The brain code would import from the stage job, so brain add and repair would depend on roadmap.stage.
  • Which fields does the structured citation of ADR 0025 hold?: {path, commit, lines, span_hash, doc_class, generated_from?, adr_status?, as_of}. commit is a first-parent commit of main, lines is the first and the last line, and span_hash is a hash of the span text. [sources: docs/adr/0025-generated-documents-stay-in-git-and-the-project-brain-cites-them.md:53-61]
  • How is the span hash computed?: SHA-256 of the span text in UTF-8, hexadecimal, cut to the first CONST_KB_SHA_DIGEST_LENGTH characters, which is 16. src/devops_cli/ai/kb.py:614 computes the knowledge base hash the same way. [sources: src/devops_cli/ai/kb.py:614, src/devops_cli/config/constants.py:5604]
  • Which Python type holds a parsed citation?: A frozen dataclass for each citation form, with the fields of the form and no free-form dictionary. The stage gate already returns a _Citation type. (best practice) [sources: src/devops_cli/roadmap/stage/stage_change.py:1769]
    • Chosen: Frozen dataclass. It gives named fields, an equality check in tests and no dependency.
    • Rejected: Plain dictionary. A dictionary hides the field names, and no type checker finds a missing key.
    • Rejected: Pydantic model. It adds a dependency for a value that no input parses.
  • How does the module verify a citation at a commit?: It runs git show <sha>:<path> in the repository and reads the text at that commit, with 0 GitHub requests. It falls back to one contents GET only when the local repository does not hold the commit. (best practice) [sources: docs/adr/0025-generated-documents-stay-in-git-and-the-project-brain-cites-them.md:57, CONTEXT.md]
    • Chosen: git show first, one contents GET as fallback. The cited commit is a first-parent commit of main, which the clone holds, so most checks cost no request.
    • Rejected: Contents GET only. Each citation costs one request, and the rate limit applies to the brain runs.
    • Rejected: Read the working tree. The working tree holds the head text, not the text at the cited commit, so a stale citation would look valid.
  • When does the module re-anchor a stale citation?: Only when the span text occurs exactly once in the file at the head commit. A span text with zero matches or two or more matches is refused with a reason. (best practice) [sources: docs/adr/0025-generated-documents-stay-in-git-and-the-project-brain-cites-them.md:57, src/devops_cli/roadmap/stage/stage_change.py:1749]
    • Chosen: Exact span text with exactly one match. ADR 0025 moves a stale citation whose text matches, and a second match makes the move a guess.
    • Rejected: First match. The first of two equal spans is arbitrary, so the brain can cite the wrong text.
    • Rejected: Nearest span by similarity. A fuzzy move can cite text that does not support the entry, and no test can fix a wrong threshold.
  • Does the module validate doc_class and adr_status?: No. The module carries doc_class and adr_status as strings. The brain sets them from the docs inventory and from devops_cli.roadmap.adr, and the module reads neither. (best practice) [sources: docs/adr/0025-generated-documents-stay-in-git-and-the-project-brain-cites-them.md:53-61]
    • Chosen: Carry doc_class and adr_status as strings. The module then has no dependency on the docs inventory or the ADR reader.
    • Rejected: Validate doc_class against the docs inventory. It couples the stage gate to the docs inventory, which the gate does not read.
  • Does the stage gate keep its behavior after the move to the module?: Yes. _line_citation, _anchored_line, _LINE_CITATION and _SYMBOL_CITATION become calls to the public functions. Each result and message of the gate stays the same. (best practice) [sources: src/devops_cli/roadmap/stage/stage_change.py:207, src/devops_cli/roadmap/stage/stage_change.py:210, src/devops_cli/roadmap/stage/stage_change.py:1749, src/devops_cli/roadmap/stage/stage_change.py:1769]
    • Chosen: Thin delegation with the same results. The gate rule is delivered and tested, and this item must not change it.
    • Rejected: Rewrite the gate on the new module. A behavior change in the gate belongs to its own item and would break its tests.
  • Does the pull request comment on feat(roadmap): the question resolver answers with verified sources #4808, feat(ai): give every agent brain tools through MCP, CLI and skill #4671 and feat(roadmap): run brain repair, consolidate and evaluate daily #4687?: No. The pull request changes the module and its callers. The module docstring names its readers, and a comment after the merge names the module on the three items. (best practice) [sources: AGENTS.md]
    • Chosen: Docstring now, comment after the merge. AGENTS.md rule 8 forbids a change that only updates tracking.
    • Rejected: Three comments from the pull request. The pull request would carry a change that only updates tracking.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/feature
    source/agent
    scope/roadmap
    status/new
    effort/low
    value/high
    on Nov 19, 2026
  2. dan-petty commented on Nov 19, 2026

    @dan-petty
    OwnerAuthor
  3. added
    priority/p2-medium
    on Nov 19, 2026
  4. devops-cli-service commented on Nov 19, 2026

    @devops-cli-service
    Contributor

    Next step: the alternatives check records a verdict. See the section Proposed design of the body.

  5. devops-cli-service commented on Nov 19, 2026

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.

    Searches (10)

    Searches run:

    • open source tool verify code citations against git commit line numbers re-anchor, results: 5
    • python library parse code citation permalink to git commit line range, results: 5
    • sphinx literalinclude code snippet check stale line numbers, results: 5
    • gitpermalink tool link source code lines to commit, results: 5
    • sourcegraph code snippets stay in sync documentation github, results: 5
    • "citation" library "span hash" verify snippet drift documentation code, results: 5
    • github permalink generator CLI commit line range tool, results: 5
    • embedme verify embedded code snippets in markdown against source, results: 5
    • "code citation" verification tool git repository commit sha snippet match, results: 5
    • tool that keeps documentation code references in sync with source and detects drift, results: 5

    Sources:

  6. added
    status/ready
    and removed
    status/new
    on Nov 19, 2026
  7. devops-cli-service commented on Nov 19, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See the section Proposed design of the body.

  8. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 25, 2026
  9. removed
    status/in-review
    owner/agent
    on Nov 25, 2026
  10. devops-cli-service commented on Nov 25, 2026

    @devops-cli-service
    Contributor
  11. devops-cli-service commented on Nov 25, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-25 06:00 UTC

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/lowpriority/p2-mediumscope/roadmapsource/agenttype/featurevalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions