command.py

clew/propose/command.py

`clew propose` — write a starter `.clew.yaml` draft for a repo.

_configure_logging

None clew.propose.command._configure_logging(bool verbose)

Send logging to stderr so the draft on stdout stays parseable.

clew/propose/command.py:213

Source · 12 lines
def _configure_logging(verbose: bool) -> None:
    """Deliberately NOT the pipeline's rich handler, which writes to stdout — a
    single INFO line there lands inside the emitted YAML.

    @brief Configure stderr logging for the proposer.
    @version 1
    """
    logging.basicConfig(
        level=logging.INFO if verbose else logging.WARNING,
        format="%(levelname)s %(message)s",
        stream=sys.stderr,
    )

1 more lines, not shown

Activity · 1 steps
lines 220–224logging.basicConfig( level=logging.INFO if verbose else logging.WARNING, format="%(levelname)s %(message)s", stream=sys.stderr, )

Flows

_emit

None clew.propose.command._emit(str text, Path|None target)

Write the draft to a file or to stdout.

clew/propose/command.py:177

Source · 10 lines
def _emit(text: str, target: Path | None) -> None:
    """@brief Emit the draft, reporting a file write on stderr."""
    if target is None:
        sys.stdout.write(text)
        return
    target.parent.mkdir(parents=True, exist_ok=True)
    target.write_text(text, encoding="utf-8")
    print(f"wrote {target} ({len(text.splitlines())} lines, all comments)", file=sys.stderr)

Activity · 6 steps
lines 179–179◇ if target is Nonelines 180–180sys.stdout.write(text)lines 181–181returnlines 182–182target.parent.mkdir(parents=True, exist_ok=True)lines 183–183target.write_text(text, encoding="utf-8")lines 184–184print(f"wrote {target} ({len(text.splitlines())} lines, all comments)", file=sys.stderr)

Flows

_refusal

str clew.propose.command._refusal(Path|None target, bool force)

Why the output path must not be written, if it must not.

clew/propose/command.py:152

Source · 20 lines
def _refusal(target: Path | None, force: bool) -> str:
    """Checked BEFORE the analysis runs: a scan of a large tree takes tens of
    seconds, and refusing after it has finished wastes the wait for an outcome
    that was decidable up front.

    @brief Decide whether the requested output path may be written.
    @version 1
    """
    if target is None or force or not target.exists():
        return ""
    return (
        f"refusing to overwrite {target} — it already exists.\n"
        "That file is the repo's own authoritative declaration; replacing it with a "
        "draft would DISABLE every convention it declares while the build kept "
        "succeeding.\n"
        "Write elsewhere, review, and merge by hand — or pass --force if you really "
        "mean to replace it."
    )

Activity · 3 steps
lines 160–160◇ if target is None or force or not target.exists()lines 161–161return ""lines 162–169return ( f"refusing to overwrite {target} — it already exists.\n" "That file is the repo's own authoritative declaration; replacing it with a " "draft would DISABLE every convention it declares while the build kept " "succeeding.\n" "Write elsewhere, review, and merge by hand — or pass --force if you really " "mean to replace it." )

Flows

_resolve_db

Path | None clew.propose.command._resolve_db(str|None explicit, Path repo_root)

Resolve the index to measure candidates against.

clew/propose/command.py:194

Source · 15 lines
def _resolve_db(explicit: str | None, repo_root: Path) -> Path | None:
    """Discovery order: the explicit flag, then the MCP server's registry (the
    database an agent would already have built for this repo), then
    `<repo>/clew.db`. Never invented — a nonexistent path is reported as "no
    index" so the gated sections refuse, rather than as a measurement of nothing.

    @brief Find the database a proposal can be measured against.
    @version 2
    """
    ## 2.1 (clew#74): one rule, shared with `clew guard impact` (`clew.indexlocate`).
    from ..indexlocate import find_index

    return find_index(repo_root, explicit)

Activity · 2 steps
lines 204–204from ..indexlocate import find_indexlines 206–206return find_index(repo_root, explicit)

Flows

build_parser

argparse.ArgumentParser clew.propose.command.build_parser()

Build the clew propose argument parser.

clew/propose/command.py:51

