Skip to content

Render API

The render module converts bundles to various output formats.

Overview

from bugsafe.bundle.reader import read_bundle
from bugsafe.render.markdown import render_markdown

# Load and render
bundle = read_bundle("crash.bugbundle")
markdown = render_markdown(bundle)
print(markdown)

Markdown Renderer

Render bundles as human-readable Markdown.

bugsafe.render.markdown.render_markdown(bundle)

Render a BugBundle as Markdown.

Parameters:

Name Type Description Default
bundle BugBundle

The bundle to render.

required

Returns:

Type Description
str

Markdown string.

JSON Export

bugsafe.render.json_export

JSON export - Generate JSON output and LLM-optimized context from bundles.

to_json(bundle, *, indent=2)

Export bundle as JSON string.

Parameters:

Name Type Description Default
bundle BugBundle

The bundle to export.

required
indent int

JSON indentation level.

2

Returns:

Type Description
str

JSON string representation.

Source code in src/bugsafe/render/json_export.py
def to_json(bundle: BugBundle, *, indent: int = 2) -> str:
    """Export bundle as JSON string.

    Args:
        bundle: The bundle to export.
        indent: JSON indentation level.

    Returns:
        JSON string representation.
    """
    return json.dumps(bundle.to_dict(), indent=indent, default=str, ensure_ascii=False)

to_llm_context(bundle, max_tokens=DEFAULT_MAX_TOKENS)

Generate LLM-optimized context from bundle.

Prioritizes: 1. Error message and traceback 2. Command and exit code 3. Relevant environment info 4. Truncated stdout/stderr if space allows

Parameters:

Name Type Description Default
bundle BugBundle

The bundle to render.

required
max_tokens int

Maximum token budget.

DEFAULT_MAX_TOKENS

Returns:

Type Description
str

LLM-optimized context string.

Source code in src/bugsafe/render/json_export.py
def to_llm_context(
    bundle: BugBundle,
    max_tokens: int = DEFAULT_MAX_TOKENS,
) -> str:
    """Generate LLM-optimized context from bundle.

    Prioritizes:
    1. Error message and traceback
    2. Command and exit code
    3. Relevant environment info
    4. Truncated stdout/stderr if space allows

    Args:
        bundle: The bundle to render.
        max_tokens: Maximum token budget.

    Returns:
        LLM-optimized context string.
    """
    sections: list[str] = []
    remaining_tokens = max_tokens

    header = _build_header(bundle)
    sections.append(header)
    remaining_tokens -= _estimate_tokens(header)

    if bundle.traceback:
        error_section = _build_error_section(bundle)
        error_tokens = _estimate_tokens(error_section)
        if error_tokens <= remaining_tokens:
            sections.append(error_section)
            remaining_tokens -= error_tokens

    env_section = _build_env_section(bundle)
    env_tokens = _estimate_tokens(env_section)
    if env_tokens <= remaining_tokens:
        sections.append(env_section)
        remaining_tokens -= env_tokens

    if bundle.capture.stderr and remaining_tokens > 100:
        stderr_budget = min(remaining_tokens // 2, 1000)
        stderr_truncated, _ = _truncate_to_tokens(bundle.capture.stderr, stderr_budget)
        stderr_section = f"## stderr\n```\n{stderr_truncated}\n```"
        sections.append(stderr_section)
        remaining_tokens -= _estimate_tokens(stderr_section)

    if bundle.capture.stdout and remaining_tokens > 100:
        stdout_truncated, _ = _truncate_to_tokens(
            bundle.capture.stdout, remaining_tokens - 50
        )
        stdout_section = f"## stdout\n```\n{stdout_truncated}\n```"
        sections.append(stdout_section)

    if bundle.redaction_report:
        redaction_note = _build_redaction_note(bundle)
        sections.append(redaction_note)

    return "\n\n".join(sections)