feat(mma): Finalize Phase 1 with AST-based outline and improved tiered selection
This commit is contained in:
15
aggregate.py
15
aggregate.py
@@ -204,7 +204,7 @@ def build_discussion_text(history: list[str]) -> str:
|
|||||||
def build_tier1_context(file_items: list[dict], screenshot_base_dir: Path, screenshots: list[str], history: list[str]) -> str:
|
def build_tier1_context(file_items: list[dict], screenshot_base_dir: Path, screenshots: list[str], history: list[str]) -> str:
|
||||||
"""
|
"""
|
||||||
Tier 1 Context: Strategic/Orchestration.
|
Tier 1 Context: Strategic/Orchestration.
|
||||||
Full content for core conductor files, summaries for others.
|
Full content for core conductor files and files with tier=1, summaries for others.
|
||||||
"""
|
"""
|
||||||
core_files = {"product.md", "tech-stack.md", "workflow.md", "tracks.md"}
|
core_files = {"product.md", "tech-stack.md", "workflow.md", "tracks.md"}
|
||||||
|
|
||||||
@@ -217,13 +217,14 @@ def build_tier1_context(file_items: list[dict], screenshot_base_dir: Path, scree
|
|||||||
path = item.get("path")
|
path = item.get("path")
|
||||||
name = path.name if path else ""
|
name = path.name if path else ""
|
||||||
|
|
||||||
if name in core_files:
|
if name in core_files or item.get("tier") == 1:
|
||||||
# Include in full
|
# Include in full
|
||||||
sections.append("### `" + (item.get("entry") or str(path)) + "`\n\n" +
|
sections.append("### `" + (item.get("entry") or str(path)) + "`\n\n" +
|
||||||
f"```{path.suffix.lstrip('.') if path.suffix else 'text'}\n{item.get('content', '')}\n```")
|
f"```{path.suffix.lstrip('.') if path.suffix else 'text'}\n{item.get('content', '')}\n```")
|
||||||
else:
|
else:
|
||||||
# Summarize
|
# Summarize
|
||||||
sections.append(summarize.summarise_file(path, item.get("content", "")))
|
sections.append("### `" + (item.get("entry") or str(path)) + "`\n\n" +
|
||||||
|
summarize.summarise_file(path, item.get("content", "")))
|
||||||
|
|
||||||
parts.append("## Files (Tier 1 - Mixed)\n\n" + "\n\n---\n\n".join(sections))
|
parts.append("## Files (Tier 1 - Mixed)\n\n" + "\n\n---\n\n".join(sections))
|
||||||
|
|
||||||
@@ -247,7 +248,7 @@ def build_tier2_context(file_items: list[dict], screenshot_base_dir: Path, scree
|
|||||||
def build_tier3_context(file_items: list[dict], screenshot_base_dir: Path, screenshots: list[str], history: list[str], focus_files: list[str]) -> str:
|
def build_tier3_context(file_items: list[dict], screenshot_base_dir: Path, screenshots: list[str], history: list[str], focus_files: list[str]) -> str:
|
||||||
"""
|
"""
|
||||||
Tier 3 Context: Execution/Worker.
|
Tier 3 Context: Execution/Worker.
|
||||||
Full content for focus_files, summaries for others.
|
Full content for focus_files and files with tier=3, summaries/skeletons for others.
|
||||||
"""
|
"""
|
||||||
parts = []
|
parts = []
|
||||||
|
|
||||||
@@ -265,7 +266,7 @@ def build_tier3_context(file_items: list[dict], screenshot_base_dir: Path, scree
|
|||||||
is_focus = True
|
is_focus = True
|
||||||
break
|
break
|
||||||
|
|
||||||
if is_focus:
|
if is_focus or item.get("tier") == 3:
|
||||||
sections.append("### `" + (entry or path_str) + "`\n\n" +
|
sections.append("### `" + (entry or path_str) + "`\n\n" +
|
||||||
f"```{path.suffix.lstrip('.') if path and path.suffix else 'text'}\n{item.get('content', '')}\n```")
|
f"```{path.suffix.lstrip('.') if path and path.suffix else 'text'}\n{item.get('content', '')}\n```")
|
||||||
else:
|
else:
|
||||||
@@ -277,9 +278,9 @@ def build_tier3_context(file_items: list[dict], screenshot_base_dir: Path, scree
|
|||||||
sections.append(f"### `{entry or path_str}` (AST Skeleton)\n\n```python\n{skeleton}\n```")
|
sections.append(f"### `{entry or path_str}` (AST Skeleton)\n\n```python\n{skeleton}\n```")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Fallback to summary if AST parsing fails
|
# Fallback to summary if AST parsing fails
|
||||||
sections.append(summarize.summarise_file(path, content))
|
sections.append(f"### `{entry or path_str}`\n\n" + summarize.summarise_file(path, content))
|
||||||
else:
|
else:
|
||||||
sections.append(summarize.summarise_file(path, content))
|
sections.append(f"### `{entry or path_str}`\n\n" + summarize.summarise_file(path, content))
|
||||||
|
|
||||||
parts.append("## Files (Tier 3 - Focused)\n\n" + "\n\n---\n\n".join(sections))
|
parts.append("## Files (Tier 3 - Focused)\n\n" + "\n\n---\n\n".join(sections))
|
||||||
|
|
||||||
|
|||||||
@@ -1,88 +1,55 @@
|
|||||||
import tree_sitter
|
import ast
|
||||||
import tree_sitter_python
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
class CodeOutliner:
|
class CodeOutliner:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.language = tree_sitter.Language(tree_sitter_python.language())
|
pass
|
||||||
self.parser = tree_sitter.Parser(self.language)
|
|
||||||
|
|
||||||
def outline(self, code: str) -> str:
|
def outline(self, code: str) -> str:
|
||||||
tree = self.parser.parse(bytes(code, "utf8"))
|
code = code.lstrip(chr(0xFEFF))
|
||||||
lines = code.splitlines()
|
try:
|
||||||
|
tree = ast.parse(code)
|
||||||
|
except SyntaxError as e:
|
||||||
|
return f"ERROR parsing code: {e}"
|
||||||
|
|
||||||
output = []
|
output = []
|
||||||
|
|
||||||
def get_docstring(node):
|
def get_docstring(node):
|
||||||
# In Python, docstring is usually the first expression statement in a block
|
doc = ast.get_docstring(node)
|
||||||
body = node.child_by_field_name("body")
|
if doc:
|
||||||
if body and body.type == "block":
|
return doc.splitlines()[0]
|
||||||
for child in body.children:
|
|
||||||
if child.type == "comment":
|
|
||||||
continue
|
|
||||||
if child.type == "expression_statement":
|
|
||||||
expr = child.children[0]
|
|
||||||
if expr.type == "string":
|
|
||||||
doc = code[expr.start_byte:expr.end_byte].strip()
|
|
||||||
# Strip quotes
|
|
||||||
if doc.startswith(('"""', "'''")):
|
|
||||||
doc = doc[3:-3]
|
|
||||||
elif doc.startswith(('"', "'")):
|
|
||||||
doc = doc[1:-1]
|
|
||||||
return doc.splitlines()[0] if doc else ""
|
|
||||||
break
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def walk(node, indent=0):
|
def walk(node, indent=0):
|
||||||
node_type = node.type
|
if isinstance(node, ast.ClassDef):
|
||||||
name = None
|
start_line = node.lineno
|
||||||
|
end_line = getattr(node, "end_lineno", start_line)
|
||||||
if node_type == "class_definition":
|
output.append(f"{' ' * indent}[Class] {node.name} (Lines {start_line}-{end_line})")
|
||||||
name_node = node.child_by_field_name("name")
|
|
||||||
if name_node:
|
|
||||||
name = code[name_node.start_byte:name_node.end_byte]
|
|
||||||
start_line = node.start_point.row + 1
|
|
||||||
end_line = node.end_point.row + 1
|
|
||||||
output.append(f"{' ' * indent}[Class] {name} (Lines {start_line}-{end_line})")
|
|
||||||
doc = get_docstring(node)
|
doc = get_docstring(node)
|
||||||
if doc:
|
if doc:
|
||||||
output.append(f"{' ' * (indent + 1)}\"\"\"{doc}\"\"\"")
|
output.append(f"{' ' * (indent + 1)}\"\"\"{doc}\"\"\"")
|
||||||
|
for item in node.body:
|
||||||
|
walk(item, indent + 1)
|
||||||
|
|
||||||
elif node_type in ("function_definition", "async_function_definition"):
|
elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
|
||||||
name_node = node.child_by_field_name("name")
|
start_line = node.lineno
|
||||||
if name_node:
|
end_line = getattr(node, "end_lineno", start_line)
|
||||||
name = code[name_node.start_byte:name_node.end_byte]
|
prefix = "[Async Func]" if isinstance(node, ast.AsyncFunctionDef) else "[Func]"
|
||||||
start_line = node.start_point.row + 1
|
|
||||||
end_line = node.end_point.row + 1
|
|
||||||
prefix = "[Async Func]" if node_type == "async_function_definition" else "[Func]"
|
|
||||||
|
|
||||||
# Check if it's a method (parent is a class body)
|
# Check if it's a method
|
||||||
parent = node.parent
|
# We can check the indent or the parent, but in AST walk we know if we are inside a ClassDef
|
||||||
while parent and parent.type != "class_definition":
|
# Let's use a simpler heuristic for the outline: if indent > 0, it's likely a method.
|
||||||
if parent.type == "module":
|
if indent > 0:
|
||||||
break
|
|
||||||
parent = parent.parent
|
|
||||||
|
|
||||||
if parent and parent.type == "class_definition":
|
|
||||||
prefix = "[Method]"
|
prefix = "[Method]"
|
||||||
|
|
||||||
output.append(f"{' ' * indent}{prefix} {name} (Lines {start_line}-{end_line})")
|
output.append(f"{' ' * indent}{prefix} {node.name} (Lines {start_line}-{end_line})")
|
||||||
doc = get_docstring(node)
|
doc = get_docstring(node)
|
||||||
if doc:
|
if doc:
|
||||||
output.append(f"{' ' * (indent + 1)}\"\"\"{doc}\"\"\"")
|
output.append(f"{' ' * (indent + 1)}\"\"\"{doc}\"\"\"")
|
||||||
|
|
||||||
for child in node.children:
|
for node in tree.body:
|
||||||
# Don't recurse into function bodies for outlining functions,
|
walk(node)
|
||||||
# but we DO want to recurse into classes to find methods.
|
|
||||||
if node_type == "class_definition":
|
|
||||||
if child.type == "block":
|
|
||||||
walk(child, indent + 1)
|
|
||||||
elif node_type == "module":
|
|
||||||
walk(child, indent)
|
|
||||||
elif node_type == "block":
|
|
||||||
walk(child, indent)
|
|
||||||
|
|
||||||
walk(tree.root_node)
|
|
||||||
return "\n".join(output)
|
return "\n".join(output)
|
||||||
|
|
||||||
def get_outline(path: Path, code: str) -> str:
|
def get_outline(path: Path, code: str) -> str:
|
||||||
|
|||||||
@@ -107,3 +107,30 @@ def test_build_files_section_with_dicts(tmp_path):
|
|||||||
result = build_files_section(tmp_path, files_config)
|
result = build_files_section(tmp_path, files_config)
|
||||||
assert "content1" in result
|
assert "content1" in result
|
||||||
assert "file1.txt" in result
|
assert "file1.txt" in result
|
||||||
|
|
||||||
|
def test_tiered_context_by_tier_field():
|
||||||
|
file_items = [
|
||||||
|
{"path": Path("tier1_file.txt"), "entry": "tier1_file.txt", "content": "Full Tier 1 Content\nLine 2", "tier": 1},
|
||||||
|
{"path": Path("tier3_file.txt"), "entry": "tier3_file.txt", "content": "Full Tier 3 Content\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8\nLine 9\nLine 10", "tier": 3},
|
||||||
|
{"path": Path("other.txt"), "entry": "other.txt", "content": "Other Content\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8\nLine 9\nLine 10", "tier": None}
|
||||||
|
]
|
||||||
|
|
||||||
|
# Test Tier 1 Context
|
||||||
|
result_t1 = build_tier1_context(file_items, Path("."), [], [])
|
||||||
|
assert "Full Tier 1 Content" in result_t1
|
||||||
|
assert "Line 2" in result_t1 # In full
|
||||||
|
|
||||||
|
# tier3_file.txt should be summarized
|
||||||
|
assert "tier3_file.txt" in result_t1
|
||||||
|
assert "preview:" in result_t1
|
||||||
|
assert "Line 9" not in result_t1 # Only first 8 lines in preview
|
||||||
|
|
||||||
|
# Test Tier 3 Context
|
||||||
|
result_t3 = build_tier3_context(file_items, Path("."), [], [], focus_files=[])
|
||||||
|
assert "Full Tier 3 Content" in result_t3
|
||||||
|
assert "Line 10" in result_t3 # In full
|
||||||
|
|
||||||
|
# tier1_file.txt should be summarized
|
||||||
|
assert "tier1_file.txt" in result_t3
|
||||||
|
assert "preview:" in result_t3
|
||||||
|
assert "Full Tier 1 Content" in result_t3 # It's short, so it's in preview
|
||||||
|
|||||||
Reference in New Issue
Block a user