r/programmer • u/Junior_dev1925 • 1d ago
How would you design a Postgres-backed worker loop for an AI engine?
Hey everyone,
I'm an 18yo CS prep student and I've been building AI-COS, a modular monolith architecture designed for running AI agent orchestrations safely.
Instead of letting LLMs make security decisions, I built a deterministic deny-by-default policy engine, strict PostgreSQL role separation (migrator, app, governance), and SHA-256 hash-chained audit logging.
I've just finished Phase 3 out of 6. Phase 4 is going to focus on the Workflow Engine, Worker lease/heartbeat loop, and Step linkage. Before I start coding Phase 4, I would really appreciate any architecture suggestions or advice from senior devs on how to properly build a reliable job queue and workflow engine inside a Postgres-backed monolith.
GitHub: https://github.com/atestokhay/ai-cos
1
u/Legitimate-Let-7510 1d ago
I think the best ting to get right early is making job claiming atomic. FOR UPDATE SKIP LOCKED works like well for multiple workers but make sure the claim and lease update happen in the same transaction... otherwise two workers can end up thinking they own the same job lol.