feat(mma): Finalize Phase 1 with AST-based outline and improved tiered selection

This commit is contained in:
2026-02-27 22:23:50 -05:00
parent f59ef247cf
commit 528aaf1957
3 changed files with 73 additions and 78 deletions

View File

@@ -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:
"""
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"}
@@ -217,13 +217,14 @@ def build_tier1_context(file_items: list[dict], screenshot_base_dir: Path, scree
path = item.get("path")
name = path.name if path else ""
if name in core_files:
if name in core_files or item.get("tier") == 1:
# Include in full
sections.append("### `" + (item.get("entry") or str(path)) + "`\n\n" +
f"```{path.suffix.lstrip('.') if path.suffix else 'text'}\n{item.get('content', '')}\n```")
else:
# 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))
@@ -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:
"""
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 = []
@@ -265,7 +266,7 @@ def build_tier3_context(file_items: list[dict], screenshot_base_dir: Path, scree
is_focus = True
break
if is_focus:
if is_focus or item.get("tier") == 3:
sections.append("### `" + (entry or path_str) + "`\n\n" +
f"```{path.suffix.lstrip('.') if path and path.suffix else 'text'}\n{item.get('content', '')}\n```")
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```")
except Exception as e:
# 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:
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))

View File

@@ -1,88 +1,55 @@
import tree_sitter
import tree_sitter_python
import ast
from pathlib import Path
class CodeOutliner:
def __init__(self):
self.language = tree_sitter.Language(tree_sitter_python.language())
self.parser = tree_sitter.Parser(self.language)
pass
def outline(self, code: str) -> str:
tree = self.parser.parse(bytes(code, "utf8"))
lines = code.splitlines()
code = code.lstrip(chr(0xFEFF))
try:
tree = ast.parse(code)
except SyntaxError as e:
return f"ERROR parsing code: {e}"
output = []
def get_docstring(node):
# In Python, docstring is usually the first expression statement in a block
body = node.child_by_field_name("body")
if body and body.type == "block":
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
doc = ast.get_docstring(node)
if doc:
return doc.splitlines()[0]
return None
def walk(node, indent=0):
node_type = node.type
name = None
if isinstance(node, ast.ClassDef):
start_line = node.lineno
end_line = getattr(node, "end_lineno", start_line)
output.append(f"{' ' * indent}[Class] {node.name} (Lines {start_line}-{end_line})")
doc = get_docstring(node)
if doc:
output.append(f"{' ' * (indent + 1)}\"\"\"{doc}\"\"\"")
for item in node.body:
walk(item, indent + 1)
if node_type == "class_definition":
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)
if doc:
output.append(f"{' ' * (indent + 1)}\"\"\"{doc}\"\"\"")
elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
start_line = node.lineno
end_line = getattr(node, "end_lineno", start_line)
prefix = "[Async Func]" if isinstance(node, ast.AsyncFunctionDef) else "[Func]"
elif node_type in ("function_definition", "async_function_definition"):
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
prefix = "[Async Func]" if node_type == "async_function_definition" else "[Func]"
# Check if it's a method
# We can check the indent or the parent, but in AST walk we know if we are inside a ClassDef
# Let's use a simpler heuristic for the outline: if indent > 0, it's likely a method.
if indent > 0:
prefix = "[Method]"
# Check if it's a method (parent is a class body)
parent = node.parent
while parent and parent.type != "class_definition":
if parent.type == "module":
break
parent = parent.parent
output.append(f"{' ' * indent}{prefix} {node.name} (Lines {start_line}-{end_line})")
doc = get_docstring(node)
if doc:
output.append(f"{' ' * (indent + 1)}\"\"\"{doc}\"\"\"")
if parent and parent.type == "class_definition":
prefix = "[Method]"
for node in tree.body:
walk(node)
output.append(f"{' ' * indent}{prefix} {name} (Lines {start_line}-{end_line})")
doc = get_docstring(node)
if doc:
output.append(f"{' ' * (indent + 1)}\"\"\"{doc}\"\"\"")
for child in node.children:
# Don't recurse into function bodies for outlining functions,
# 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)
def get_outline(path: Path, code: str) -> str:

View File

@@ -107,3 +107,30 @@ def test_build_files_section_with_dicts(tmp_path):
result = build_files_section(tmp_path, files_config)
assert "content1" 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