How dinesh1309/payroll-compliance-agent works
1. What this repo is
This is a payroll-compliance review system for hospitality batches. It inspects a submitted payroll run *before* payroll is sent, looking for tip-credit / tipped-minimum-wage problems, then computes dollars owed and drafts a human-readable correction.
2. Two entry points, one shared engine
The same review engine is used from both the browser and the terminal. The browser app lives in app/, while the CLI lives in src/cli.ts and can review data/sample-batch.json headlessly. That shared-core design is important: the UI is just a wrapper around the compliance pipeline.
3. Frontend / demo surface
app/page.tsx, app/Demo.tsx, and components like EditableGrid, Donut, and Pill make the review results visible and interactive. The UI is a Next.js App Router app, and app/api/review/route.ts is the server endpoint that likely bridges the browser trigger to the same review workflow. PostHogProvider suggests lightweight product analytics, but it is not part of the compliance logic.
4. Agent orchestration layer
The agent loop is centered in src/agent/orchestrator.ts, with supporting phase definitions in src/agent/phases.ts, triage logic in src/agent/triage.ts, and a human-approval boundary in src/agent/humanGate.ts. This layer decides *when* to run which checks, when to escalate, and when to stop for human review.
5. Deterministic compliance core
The legal/math core is in src/tools/. This includes loading batches, classifying worker roles, scanning anomalies, estimating liability, drafting corrections, and building the final report. The important architectural boundary is that these tools own the law and the arithmetic; they are not delegated to the model.
6. Rule/check subsystem
Specific payroll rules are implemented as checks under src/tools/checks/: tipCreditLegality, tipPool, makeupPay, notice, and eighty20, plus a shared _base.ts. These represent the actual compliance predicates the agent applies to each batch and are the main source of violation findings.
7. State-specific policy data
data/state-rules.json holds jurisdiction-specific rules, and src/tools/stateRules.ts loads them into the evaluation pipeline. That means the architecture is data-driven for state law variation, which is critical for a multi-location compliance product.
8. Batch ingestion and domain model
src/tools/loadBatch.ts and data/sample-batch.json represent the intake boundary for payroll data. The batch is scanned for anomalies and then normalized into the domain model consumed by the checks, liability estimator, and report builder.
9. LLM boundary
The model integration is isolated in src/llm/client.ts and src/llm/openrouter.ts. Per the README, OpenRouter is used only for judgment and language generation; if no API key is present, the system falls back to deterministic behavior. In other words, the LLM is an explanation layer, not a source of truth for compliance.
10. Output and human-in-the-loop flow
The pipeline culminates in a report (src/tools/buildReport.ts) that explains each issue in plain English and quantifies liability. The human gate ensures corrections are routed for approval rather than auto-applied, which is the key operational boundary in the architecture.
11. Runtime / deployment shape
The project is built on Next.js with Bun as the local/runtime tooling (bun.lock, bun run ...). Production is the standard Next build/start flow, and the README notes Vercel as the intended deployment target.
12. How to draw the diagram
A good architecture diagram would show: user or CLI input → Next.js/API or CLI entry point → agent orchestrator/phase loop → deterministic tools and state rules → optional OpenRouter LLM for wording/judgment → human approval gate → final compliance report/UI display.
Main entry points: orchestrator.ts Orchestrator · Demo.tsx Demo flow · loadBatch.ts Load batch · _base.ts Compliance checks
Components 21
Next.js app
Browser-facing review UI and API route.
- Review UI · Browser entry and main screen for the compliance review experience.
app/page.tsx - Demo flow · Interactive demo wrapper that feeds the review experience.
app/Demo.tsx - Review API · Server endpoint that invokes the shared review workflow from the browser.
app/api/review/route.ts - Editable Grid · Interactive batch editing surface used by the UI.
app/components/EditableGrid.tsx - Donut chart · Visual summary component for review results.
app/components/Donut.tsx - Pill badge · Compact status/label UI component.
app/components/Pill.tsx
Core engine
Shared agent, compliance tools, and CLI runtime.
- CLI entry · Headless terminal entry point for reviewing a batch.
src/cli.ts - Orchestrator · Coordinates phases, tool calls, escalation, and stop conditions.
src/agent/orchestrator.ts - Phases · Defines the review stages used by the orchestrator.
src/agent/phases.ts - Triage · Decides which checks or escalation paths to run.
src/agent/triage.ts - Human gate · Stops automated action and routes corrections for human approval.
src/agent/humanGate.ts - Load batch · Parses and normalizes payroll batch input into the shared domain model.
src/tools/loadBatch.ts - Compliance checks · Deterministic payroll-rule predicates for legality and wage violations.
src/tools/checks/_base.ts - Scan anomalies · Finds irregularities in the submitted batch before evaluation.
src/tools/scanAnomalies.ts - Liability · Computes dollars owed from detected violations.
src/tools/estimateLiability.ts - Draft correction · Generates a human-readable corrective action summary.
src/tools/draftCorrection.ts - Build report · Assembles the final plain-English compliance report.
src/tools/buildReport.ts - LLM client · Shared model integration layer with deterministic fallback behavior.
src/llm/client.ts - OpenRouter · OpenRouter-backed language and judgment provider.
src/llm/openrouter.ts
Policy data
Static batch samples and state-specific rules.
- State rules · Jurisdiction-specific wage and tip-credit rules.
data/state-rules.json - Sample batch · Representative payroll batch used for local review and demo flows.
data/sample-batch.json
Connections 21
- Review UI → Demo flow: renders · The page composes the demo experience.
- Demo flow → Editable Grid: edits · The demo allows batch rows to be modified before review.
- Demo flow → Donut chart: summarizes · The demo visualizes outcomes with charts.
- Demo flow → Pill badge: labels · Status and issue badges are shown in the UI.
- Review API → Orchestrator: runs · The API route triggers the shared agent workflow.
- CLI entry → Orchestrator: runs · The CLI invokes the same compliance engine headlessly.
- Orchestrator → Phases: orders · The orchestrator advances through defined review phases.
- Orchestrator → Triage: routes · Triage selects which checks and escalation paths apply.
- Orchestrator → Human gate: escalates · Potential corrections are paused for approval.
- Orchestrator → Load batch: ingests · Batch data is loaded before analysis begins.
- Load batch → Sample batch: sample input · The demo/CLI can start from the bundled batch fixture.
- Load batch → Scan anomalies: normalizes · Normalized batch data is scanned for irregularities.
- Scan anomalies → Compliance checks: feeds · Detected irregularities inform compliance evaluation.
- Compliance checks → State rules: applies · Checks use jurisdiction-specific policy data.
- Compliance checks → Liability: finds violations · Rule outcomes flow into liability estimation.
- Liability → Draft correction: drafts · Monetized findings are turned into a proposed correction.
- Draft correction → Build report: explains · The correction text becomes part of the final report.
- Build report → Review UI: displays · The browser UI presents the generated report.
- Build report → CLI entry: prints · The CLI can emit the report in terminal output.
- Orchestrator → LLM client: optionally uses · The agent may call the model for judgment or wording.
- LLM client → OpenRouter: backs onto · OpenRouter is the primary model provider implementation.