Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /promptdriven_pdd /pdd /commands /modify.py
| from __future__ import annotations | |
| import os | |
| import re | |
| import sys | |
| from pathlib import Path | |
| from typing import Optional, Tuple, Any | |
| import click | |
| from rich.console import Console | |
| # Relative imports from parent package | |
| from ..split_main import split_main | |
| from ..agentic_split import run_agentic_split | |
| from ..change_main import change_main | |
| from ..agentic_change import run_agentic_change | |
| from ..update_main import update_main | |
| from ..track_cost import track_cost | |
| from ..core.errors import handle_error | |
| from ..core.utils import echo_model_line | |
| from ..operation_log import log_operation | |
| from ..evidence_manifest import write_evidence_manifest | |
| from ..prompt_gate import ( | |
| maybe_run_workflow_prompt_gate, | |
| parse_prompt_gate_block_exit, | |
| resolve_prompt_gate_project_root, | |
| ) | |
| console = Console() | |
| _GITHUB_ISSUE_RE = re.compile( | |
| r"^(?:https?://)?(?:www\.)?github\.com/[^/]+/[^/]+/issues/\d+(?:[/?#].*)?$" | |
| ) | |
| def _is_github_issue_url(value: str) -> bool: | |
| """Return True when value is a GitHub issue URL.""" | |
| return bool(_GITHUB_ISSUE_RE.match(value.strip())) | |
| def split( | |
| ctx: click.Context, | |
| args: Tuple[str, ...], | |
| legacy: bool, | |
| output_sub: Optional[str], | |
| output_modified: Optional[str], | |
| diagnose: bool, | |
| propose_only: bool, | |
| delete_dead: bool, | |
| force_split: bool, | |
| no_verify: bool, | |
| skip_regen_gate: bool, | |
| experimental_language: bool, | |
| timeout_adder: float, | |
| no_github_state: bool, | |
| intent: Optional[str], | |
| no_phase_extraction: bool, | |
| strangler: bool, | |
| max_cost: Optional[float], | |
| ) -> Optional[Tuple[Any, float, str]]: | |
| """ | |
| Split large dev units into smaller, more manageable ones. | |
| \b | |
| Agentic Mode (default): | |
| pdd split TARGET_FILE | |
| \b | |
| Legacy Mode (--legacy): | |
| pdd split --legacy INPUT_PROMPT INPUT_CODE EXAMPLE_CODE | |
| """ | |
| ctx.ensure_object(dict) | |
| try: | |
| quiet = ctx.obj.get("quiet", False) | |
| verbose = ctx.obj.get("verbose", False) | |
| if legacy: | |
| # Legacy mode: 3 positional args required | |
| if len(args) != 3: | |
| raise click.UsageError( | |
| "Legacy mode requires 3 arguments: INPUT_PROMPT INPUT_CODE EXAMPLE_CODE" | |
| ) | |
| for arg in args: | |
| if not Path(arg).exists(): | |
| raise click.UsageError(f"File not found: {arg}") | |
| result_data, total_cost, model_name = split_main( | |
| ctx, | |
| input_prompt_file=args[0], | |
| input_code_file=args[1], | |
| example_code_file=args[2], | |
| output_sub=output_sub, | |
| output_modified=output_modified, | |
| ) | |
| return result_data, total_cost, model_name | |
| else: | |
| # Agentic mode: 1 positional arg required | |
| if len(args) != 1: | |
| raise click.UsageError( | |
| "Agentic mode requires exactly 1 argument: TARGET_FILE" | |
| ) | |
| target_file = args[0] | |
| success, message, cost, model, changed_files = run_agentic_split( | |
| target_file=target_file, | |
| verbose=verbose, | |
| quiet=quiet, | |
| timeout_adder=timeout_adder, | |
| use_github_state=not no_github_state, | |
| diagnose_only=diagnose, | |
| propose_only=propose_only, | |
| delete_dead=delete_dead, | |
| force_split=force_split, | |
| no_verify=no_verify, | |
| skip_regen_gate=skip_regen_gate, | |
| experimental_language=experimental_language, | |
| intent=intent, | |
| no_phase_extraction=no_phase_extraction, | |
| strangler=strangler, | |
| max_cost=max_cost, | |
| ) | |
| if not quiet: | |
| status = "Success" if success else "Failed" | |
| click.echo(f"Status: {status}") | |
| click.echo(f"Message: {message}") | |
| click.echo(f"Cost: ${cost:.4f}") | |
| echo_model_line(model) | |
| if changed_files: | |
| click.echo("Changed files:") | |
| for f in changed_files: | |
| click.echo(f" - {f}") | |
| if not success: | |
| raise click.exceptions.Exit(1) | |
| return message, cost, model | |
| except (click.Abort, click.exceptions.Exit, click.UsageError): | |
| raise | |
| except Exception as e: | |
| handle_error(e, "split", ctx.obj.get("quiet", False)) | |
| return None | |
| def change( | |
| ctx: click.Context, | |
| args: Tuple[str, ...], | |
| manual: bool, | |
| budget: float, | |
| output: Optional[str], | |
| csv: bool, | |
| timeout_adder: float, | |
| no_github_state: bool, | |
| evidence: bool, | |
| clean_restart: bool, | |
| prompt_checkup: Optional[str], | |
| no_prompt_checkup: bool, | |
| interactive: bool, | |
| apply: bool, | |
| preflight: bool, | |
| as_json: bool, | |
| ) -> Optional[Tuple[Any, float, str]]: | |
| """ | |
| Modify an input prompt file based on a change prompt or issue. | |
| Agentic Mode (default): | |
| pdd change ISSUE_URL | |
| Manual Mode (--manual): | |
| pdd change --manual CHANGE_PROMPT_FILE INPUT_CODE_FILE [INPUT_PROMPT_FILE] | |
| """ | |
| ctx.ensure_object(dict) | |
| ctx.obj["prompt_checkup"] = prompt_checkup | |
| ctx.obj["no_prompt_checkup"] = no_prompt_checkup | |
| ctx.obj["interactive"] = interactive | |
| ctx.obj["apply"] = apply | |
| if preflight: | |
| from ..continuous_sync import build_report | |
| import json as _json | |
| if as_json: | |
| ctx.obj["_suppress_result_summary"] = True | |
| click.echo( | |
| _json.dumps( | |
| build_report(consumer="change-preflight"), | |
| indent=2, | |
| sort_keys=True, | |
| ) | |
| ) | |
| else: | |
| report = build_report(consumer="change-preflight") | |
| summary = report["summary"] | |
| click.echo( | |
| "metadata_stale={metadata_stale} conflicts={conflicts} " | |
| "unbaselined={unbaselined} failures={failures}".format(**summary) | |
| ) | |
| return None | |
| if clean_restart and manual: | |
| raise click.UsageError( | |
| "--clean-restart is only valid in agentic mode and cannot be used with --manual" | |
| ) | |
| try: | |
| # Set budget in context for manual mode usage | |
| ctx.obj["budget"] = budget | |
| quiet = ctx.obj.get("quiet", False) | |
| verbose = ctx.obj.get("verbose", False) | |
| if manual: | |
| # Manual Mode Validation and Execution | |
| if csv: | |
| # CSV Mode: Expecting CSV_FILE and CODE_DIRECTORY (no input_prompt) | |
| if len(args) == 3: | |
| raise click.UsageError("Cannot use --csv and specify an INPUT_PROMPT_FILE simultaneously.") | |
| if len(args) != 2: | |
| raise click.UsageError("CSV mode requires 2 arguments: CSV_FILE CODE_DIRECTORY") | |
| change_file, input_code = args | |
| input_prompt = None | |
| # CSV mode requires input_code to be a directory | |
| if not Path(input_code).is_dir(): | |
| raise click.UsageError("INPUT_CODE must be a directory when using --csv") | |
| else: | |
| # Standard Manual Mode: Expecting 2 or 3 arguments | |
| if len(args) == 3: | |
| change_file, input_code, input_prompt = args | |
| # Non-CSV mode requires input_code to be a file, not a directory | |
| if Path(input_code).is_dir(): | |
| raise click.UsageError("INPUT_CODE must be a file when not using --csv") | |
| elif len(args) == 2: | |
| change_file, input_code = args | |
| input_prompt = None | |
| # Without CSV mode, input_prompt_file is required | |
| raise click.UsageError("INPUT_PROMPT_FILE is required when not using --csv") | |
| else: | |
| raise click.UsageError( | |
| "Manual mode requires 3 arguments: CHANGE_PROMPT INPUT_CODE INPUT_PROMPT" | |
| ) | |
| # Validate file existence | |
| if not Path(change_file).exists(): | |
| raise click.UsageError(f"Change file not found: {change_file}") | |
| if not Path(input_code).exists(): | |
| raise click.UsageError(f"Input code path not found: {input_code}") | |
| if input_prompt and not Path(input_prompt).exists(): | |
| raise click.UsageError(f"Input prompt file not found: {input_prompt}") | |
| # Call change_main | |
| result, cost, model = change_main( | |
| ctx=ctx, | |
| change_prompt_file=change_file, | |
| input_code=input_code, | |
| input_prompt_file=input_prompt, | |
| output=output, | |
| use_csv=csv, | |
| budget=budget | |
| ) | |
| gate_exit = ( | |
| parse_prompt_gate_block_exit(result) | |
| if isinstance(result, str) | |
| else None | |
| ) | |
| if gate_exit is not None: | |
| raise click.exceptions.Exit(gate_exit) | |
| if evidence: | |
| write_evidence_manifest( | |
| command="pdd change", | |
| prompt_file=input_prompt, | |
| output_files=[output] if output else (), | |
| model=model, | |
| cost_usd=cost, | |
| temperature=ctx.obj.get("temperature", 0.0), | |
| ) | |
| return result, cost, model | |
| else: | |
| # Agentic Mode Validation and Execution | |
| if len(args) != 1: | |
| raise click.UsageError("Agentic mode requires exactly 1 argument: ISSUE_URL") | |
| issue_url = args[0] | |
| if clean_restart and not _is_github_issue_url(issue_url): | |
| raise click.UsageError( | |
| "--clean-restart can only be used with an agentic GitHub issue URL." | |
| ) | |
| previous_no_github_state = os.environ.get("PDD_NO_GITHUB_STATE") | |
| if no_github_state: | |
| os.environ["PDD_NO_GITHUB_STATE"] = "1" | |
| try: | |
| # Call run_agentic_change | |
| success, message, cost, model, changed_files = run_agentic_change( | |
| issue_url=issue_url, | |
| verbose=verbose, | |
| quiet=quiet, | |
| timeout_adder=timeout_adder, | |
| use_github_state=not no_github_state, | |
| clean_restart=clean_restart, | |
| reasoning_time=ctx.obj.get("time") if ctx.obj.get("time_explicit") else None, | |
| ) | |
| finally: | |
| if no_github_state: | |
| if previous_no_github_state is None: | |
| os.environ.pop("PDD_NO_GITHUB_STATE", None) | |
| else: | |
| os.environ["PDD_NO_GITHUB_STATE"] = previous_no_github_state | |
| # Display results using click.echo as requested | |
| if not quiet: | |
| status = "Success" if success else "Failed" | |
| click.echo(f"Status: {status}") | |
| click.echo(f"Message: {message}") | |
| click.echo(f"Cost: ${cost:.4f}") | |
| echo_model_line(model) | |
| if changed_files: | |
| click.echo("Changed files:") | |
| for f in changed_files: | |
| click.echo(f" - {f}") | |
| if not success: | |
| raise click.exceptions.Exit(1) | |
| should_continue, gate_exit = maybe_run_workflow_prompt_gate( | |
| changed_files, | |
| cli_prompt_checkup=prompt_checkup, | |
| no_prompt_checkup=no_prompt_checkup, | |
| project_root=resolve_prompt_gate_project_root(changed_files or []), | |
| quiet=quiet, | |
| interactive=interactive, | |
| apply=apply, | |
| ) | |
| if not should_continue: | |
| raise click.exceptions.Exit(gate_exit) | |
| if evidence: | |
| write_evidence_manifest( | |
| command="pdd change", | |
| output_files=changed_files, | |
| model=model, | |
| cost_usd=cost, | |
| temperature=ctx.obj.get("temperature", 0.0), | |
| basename="agentic-change", | |
| ) | |
| return message, cost, model | |
| except (click.Abort, click.exceptions.Exit, click.UsageError): | |
| raise | |
| except Exception as e: | |
| handle_error(e, "change", ctx.obj.get("quiet", False)) | |
| return None | |
| def update( | |
| ctx: click.Context, | |
| files: Tuple[str, ...], | |
| all_: bool, | |
| extensions: Optional[str], | |
| directory: Optional[str], | |
| git: bool, | |
| output: Optional[str], | |
| simple: bool, | |
| base_branch: str, | |
| budget: Optional[float], | |
| dry_run: bool, | |
| as_json: bool, | |
| sync_metadata: bool, | |
| ) -> Optional[Tuple[Any, float, str]]: | |
| """ | |
| Update the original prompt file based on code changes. | |
| Repo-wide mode (no args, or --all): Scan entire repo. | |
| Single-file mode (1 arg): Update prompt for specific code file. | |
| """ | |
| ctx.ensure_object(dict) | |
| # Validate argument counts before try/except so UsageError propagates naturally | |
| if len(files) == 2 and not git: | |
| raise click.UsageError( | |
| "Two arguments require --git flag: pdd update --git <prompt> <modified_code>" | |
| ) | |
| if len(files) == 3 and git: | |
| raise click.UsageError( | |
| "Cannot use --git with 3 arguments (--git and original_code are mutually exclusive)" | |
| ) | |
| if len(files) > 3: | |
| raise click.UsageError("Too many arguments. Max 3: <prompt> <modified_code> <original_code>") | |
| if all_ and len(files) > 0: | |
| raise click.UsageError( | |
| "Cannot combine --all with file paths; use repository-wide mode with no arguments or only --all." | |
| ) | |
| if budget is not None and budget <= 0: | |
| raise click.UsageError("--budget must be a positive number") | |
| try: | |
| # Handle argument counts per modify_python.prompt spec (aligned with README) | |
| if len(files) == 0 or all_: | |
| # Repo-wide mode | |
| is_repo_mode = True | |
| input_prompt_file = None | |
| modified_code_file = None | |
| input_code_file = None | |
| elif len(files) == 1: | |
| # Regeneration mode: just the code file | |
| is_repo_mode = False | |
| input_prompt_file = None | |
| modified_code_file = files[0] | |
| input_code_file = None | |
| elif len(files) == 2: | |
| # Git-based update: prompt + modified_code (--git guaranteed by pre-validation) | |
| is_repo_mode = False | |
| input_prompt_file = files[0] | |
| modified_code_file = files[1] | |
| input_code_file = None | |
| elif len(files) == 3: | |
| # Manual update: prompt + modified_code + original_code (no --git guaranteed) | |
| is_repo_mode = False | |
| input_prompt_file = files[0] | |
| modified_code_file = files[1] | |
| input_code_file = files[2] | |
| # Validate mode-specific options | |
| if is_repo_mode: | |
| # Repo-wide mode: --git and --output are not allowed | |
| if git: | |
| raise click.UsageError( | |
| "Cannot use --git in repository-wide mode" | |
| ) | |
| if output: | |
| raise click.UsageError( | |
| "Cannot use --output in repository-wide mode" | |
| ) | |
| if as_json and not dry_run: | |
| raise click.UsageError("--json is only supported with update --all --dry-run.") | |
| else: | |
| # File modes: --extensions, --directory, and --base-branch are not allowed | |
| if extensions: | |
| raise click.UsageError( | |
| "--extensions can only be used in repository-wide mode" | |
| ) | |
| if directory: | |
| raise click.UsageError( | |
| "--directory can only be used in repository-wide mode" | |
| ) | |
| if base_branch != "main": | |
| raise click.UsageError( | |
| "--base-branch can only be used in repository-wide mode" | |
| ) | |
| if dry_run: | |
| raise click.UsageError( | |
| "--dry-run is only valid in repository-wide mode (no file arguments, or use --all)." | |
| ) | |
| if as_json: | |
| raise click.UsageError( | |
| "--json is only valid in repository-wide dry-run mode." | |
| ) | |
| if budget is not None: | |
| raise click.UsageError( | |
| "--budget is only valid in repository-wide mode (no file arguments, or use --all)." | |
| ) | |
| if is_repo_mode and dry_run and as_json: | |
| from ..continuous_sync import build_report | |
| import json as _json | |
| ctx.obj["_suppress_result_summary"] = True | |
| report = build_report(consumer="update-dry-run") | |
| click.echo(_json.dumps(report, indent=2, sort_keys=True)) | |
| return None | |
| # Call update_main with correct parameters | |
| ret = update_main( | |
| ctx=ctx, | |
| input_prompt_file=input_prompt_file, | |
| modified_code_file=modified_code_file, | |
| input_code_file=input_code_file, | |
| output=output, | |
| use_git=git, | |
| repo=is_repo_mode, | |
| extensions=extensions, | |
| directory=directory, | |
| simple=simple, | |
| base_branch=base_branch, | |
| budget=budget, | |
| dry_run=dry_run, | |
| sync_metadata=sync_metadata, | |
| ) | |
| if ret is None: | |
| # In single-file mode `update_main` returns None ONLY on failure | |
| # (input error, git-history/provider failure, invalid output). A bare | |
| # `return None` here makes Click exit 0, so a subprocess caller that | |
| # only sees the exit code — e.g. the change orchestrator's Step 8.5 | |
| # preflight drift-heal running `pdd update --sync-metadata --git ...` | |
| # — would record an unchanged, still-stale prompt as "healed" and | |
| # reason Step 9 from stale intent. Surface a non-zero exit so that | |
| # boundary fails closed. Repo-wide mode keeps returning None (exit 0) | |
| # because there None is also the legitimate "everything in sync" | |
| # no-op, not a failure (Codex review, PR #1998). | |
| if not is_repo_mode: | |
| raise click.exceptions.Exit(1) | |
| return None | |
| return ret | |
| except (click.Abort, click.UsageError, click.exceptions.Exit): | |
| # click.exceptions.Exit carries an intentional non-zero exit code | |
| # (raised e.g. when sync_metadata finalization fails — see | |
| # update_main). Letting `except Exception` swallow it would silently | |
| # convert it to exit 0 and re-introduce the bug fixed for #871. | |
| raise | |
| except Exception as e: | |
| handle_error(e, "update", ctx.obj.get("quiet", False)) | |
| return None | |
Xet Storage Details
- Size:
- 25.1 kB
- Xet hash:
- fb552d7ace7ac99c0e67538fc4d51db67d59c5a2e52c23c381c758d95ace2f87
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.