# The Workflow-Authoring Contract (MVP) _Captured 2026-06-17. The keystone: it defines what a "workflow" **is**, so a non-engineer can author one and the engine can run it. Everything else (UI, storage, jobs) hangs off this. Framed as a **general document-workflow product** — the audit register and the contract-calendar are just the first two use cases._ --- ## 0. The one idea There are **two layers**, and keeping them apart is the whole game: - **Layer 1 — the step library (engineers build, rarely).** A small set of typed building-blocks (today: *extract*, *enrich*). Robust, tested, versioned. - **Layer 2 — the workflow (a non-engineer composes, all the time).** A plain, declarative description that picks and configures those blocks and says what the output looks like. **New workflows = configuration (the product scales). New block types = occasional engineering (the platform deepens).** That is what makes it a product instead of a consulting engagement. --- ## 1. The mental model (plain) ``` A FOLDER has one WORKFLOW. Add a FILE to the folder → the workflow RUNS on it → one growing SHEET updates. ``` A workflow answers **three plain questions** — nothing more: 1. **What do I pull out of each document?** (the fields) 2. **What do I add from my own reference sheet?** (the lookup) 3. **What does each row of my output sheet look like?** (the columns) And the whole thing reads back as **one sentence** a CA can check at a glance: > **For each** *observation* in the report, **capture** its *title, grading, root cause, risk, recommendation, responsible person, target date*; **also add** the document's *client* and *period*, and from *Master data.xlsx* the *Partner* and *Industry*; **into** the *Audit Observations* sheet. If the user can read that sentence and nod, the workflow is right. That sentence **is** the workflow. --- ## 2. Core concepts | Concept | What it is | |---|---| | **Folder** | A named container. Has **one** workflow, the files added to it, and one output sheet. (One workflow per folder in the MVP — keep it simple.) | | **Workflow** | The authored description bound to the folder. A declarative document (stored in Postgres). | | **Step** | One typed building-block in the workflow (e.g. an *extract*). Steps run in order; each can use earlier steps' results. | | **Output sheet** | The one growing table the folder owns — the single source of truth. Lives as rows in Postgres, exports to xlsx. | | **Run** | One execution of the workflow over one file. An async job with status (queued → running → done / failed). | | **Test-run** | A *dry* run on a sample file that shows the would-be output **without writing anything**. The heart of the authoring experience (§6). | --- ## 3. How a document becomes rows (the data model) This is the part that has to be exactly right, because it's what every workflow is built from. **Step 0 — Parse (automatic, under the hood).** Every uploaded file is turned into text + page layout before anything else. The non-engineer never configures this — it just happens. **The `extract` step.** Pulls a set of fields out of the parsed document. It has one important choice — **how many of this thing are in the document?** - **`one`** — a single fact about the whole document (e.g. *client*, *period*). Becomes a value attached to every row. - **`many`** — a list (e.g. the *observations*). Each item becomes **one row** in the output sheet. Every extracted value carries its **provenance** — where in the document it was found (page + the exact text). And each extract step has one trust dial: - **`when_not_found: leave_blank_and_flag`** (default) — if it can't point to a real spot in the document, it leaves the cell empty and flags it. *It does not guess.* - **`when_not_found: best_effort`** — fill if reasonably inferable. (For workflows where a soft guess is fine.) **The `enrich` step.** Joins your records to a **reference sheet** you uploaded (e.g. the client master). You give it: which key to match on, which columns to pull in, and what to do on no-match (`leave_blank_and_flag` / `skip_row`). **The output definition.** Says what the sheet looks like: - **`row_source`** — which `many` step defines the rows (one row per item). - **`columns`** — each column's value comes from a step's field (`step_id.field`). - **The broadcast rule** — `one`-facts and joined columns automatically attach to **every** row. (This is how each observation row gets the client name and the Partner.) - **`file_ownership: by_filename`** — each file owns its rows; re-uploading the same name replaces them; deleting the file removes them. (Decided in the output-model brief.) --- ## 4. Layer 2 — the workflow definition This is what the authoring UI produces and Postgres stores. The user never types it by hand; they see the sentence/forms (§7). Shown here so the shape is unambiguous. ### Use case 1 — Audit Observations Register (fully expressed) ```yaml workflow: "Audit Observations Register" folder: "TACL Internal Audit" trigger: file_added steps: - id: doc_facts # one record for the whole document type: extract cardinality: one fields: [client, period, process] when_not_found: leave_blank_and_flag - id: observations # a list → one row each type: extract cardinality: many fields: - title - grading # enum: Critical | Major | Medium | Low - root_cause # enum: IT | BusinessProcess | People - risk_impact - recommendation - responsible_official - target_date # date when_not_found: leave_blank_and_flag - id: client_master # look up from the reference sheet type: enrich reference: master_clients # = the uploaded Master data.xlsx match: { left: doc_facts.client, right: "Customer Name", strategy: fuzzy } pull: [Partner, Location, Industry, Sub-sector] on_miss: leave_blank_and_flag output: row_source: observations # one row per observation file_ownership: by_filename columns: - { name: "Client", from: doc_facts.client } - { name: "Period", from: doc_facts.period } - { name: "Process", from: doc_facts.process } - { name: "Observation", from: observations.title } - { name: "Grading", from: observations.grading } - { name: "Root Cause", from: observations.root_cause } - { name: "Risk & Impact", from: observations.risk_impact } - { name: "Recommendation", from: observations.recommendation } - { name: "Responsible", from: observations.responsible_official } - { name: "Target Date", from: observations.target_date } - { name: "Partner", from: client_master.Partner } - { name: "Industry", from: client_master.Industry } - { name: "Location", from: client_master.Location } ``` ### Use case 2 — Contract Obligations (MVP-partial: the events as a table) ```yaml workflow: "Contract Obligations" folder: "Vendor Contracts" trigger: file_added steps: - id: obligations type: extract cardinality: many fields: [obligation_title, category, due_date, recurrence, trigger_logic] when_not_found: leave_blank_and_flag output: row_source: obligations file_ownership: by_filename columns: - { name: "Obligation", from: obligations.obligation_title } - { name: "Category", from: obligations.category } - { name: "Due Date", from: obligations.due_date } - { name: "Recurs", from: obligations.recurrence } - { name: "Trigger", from: obligations.trigger_logic } ``` **This is the proof the contract generalizes:** two very different jobs, same building-blocks, only configuration differs. UC2's calendar `.ics` and its human "resolve the blanks" step are *new block types added later* — they slot into the same `steps:` list without changing the engine. --- ## 5. Layer 1 — the step-type library (engineer-facing) A step type is a small plugin that declares: ``` StepType: type: "extract" | "enrich" | ... config_schema: what the author can configure (fields, cardinality, match, ...) input: what it reads from earlier steps output: the records it produces run(ctx, config, dry_run) -> records # MUST be side-effect-free ``` - **v1 library: `extract`, `enrich`.** That's all UC1 needs end-to-end. - **`parse` and `write` are engine built-ins**, not user-placed steps (parsing is automatic; writing is driven by the output definition). - **Adding a new block** (e.g. `compose` → generate an `.ics`/`.docx`; `human_review` → confirm/resolve before write; `filter`, `branch`) means registering a new `StepType`. The workflow definition and the engine don't change — this is the extensibility guarantee. --- ## 6. Running & test-running **Live run.** File added → a `Run` is created (`queued`). A worker does: parse → run steps in order, building a context `{ step_id: records }` → apply the output definition → **upsert** rows into the sheet (files own rows, keyed by filename). Run states: `queued → running → succeeded | failed`, with any flagged/blank fields surfaced. **Test-run (the USP).** Same execution in **dry mode** on a chosen sample file → produces the would-be rows, each cell showing its provenance and any blanks/flags → displayed as a preview table. **Nothing is written.** The author tweaks fields/mapping, re-runs, and only when it looks right do they **save** the workflow. > Design requirement this imposes: **every step must run with no side effects** (the only write is the final upsert, which dry-run skips). This is why "a workflow" is, by definition, *something you can safely dry-run.* --- ## 7. The authoring experience (how a Layer-2 definition actually gets made — the USP) The user never writes YAML. The definition is produced by: 1. **Templates first** — pick a pre-built workflow (e.g. "Audit Observations Register"); done in one click for common cases. 2. **AI-draft from a sample** — if none fits, drop in *one* example document + say in plain English what you want. The assistant proposes the steps, the fields, **and detects cardinality** (one client vs. many observations) and the column mapping → returns the editable sentence/forms. 3. **Edit the draft** — rename/remove/add fields, mark the trust dial. (Editing a draft, never a blank page.) 4. **Test-run** on the sample → see the real output → adjust → repeat. 5. **Save** → the workflow is bound to the folder and live. The metric we optimise: **how quickly a non-engineer gets to a workflow whose test-run output is correct.** --- ## 8. Deliberately deferred (NOT in the MVP) - **Block types:** `compose` (generate `.ics`/`.docx`/`.pdf`), `human_review`/resolve, `filter`, `branch`/conditionals. - **Output:** per-file output files, CSV/JSON/.ics exports, cross-name versioning (a per-workflow "same source" dedup key for Draft→Final under different names). - **Triggers:** anything other than *file added* (schedule, email-in, API). - **Multiple workflows per folder.** - **Auth / multi-tenant** (pre-rollout gate, not MVP-build). --- ## 9. Open questions to settle while building 1. **Execution: async job (recommended).** A 20-page PDF + LLM calls take seconds–minutes, so upload returns immediately and a worker runs it; the UI polls status. Confirm. 2. **Reference sheets:** uploaded once per workflow at setup and imported into a table (the `enrich` step points at it). Re-upload to refresh. (Assumed.) 3. **AI-draft model:** default to **Gemini** (consistent with both reference projects). Revisit if needed. 4. **Fuzzy client match** (report "Talbros...Ltd" vs master "Talbros...Limited"): the one genuinely hard bit. Start with normalized fuzzy match + show the matched name in the test-run so the author can catch a wrong match. Harden later. --- ## 10. Next step Scaffold the project against this contract: Postgres schema (folders, workflows, files, runs, sheet rows, reference tables), the engine (parse → steps → upsert + dry-run), the `extract`/`enrich` blocks, and a thin UI for the authoring loop. Build **UC1 end-to-end** first; UC2 validates the contract afterward.