Assignment 4: Quality + Debugging#
Course: 06-643 Scientific Software Engineering with AI Coding Agents
Topics covered: L06 (Code Quality), L07 (Debugging and Profiling with AI)
In this assignment you will add code quality tooling to your scholartools package, practice debugging with an AI agent, and add type annotations to your codebase. The assignment has two components: a technical component (Parts 1–3) and a reflection (Part 4).
Part 1: Code Quality Tools#
Add code quality tooling to your scholartools package by completing the steps below.
1.1 Configure ruff for linting and formatting#
Add a [tool.ruff] section to your pyproject.toml. Choose appropriate rule sets (at minimum, enable the E, F, and I rule categories). Set a line length and any other options you find useful.
Show the relevant section of your pyproject.toml below.
# Show your [tool.ruff] configuration from pyproject.toml
# !cat /path/to/scholartools/pyproject.toml
1.2 Run ruff check and ruff format — show before/after#
Run ruff check . and ruff format . on your scholartools package. Show:
The before output (any linting errors or formatting changes ruff reports)
The after output (clean pass after fixing issues)
# Before: run ruff check and ruff format, show the output
# !cd /path/to/scholartools && ruff check .
# !cd /path/to/scholartools && ruff format --check .
# After: apply fixes and show clean output
# !cd /path/to/scholartools && ruff check . --fix
# !cd /path/to/scholartools && ruff format .
# !cd /path/to/scholartools && ruff check .
# !cd /path/to/scholartools && ruff format --check .
1.3 Set up pre-commit with hooks for ruff#
Install pre-commit and configure it with hooks for ruff (both linting and formatting). Run pre-commit install to activate the hooks in your repository.
Show the contents of your .pre-commit-config.yaml file.
# Show your .pre-commit-config.yaml
# !cat /path/to/scholartools/.pre-commit-config.yaml
1.4 Demonstrate a commit blocked by pre-commit#
Intentionally introduce a code quality violation (e.g., an unused import, inconsistent formatting), then attempt a commit. Show:
The blocked commit — pre-commit rejects it with ruff errors
The fix — correct the violation
The successful commit — pre-commit passes and the commit goes through
# Show the blocked commit attempt
# Example:
# $ echo "import os" >> scholartools/api.py
# $ git add scholartools/api.py
# $ git commit -m "test: trigger pre-commit failure"
# (paste output showing the failure)
# Show the fix and successful commit
# Example:
# $ ruff check --fix .
# $ git add scholartools/api.py
# $ git commit -m "chore: clean up unused import"
# (paste output showing success)
Part 2: Debug Buggy Code#
The following code has 3 bugs. Use an AI coding agent to help identify and fix them. Document your debugging process.
def analyze_citations(works):
"""Analyze citation patterns in a list of works.
Each work is a dict with keys: 'title', 'year', 'cited_by_count'
Returns summary statistics by year.
"""
yearly_stats = {}
for work in works:
year = work['year']
if year not in yearly_stats:
yearly_stats[year] = {'count': 0, 'total_citations': 0, 'titles': []}
yearly_stats[year]['count'] =+ 1
yearly_stats[year]['total_citations'] += work['cited_by_count']
yearly_stats[year]['titles'].append(work['title'])
for year in yearly_stats:
stats = yearly_stats[year]
stats['avg_citations'] = stats['total_citations'] / stats['count']
stats['max_citations'] = max(work['cited_by_count'] for work in works if work['year'] == year)
sorted_years = sorted(yearly_stats.keys(), reverse=True)
return {year: yearly_stats[year] for year in sorted_years}
def find_highly_cited(works, threshold=None):
"""Find works cited more than the threshold.
If threshold is None, use the mean citation count.
"""
if threshold is None:
threshold = sum(w['cited_by_count'] for w in works) / len(works)
result = []
for work in works:
if work['cited_by_count'] > threshold:
result.append(work)
result.sort(key=lambda w: w['cited_by_count'])
return result
Your tasks#
Identify the 3+ bugs. For each bug, describe:
Where it is (line number or code snippet)
What the incorrect behavior is
What the correct behavior should be
Show the fixed code.
Write tests that would have caught each bug.
Describe how you used the AI agent in the debugging process. What prompts did you give it? Did it find all the bugs immediately, or did you need to guide it?
Bug descriptions#
Describe each bug here:
Bug 1:
Bug 2:
Bug 3:
# Paste your fixed version of the code here
# Write tests that would have caught each bug
# You can use pytest-style tests or simple assert statements
# Example test data:
test_works = [
{'title': 'Paper A', 'year': 2020, 'cited_by_count': 10},
{'title': 'Paper B', 'year': 2020, 'cited_by_count': 20},
{'title': 'Paper C', 'year': 2021, 'cited_by_count': 5},
]
# Test for Bug 1:
# Test for Bug 2:
# Test for Bug 3:
AI debugging process#
Describe how you used the AI agent to find and fix the bugs. Include:
What prompts you gave the agent
Whether it found all bugs on the first attempt
Any back-and-forth that was needed
Part 3: Add Type Hints and Docstrings#
Use an AI coding agent to add proper type hints and documentation to your scholartools package.
3.1 Add type hints#
Using the typing module, add type hints to all public functions in your scholartools package. Show a representative example below (you do not need to paste every file, but the hints should be in your repository).
# Show a representative example of a function with type hints and a NumPy-style docstring
# Example:
#
# def search_works(query: str, per_page: int = 25) -> list[dict[str, Any]]:
# """Search for academic works via the OpenAlex API.
#
# Parameters
# ----------
# query : str
# The search query string.
# per_page : int, optional
# Number of results per page, by default 25.
#
# Returns
# -------
# list[dict[str, Any]]
# A list of work records returned by the API.
# """
3.2 Run a type checker#
Run mypy or pyright on your scholartools package and show the output. Fix any type errors the checker reports.
# Run the type checker and show the output
# !cd /path/to/scholartools && mypy scholartools/
# or
# !cd /path/to/scholartools && pyright scholartools/
# If there were type errors, show the fixes and the clean output after fixing
# !cd /path/to/scholartools && mypy scholartools/
Part 4: Reflection#
How did ruff change your coding habits?#
Your answer here.
How effective was the AI at finding the bugs? Did it find all of them immediately?#
Your answer here.
What is your debugging workflow now compared to before this course?#
Your answer here.
When do you trust AI-generated fixes vs. investigating yourself?#
Your answer here.