Source · 60 lines
def build_parser() -> argparse.ArgumentParser:
    """@brief Construct the proposer's CLI parser."""
    parser = argparse.ArgumentParser(
        prog="clew propose",
        description="Propose a starter .clew.yaml for a repo, from evidence.",
        epilog=_EPILOG,
    )
    parser.add_argument(
        "--repo-root",
        default=".",
        help="Repo to analyse (default: the current directory).",
    )
    parser.add_argument(
        "--db",
        help=(
            "Index to measure candidates against. Defaults to the registered "
            "database for this repo, then <repo-root>/clew.db. Without one, every "
            "dry-run-gated section refuses to propose."
        ),
    )
    parser.add_argument(
        "-o",
        "--output",
        help=(
            f"Write the draft here instead of stdout (typically <repo>/{DECLARATION_NAME}). "
            "An existing file is REFUSED unless --force is also given."
        ),
    )
    parser.add_argument(
        "--force",
        action="store_true",
        help=(
            "Overwrite the --output file if it exists. The repo's own declaration is "
            "authoritative, so this is never implied."
        ),
    )
    parser.add_argument(
        "--no-dry-run",
        action="store_true",
        help=(
            "Do not measure candidates against the index. FAIL-CLOSED: the gated "
            "sections then propose NOTHING, because an unmeasured shared-key "
            "declaration can mint a blob of fabricated causal edges."
        ),
    )
    parser.add_argument(
        "--ignore-declaration",
        action="store_true",
        help=(
            "Detect as if the repo declared nothing, so entries its .clew.yaml "
            "already covers are proposed again instead of being refused as covered. "
            "Use it to AUDIT an existing declaration against the evidence. The index "
            "scope is still derived normally — every measured number must come from "
            "the scope the build actually uses."
        ),
    )
    parser.add_argument("--verbose", action="store_true", help="Verbose logging (to stderr).")
    return parser

Activity · 9 steps
lines 53–57parser = argparse.ArgumentParser( prog="clew propose", description="Propose a starter .clew.yaml for a repo, from evidence.", epilog=_EPILOG, )lines 58–62parser.add_argument( "--repo-root", default=".", help="Repo to analyse (default: the current directory).", )lines 63–70parser.add_argument( "--db", help=( "Index to measure candidates against. Defaults to the registered " "database for this repo, then <repo-root>/clew.db. Without one, every " "dry-run-gated section refuses to propose." ), )lines 71–78parser.add_argument( "-o", "--output", help=( f"Write the draft here instead of stdout (typically <repo>/{DECLARATION_NAME}). " "An existing file is REFUSED unless --force is also given." ), )lines 79–86parser.add_argument( "--force", action="store_true", help=( "Overwrite the --output file if it exists. The repo's own declaration is " "authoritative, so this is never implied." ), )lines 87–95parser.add_argument( "--no-dry-run", action="store_true", help=( "Do not measure candidates against the index. FAIL-CLOSED: the gated " "sections then propose NOTHING, because an unmeasured shared-key " "declaration can mint a blob of fabricated causal edges." ), )lines 96–106parser.add_argument( "--ignore-declaration", action="store_true", help=( "Detect as if the repo declared nothing, so entries its .clew.yaml " "already covers are proposed again instead of being refused as covered. " "Use it to AUDIT an existing declaration against the evidence. The index " "scope is still derived normally — every measured number must come from " "the scope the build actually uses." ), )lines 107–107parser.add_argument("--verbose", action="store_true", help="Verbose logging (to stderr).")lines 108–108return parser

Requirements

REQ-DDB-CONFIG-001

Flows

propose_main

int clew.propose.command.propose_main(list[str]|None argv=None)

Run clew propose .

clew/propose/command.py:116

Source · 30 lines
def propose_main(argv: list[str] | None = None) -> int:
    """Stdout is redirected to stderr FOR THE ANALYSIS. Every dry run re-runs a
    pipeline importer, and those render a rich progress bar onto the shared
    console — whose file is `sys.stdout`. Piping the draft (`clew propose >
    .clew.yaml`, the obvious first thing anyone does) would otherwise land
    progress bars inside the YAML, ahead of the header. Only the finished document
    reaches the real stdout.

    @brief Parse arguments, build the proposal, emit or refuse.
    @version 4
    """
    args = build_parser().parse_args(argv)
    _configure_logging(args.verbose)
    repo_root = Path(args.repo_root).expanduser().resolve()
    target = Path(args.output).expanduser().resolve() if args.output else None
    refusal = _refusal(target, args.force)
    if refusal:
        print(refusal, file=sys.stderr)
        return 2
    with contextlib.redirect_stdout(sys.stderr):
        proposal = propose(
            repo_root,
            _resolve_db(args.db, repo_root),
            dry_run=not args.no_dry_run,
            use_declaration=not args.ignore_declaration,
        )
    _emit(proposal.yaml_text, target)
    return 0

Activity · 12 steps
lines 127–127args = build_parser().parse_args(argv)lines 128–128_configure_logging(args.verbose)lines 129–129repo_root = Path(args.repo_root).expanduser().resolve()lines 130–130target = Path(args.output).expanduser().resolve() if args.output else Nonelines 131–131refusal = _refusal(target, args.force)lines 132–132◇ if refusallines 133–133print(refusal, file=sys.stderr)lines 134–134return 2lines 135–135with contextlib.redirect_stdout(sys.stderr)lines 136–141proposal = propose( repo_root, _resolve_db(args.db, repo_root), dry_run=not args.no_dry_run, use_declaration=not args.ignore_declaration, )lines 142–142_emit(proposal.yaml_text, target)lines 143–143return 0

Requirements

REQ-DDB-CONFIG-001

Flows