Skip to content

API Reference parrot_tools.business_automation.ingest

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_tools.business_automation.ingest

View source

Bank-statement Excel ingestion via ExecutionPlan (FEAT-453, Module 9, G7).

Builds a deterministic, LLM-free :class:~parrot.bots.flows.plan.ExecutionPlan that registers one expense per Excel row, through :class:~parrot.tools.execution_plan.toolkit.ExecutionPlanToolkit's zero-token tool-call DAG substrate.

Decision D3 (the hazard this module exists to defuse): two legitimate imports of different bank statements for the same period must never share a resumability identity. ImportRun.statement_digest (sha256 of the source bytes) is baked into every node's id/store_as at plan-build time, so a second import for the same period never collides with the first's working-memory keys.

Contract correction (documented per the anti-hallucination discipline — see this task's Completion Note for the full account): the task's Codebase Contract describes the D3 hazard in terms of parrot_tools.scraping.flow_executor._checkpoint_token — that mechanism belongs to the browser FlowExecutor (used by BusinessAutomationToolkit/TASK-2390), not to ExecutionPlanToolkit's ExecutionPlan/AgentsFlow substrate, which this module actually uses and which has no global_params field and explicitly disables flow-level checkpointing (FEAT-399, PlanMetadata.checkpoint=False at the toolkit). This module therefore (a) discriminates re-imports by baking the digest into node/working-memory identity instead of a nonexistent flow.global_params, and (b) maintains its own small, permission-hardened import manifest under ${PARROT_STATE_DIR} as the audit/reconciliation record this feature needs, independent of AgentsFlow's own (disabled) checkpointing.

Resume-without-duplicates (code-review remediation, AC-12): since AgentsFlow's own checkpointing is deliberately disabled (see above), per-row resumability lives entirely in this module's manifest instead. :func:make_import_progress_listener returns a sync (event, node_id, info) -> None callback matching AgentsFlow/ExecutionPlanToolkit's already-public on_node_event contract (parrot/bots/flows/flow/flow.py :422, parrot/tools/execution_plan/toolkit.py:103) — no core file is modified. The caller passes it to ExecutionPlanToolkit(on_node_event=make_import_progress_listener(...)) (or composes it via AgentsFlow.add_node_event_listener for a lower-level driver). On every "node_completed" event for one of this plan's row-<digest>-<i> nodes, the row index is appended to the manifest's completed_rows list — synchronously, before the flow advances to the next node, so a kill immediately after leaves that row durably marked done. :func:build_import_plan reads any existing manifest for the same statement_digest before building nodes and skips already-completed rows, so re-running it after a crash produces a plan containing only the remaining rows — never a duplicate registration.

Class ImportPlanBundle

The constructed :class:ExecutionPlan plus its :class:ImportRun discriminator (Decision D3) and row counts (for reconciliation).

Parameter Type Default Description
plan Optional[ExecutionPlan]
import_run ImportRun
row_count int
already_completed_rows int 0
fully_completed bool False

Fields:

Field Type Default Description
plan Optional[ExecutionPlan] The plan to run, containing only rows not yet completed. None when :attr:fully_completed is True — an ExecutionPlan cannot hold zero nodes (min_length=1), so a fully-resumed import returns no plan at all rather than constructing an invalid one.
import_run ImportRun The statement's ImportRun discriminator.
row_count int Total rows in the source statement (Decision D3's reconciliation unit) — unaffected by resume; always the original Excel row count.
already_completed_rows int 0 How many of row_count were already marked done in a prior manifest (0 on a first import).
fully_completed bool False True when every row was already completed — re-running :func:build_import_plan after the whole statement finished is a safe no-op, not an error.

Attributes:

Attribute Type Description
remaining_row_count int Rows the returned :attr:plan will actually register.

Alias ImportRun

Re-exported from parrot_tools.business_automation.models.ImportRun.

Function build_import_plan

Build a deterministic, LLM-free ExecutionPlan for a bank-statement import.

One :class:PlanNode per row, each invoking operation (via the run_operation tool — see :meth:~parrot_tools.business_automation.toolkit.BusinessAutomationToolkit.run_operation) with that row's values baked in as literal args (this function generates the plan fresh per import; there is no need for ExecutionPlan's {params.<name>}/{item...} runtime placeholders). Nodes are chained via depends_on — and PlanMetadata.max_parallel_tasks=1 set as a second line of defense — so the import runs strictly sequentially over the one authenticated browser session the underlying flow drives.

build_import_plan(xlsx_path: Union[str, Path], *, period: str, operation: str = 'register_expense', client_column: str = 'client', amount_column: str = 'amount') -> ImportPlanBundle

Parameter Type Default Description
xlsx_path Union[str, Path] Path to the bank-statement Excel file.
period str Accounting period label (e.g. "2026-Q1") — a human identifier, not itself a uniqueness guarantee (Decision D3: two statements can share a period).
operation str 'register_expense' The :class:~parrot_tools.business_automation.models.BusinessOperation name to invoke per row (default "register_expense").
client_column str 'client' Excel column holding the client name.
amount_column str 'amount' Excel column holding the expense amount.

Returns: ImportPlanBundle — class:ImportPlanBundle with the plan (only not-yet-completed rows — see :func:make_import_progress_listener), its ImportRun, and the row counts (for reconciliation via func:reconcile).

Function checkpoint_dir_for

Return (creating if needed) the checkpoint directory for operation.

${PARROT_STATE_DIR}/business_automation/checkpoints/<operation>/, directory mode 0700 (Decision D3) — never inside the Obsidian vault or a wiki storage root, both of which are mirrored/ingested surfaces.

checkpoint_dir_for(operation: str) -> Path

Parameter Type Default Description
operation str

Returns: Path

Function compute_statement_digest

Return a stable digest of the source Excel file's bytes.

Decision D3: this discriminates two legitimate imports of different statements for the same period — without it, the second import would resume/collide with the first's working-memory keys and silently skip every row it (wrongly) believes is already done.

compute_statement_digest(xlsx_path: Union[str, Path]) -> str

Parameter Type Default Description
xlsx_path Union[str, Path]

Returns: str

Attribute logger

Function make_import_progress_listener

Build an AgentsFlow/ExecutionPlanToolkit-compatible on_node_event listener that records per-row completion (AC-12).

Pass the returned callback to ExecutionPlanToolkit(on_node_event=...) (or AgentsFlow.add_node_event_listener) when executing the :class:ExecutionPlan from :func:build_import_plan for this exact (operation, digest) pair — no core file is modified; both extension points already accept an arbitrary sync/async (event, node_id, info) callback (parrot/bots/flows/flow/flow.py:422, parrot/tools/execution_plan/toolkit.py:103).

On every "node_completed" event for one of this plan's row-<digest>-<i> nodes, appends i to the manifest's completed_rows — synchronously, so a process kill immediately after this callback returns still leaves that row durably marked done. Events for any other node id (a different plan/digest sharing the same listener registry) are ignored. Never raises: a manifest write failure is logged and swallowed, matching this ecosystem's "telemetry must never break the run" contract.

make_import_progress_listener(operation: str, digest: str) -> Callable[[str, str, Dict[str, Any]], None]

Parameter Type Default Description
operation str The :class:~parrot_tools.business_automation.models.BusinessOperation name this import registers against (selects the checkpoint dir).
digest str The statement's statement_digest (selects the manifest).

Returns: Callable[[str, str, Dict[str, Any]], None] — A sync (event, node_id, info) -> None callback.

Function reconcile

Compare rows-in vs registrations-out and report the delta.

registrations_out counts only registrations from running this bundle's plan; rows already completed by a prior (resumed-from) run are added in automatically via :attr:ImportPlanBundle.already_completed_rows, so reconciliation always reflects the whole statement, not just the remainder a resumed plan actually executed.

reconcile(bundle: ImportPlanBundle, registrations_out: int) -> Dict[str, Any]

Parameter Type Default Description
bundle ImportPlanBundle The :class:ImportPlanBundle returned by :func:build_import_plan.
registrations_out int Number of register_expense (or equivalent) invocations that actually succeeded in this run.

Returns: Dict[str, Any] — {"rows_in", "registrations_out", "delta", "reconciled"} — reconciled is True only when delta == 0.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally