π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
name = "no-transactions-in-models"
builtInAgent = "basic-structured"
instruction = """
You are reviewing one file from the LobeHub database package's models layer (packages/database/src/models).
Team rule: the models layer holds one Model class per aggregate. An aggregate is a root table plus the rows it owns. When a write path composes SEVERAL AGGREGATES in one transaction β for example binding a project directory by upserting an environment, its instance, a project link and a directory row, each of which has a lifecycle of its own β that flow belongs in a Repository under packages/database/src/repositories, not in a Model. Models own their aggregate; repositories own cross-aggregate units of work and compose the models.
A table is part of THIS model's aggregate, so writing it together with the root is fine, when it is one of:
- owned child rows that have no meaning without the root row and are only written through it: a message's plugins / files / query chunks, an experiment's benchmark links, a flow's nodes and edges, a document's versions, a persona's history rows;
- an append-only event, audit, history or outbox row that records the change being made (`appendEvent`, `*_events`, `*_history`, `*_revisions`);
- the join / link row between this root and another entity, written when this root is created, re-linked or deleted (`agents_to_sessions`, `*_bindings`, `*_members` for this root);
- cascade cleanup of dependents while deleting this root (`delete` of rows keyed by the root id).
- the base row of a subtype table: when this model's table extends a shared base table one-to-one (each `user_memories_*` row pairs with a `user_memories` row), creating or deleting the pair is one entity;
- a derived rollup or counter on a parent, recomputed from this aggregate's rows (`recomputeTopicUsage`, `*Count`, `updatedAt` touches): it caches a fact this aggregate owns rather than composing the parent.
Report a Model method whose `this.db.transaction(...)` (or a `tx` / `trx` handle it receives) creates or updates the primary rows of ANOTHER aggregate β a table that has its own Model class or its own lifecycle (users, topics, tasks, agents, sessions, files, documents, projects, devices, environments, β¦) β beyond the exceptions above. Anchor the finding on the line where the transaction starts (the `.transaction(` call). In the message, name the method, this model's aggregate, and the other aggregate it writes. In the suggestion, name the Repository to extract the flow into, and which single-aggregate pieces can remain Model methods that the repository composes.
Do not report:
- a transaction whose extra tables all fall under the exceptions above;
- a table that is only read (`select`, joins, `for('update')` locks) inside the transaction: only insert / update / delete count as writes;
- read-only queries, including joins across many tables β reading widely is what models do;
- a transaction that writes only one table, such as a lock followed by an update of that same table, or a batch update / reorder of one table;
- single-table upserts (`onConflictDoNothing`, `onConflictDoUpdate`) without an explicit transaction;
- files outside the models layer (repositories, services, routers) β transactions are expected there.
If an exclusion applies, return no finding for that method.
If uncertain whether a write hits a second table, use low confidence rather than forcing a finding.
"""