|
| 1 | +"""Socratic inquiry and knowledge gap formulator for metacognitive task self-assessment.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from devops_cli.ai.research.models import ( |
| 9 | + SocraticInquiryResult, |
| 10 | +) |
| 11 | +from devops_cli.ai.task_loader import load_task_prompt |
| 12 | +from devops_cli.config.defaults import DEFAULT_AI_SOCRATIC_MAX_PROBES |
| 13 | +from devops_cli.config.settings import get_llm_client |
| 14 | + |
| 15 | +_DEFAULT_SOCRATIC_SYSTEM = ( |
| 16 | + "You are a metacognitive Socratic inquiry engine for software engineering tasks. " |
| 17 | + "Analyze the task to identify gaps, ambiguities, and underspecified contracts, " |
| 18 | + "and formulate concise, targeted clarifying questions and concrete code lookup instructions." |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def _build_user_prompt( |
| 23 | + task: str, |
| 24 | + context: str | None = None, |
| 25 | + repo_root: Path | None = None, |
| 26 | + max_probes: int = DEFAULT_AI_SOCRATIC_MAX_PROBES, |
| 27 | +) -> str: |
| 28 | + """Compose user prompt containing task, optional context, and probe constraints.""" |
| 29 | + sections = [ |
| 30 | + f"Task Description:\n{task}", |
| 31 | + f"Max Probes Constraint: Limit clarifying questions to at most {max_probes} " |
| 32 | + f"and code lookups to at most {max_probes}.", |
| 33 | + ] |
| 34 | + if context: |
| 35 | + sections.append(f"Context / Instructions:\n{context}") |
| 36 | + if repo_root: |
| 37 | + sections.append(f"Repository Root:\n{repo_root}") |
| 38 | + return "\n\n".join(sections) |
| 39 | + |
| 40 | + |
| 41 | +def _bound_inquiry_result( |
| 42 | + result: SocraticInquiryResult, |
| 43 | + original_task: str, |
| 44 | + max_probes: int, |
| 45 | +) -> SocraticInquiryResult: |
| 46 | + """Enforce bounded probe ceiling on questions and code lookup instructions.""" |
| 47 | + bounded_questions = result.questions[:max_probes] |
| 48 | + bounded_lookups = result.lookups[:max_probes] |
| 49 | + return SocraticInquiryResult( |
| 50 | + task=original_task, |
| 51 | + summary=result.summary, |
| 52 | + gaps=result.gaps, |
| 53 | + questions=bounded_questions, |
| 54 | + lookups=bounded_lookups, |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def render_socratic_markdown(result: SocraticInquiryResult) -> str: |
| 59 | + """Render Socratic inquiry results as clean GitHub-flavored markdown.""" |
| 60 | + lines: list[str] = [ |
| 61 | + "# Socratic Inquiry & Knowledge Gap Analysis", |
| 62 | + "", |
| 63 | + f"**Task**: {result.task}", |
| 64 | + "", |
| 65 | + ] |
| 66 | + if result.summary: |
| 67 | + lines.extend(["## Metacognitive Summary", "", result.summary, ""]) |
| 68 | + |
| 69 | + lines.extend(["## Knowledge Gaps & Ambiguities", ""]) |
| 70 | + if not result.gaps: |
| 71 | + lines.extend(["*No knowledge gaps identified.*", ""]) |
| 72 | + else: |
| 73 | + lines.extend( |
| 74 | + [ |
| 75 | + "| Type | Description | Rationale |", |
| 76 | + "| :--- | :--- | :--- |", |
| 77 | + ] |
| 78 | + ) |
| 79 | + for gap in result.gaps: |
| 80 | + desc = gap.description.replace("|", "\\|") |
| 81 | + rat = gap.rationale.replace("|", "\\|") |
| 82 | + lines.append(f"| `{gap.type}` | {desc} | {rat} |") |
| 83 | + lines.append("") |
| 84 | + |
| 85 | + lines.extend(["## Targeted Clarifying Questions", ""]) |
| 86 | + if not result.questions: |
| 87 | + lines.extend(["*No clarifying questions required.*", ""]) |
| 88 | + else: |
| 89 | + for idx, q in enumerate(result.questions, start=1): |
| 90 | + lines.extend( |
| 91 | + [ |
| 92 | + f"{idx}. **{q.question}**", |
| 93 | + f" - *Target Gap*: {q.target_gap}", |
| 94 | + f" - *Rationale*: {q.rationale}", |
| 95 | + ] |
| 96 | + ) |
| 97 | + lines.append("") |
| 98 | + |
| 99 | + lines.extend(["## Concrete Code Lookup Instructions", ""]) |
| 100 | + if not result.lookups: |
| 101 | + lines.extend(["*No code lookup instructions required.*", ""]) |
| 102 | + else: |
| 103 | + for idx, lk in enumerate(result.lookups, start=1): |
| 104 | + lines.extend( |
| 105 | + [ |
| 106 | + f"{idx}. `{lk.target}` ({lk.lookup_type})", |
| 107 | + f" - *Target Gap*: {lk.target_gap}", |
| 108 | + f" - *Rationale*: {lk.rationale}", |
| 109 | + ] |
| 110 | + ) |
| 111 | + lines.append("") |
| 112 | + |
| 113 | + return "\n".join(lines).strip() + "\n" |
| 114 | + |
| 115 | + |
| 116 | +class SocraticFormulator: |
| 117 | + """Metacognitive analyzer formulating knowledge gaps, questions, and lookups.""" |
| 118 | + |
| 119 | + def __init__( |
| 120 | + self, |
| 121 | + client: Any | None = None, |
| 122 | + max_probes: int = DEFAULT_AI_SOCRATIC_MAX_PROBES, |
| 123 | + ) -> None: |
| 124 | + self.client = client |
| 125 | + self.max_probes = max_probes |
| 126 | + |
| 127 | + def formulate( |
| 128 | + self, |
| 129 | + task: str, |
| 130 | + context: str | None = None, |
| 131 | + repo_root: Path | None = None, |
| 132 | + ) -> SocraticInquiryResult: |
| 133 | + """Analyze task and context to formulate knowledge gaps and minimal probes.""" |
| 134 | + stripped_task = task.strip() |
| 135 | + if not stripped_task: |
| 136 | + raise ValueError("Task description must not be empty.") |
| 137 | + |
| 138 | + client = self.client or get_llm_client(task="research") |
| 139 | + system_prompt = load_task_prompt("socratic_inquiry.md") or _DEFAULT_SOCRATIC_SYSTEM |
| 140 | + user_prompt = _build_user_prompt( |
| 141 | + stripped_task, |
| 142 | + context=context, |
| 143 | + repo_root=repo_root, |
| 144 | + max_probes=self.max_probes, |
| 145 | + ) |
| 146 | + |
| 147 | + res = client.chat_structured( |
| 148 | + system=system_prompt, |
| 149 | + prompt=user_prompt, |
| 150 | + schema=SocraticInquiryResult, |
| 151 | + ) |
| 152 | + return _bound_inquiry_result(res, stripped_task, self.max_probes) |
0 commit comments