Skip to content

Fix: ignore blank lines in parser (Resolves #32)#33

Open
Thareesha98 wants to merge 1 commit into
wso2:mainfrom
Thareesha98:fix/issue-32-ignore-blank-lines
Open

Fix: ignore blank lines in parser (Resolves #32)#33
Thareesha98 wants to merge 1 commit into
wso2:mainfrom
Thareesha98:fix/issue-32-ignore-blank-lines

Conversation

@Thareesha98

Copy link
Copy Markdown

Purpose

When an .afm.md file has one or more blank lines before the opening ---, both interpreters ignore the whole frontmatter. Because of this, the model block is dropped and the interpreter silently falls back to its default provider (OpenAI in Python, WSO2 in Ballerina), which then fails with a confusing, unrelated error (e.g. "No API key found"). The provider the user actually wrote is never used.

Resolves #32

Goals

  • Make both interpreters ignore any leading blank lines and read the frontmatter correctly.
  • Make sure a file that declares a provider (e.g. ollama) actually uses that provider, even if it starts with a blank line.
  • Keep the change small and not touch the spec.

Approach

The root cause is that both parsers only look at the first line for the --- delimiter, so a blank first line makes them think there is no frontmatter.

  • Python (packages/afm-core/src/afm/parser.py): in extract_raw_frontmatter, skip leading blank lines before checking for the opening ---. This function is shared by the AFM parser and the skills parser, so both are fixed.
  • Ballerina (ballerina-interpreter/parser.bal): in parseAfm, drop leading blank lines from the content before detecting the frontmatter.

Both changes are a no-op for normal files (where --- is already the first line), so existing behaviour is unchanged. A regression test was added in each interpreter that parses a file starting with blank lines and confirms the model block is read correctly.

No UI changes.

User stories

  • As an AFM author, when my file accidentally starts with a blank line before ---, the interpreter still reads my frontmatter and uses the model/provider I declared, instead of silently using a default one.

Release note

Fixed AFM files that begin with blank lines before the --- frontmatter being parsed as having no frontmatter, which caused the declared model provider to be ignored. Leading blank lines are now skipped in both the Python and Ballerina interpreters.

@Thareesha98 Thareesha98 requested a review from MaryamZi as a code owner June 10, 2026 19:24
@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0802224f-6d6b-466f-9676-08c27dc2d179

📥 Commits

Reviewing files that changed from the base of the PR and between 9c1fd1c and 553d429.

📒 Files selected for processing (4)
  • ballerina-interpreter/parser.bal
  • ballerina-interpreter/tests/agent_test.bal
  • python-interpreter/packages/afm-core/src/afm/parser.py
  • python-interpreter/packages/afm-core/tests/test_parser.py

📝 Walkthrough

Overview

This PR fixes a parser issue where leading blank lines in .afm.md files prevented frontmatter detection, causing declared model providers to be ignored and interpreters to fall back to defaults.

Changes

Ballerina Interpreter

  • ballerina-interpreter/parser.bal: Modified the parseAfm function to strip leading blank lines from file content before checking for the YAML frontmatter delimiter (---), ensuring frontmatter is correctly detected regardless of leading whitespace.
  • ballerina-interpreter/tests/agent_test.bal: Added regression test testParseAfmIgnoresLeadingBlankLines that verifies AFM files with leading blank lines are parsed correctly and model metadata is extracted.

Python Interpreter

  • packages/afm-core/src/afm/parser.py: Updated extract_raw_frontmatter to skip leading blank or whitespace-only lines before locating the opening --- delimiter, then correctly extracts the frontmatter content between the opening and closing delimiters.
  • packages/afm-core/tests/test_parser.py: Added regression test test_parse_frontmatter_with_leading_blank_lines that validates correct frontmatter parsing when AFM content starts with blank lines.

Impact

  • AFM files that begin with blank lines before the frontmatter delimiter are now parsed correctly
  • Declared model providers (e.g., ollama) are properly recognized and used
  • No impact on files where --- is already the first non-whitespace element
  • No UI or specification changes

Walkthrough

This pull request fixes a bug in both Ballerina and Python AFM parsers where frontmatter was ignored if the document began with blank lines. The Ballerina implementation preprocesses the resolved content by stripping leading blank lines before computing line splits and detecting the opening --- delimiter. The Python implementation similarly advances past leading whitespace before searching for the opening delimiter, then locates the closing --- and extracts YAML content from between the delimiters. Both implementations include regression tests verifying that metadata extraction succeeds with leading blank lines present.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: fixing blank line handling in the parser across both Python and Ballerina interpreters.
Description check ✅ Passed The description covers purpose, goals, approach, user story, and release notes with clear detail. Several template sections are not applicable (UI changes, training, certification, marketing, samples, migrations) which is appropriate for this fix.
Linked Issues check ✅ Passed The PR successfully addresses issue #32 by implementing blank line handling in both interpreters' parsers as required, with regression tests confirming the fix works.
Out of Scope Changes check ✅ Passed All changes are directly scoped to issue #32: parser modifications in Python and Ballerina, plus corresponding regression tests. No unrelated alterations detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Frontmatter is ignored when the file starts with a blank line before ---

1 participant