Skip to content

Source body

source_body

SourceBodyHook — symbol body extraction with line numbers.

Protocol hook that extracts the full source body of one or more symbols, returning file path, start/end lines, and complete source code. Registered as ast:source-body via axm.hooks entry point.

Designed for protocol agents that need to Edit a symbol directly from the briefing without a preceding Read.

SourceBodyHook dataclass

Extract the full source body of one or more symbols.

Reads path from params (or working_dir from context) and symbol from params. When symbol contains newline characters, each line is processed independently.

Source code in packages/axm-ast/src/axm_ast/hooks/source_body.py
Python
@dataclass
class SourceBodyHook:
    """Extract the full source body of one or more symbols.

    Reads ``path`` from *params* (or ``working_dir`` from context)
    and ``symbol`` from *params*.  When *symbol* contains newline
    characters, each line is processed independently.
    """

    def execute(self, context: dict[str, object], **params: object) -> HookResult:
        """Execute the hook action.

        Args:
            context: Session context dictionary.
            **params: Must include ``symbol`` (name(s) to extract).
                Optional ``path`` (overrides ``working_dir`` from context).

        Returns:
            HookResult with ``symbols`` list in metadata on success.
        """
        symbol, working_dir, error = _validate_source_body_params(
            context,
            params,
        )
        if error:
            return HookResult.fail(error)
        assert symbol is not None
        assert working_dir is not None

        try:
            return _run_extraction(symbol, working_dir)
        except Exception as exc:  # noqa: BLE001
            return HookResult.fail(f"Source body extraction failed: {exc}")
execute(context, **params)

Execute the hook action.

Parameters:

Name Type Description Default
context dict[str, object]

Session context dictionary.

required
**params object

Must include symbol (name(s) to extract). Optional path (overrides working_dir from context).

{}

Returns:

Type Description
HookResult

HookResult with symbols list in metadata on success.

Source code in packages/axm-ast/src/axm_ast/hooks/source_body.py
Python
def execute(self, context: dict[str, object], **params: object) -> HookResult:
    """Execute the hook action.

    Args:
        context: Session context dictionary.
        **params: Must include ``symbol`` (name(s) to extract).
            Optional ``path`` (overrides ``working_dir`` from context).

    Returns:
        HookResult with ``symbols`` list in metadata on success.
    """
    symbol, working_dir, error = _validate_source_body_params(
        context,
        params,
    )
    if error:
        return HookResult.fail(error)
    assert symbol is not None
    assert working_dir is not None

    try:
        return _run_extraction(symbol, working_dir)
    except Exception as exc:  # noqa: BLE001
        return HookResult.fail(f"Source body extraction failed: {exc}")