MT-11: Add owner column to entities + scope all DB queries#
Summary#
Add an owner column (defaulting to 'AgentSDE') to TaskEntity, JobEntity, and WebhookDeliveryEntity, then scope all create/query operations to the current tenant via TenantConfigService.getConfig().org. This enables multi-tenant data isolation in a single agent-core instance.
Files#
| File | Action | Description |
|---|---|---|
src/database/entities/task.entity.ts | modify | Add owner column, update unique constraint to ['owner', 'issue', 'repo'] |
src/database/entities/job.entity.ts | modify | Add owner column with default 'AgentSDE' |
src/database/entities/webhook-delivery.entity.ts | modify | Add owner column with default 'AgentSDE' |
src/task-state/task-state.service.ts | modify | Inject TenantConfigService, scope all find/create queries by owner |
src/task-state/task-state.module.ts | modify | Import TenantModule |
src/queue/sqlite-job-queue.ts | modify | Inject TenantConfigService, scope job creation and polling by owner |
src/queue/queue.module.ts | modify | Import TenantModule |
src/webhook/webhook.controller.ts | modify | Inject TenantConfigService, set owner on delivery creation |
src/webhook/webhook.module.ts | modify | Import TenantModule |
src/task-state/task-state.service.spec.ts | modify | Update tests with owner in fixtures and assertions |
src/queue/sqlite-job-queue.spec.ts | modify | Update tests with owner in fixtures and assertions |
Steps#
- Add
ownercolumn to all three entities. Add@Column({ default: 'AgentSDE' }) owner!: stringtoTaskEntity,JobEntity,WebhookDeliveryEntity. UpdateTaskEntityunique constraint from['issue', 'repo']to['owner', 'issue', 'repo']. - Scope TaskStateService queries. Inject
TenantConfigService, addownertowhereclause infindByIssueAndRepo(),findByStatus(),findByStatuses(),findAll(). SetownerfromtenantConfig.getConfig().orgincreateTask(). ImportTenantModuleintask-state.module.ts. - Scope SqliteJobQueue queries. Inject
TenantConfigService, addownerfilter toenqueue()creation,processNext()QueryBuilder, andonModuleInit()stale-job recovery. Addownerto the subquery inprocessNext(). ImportTenantModuleinqueue.module.ts. - Scope WebhookDeliveryEntity creation. Inject
TenantConfigServiceintoWebhookController, setownerondeliveryRepo.create(). ImportTenantModuleinwebhook.module.ts. - Update unit tests. Add
owner: 'AgentSDE'to entity fixtures intask-state.service.spec.tsandsqlite-job-queue.spec.ts. MockTenantConfigServicereturning{ org: 'AgentSDE' }. Verify scoped queries includeowner.
Verification#
npm run test— all unit tests pass with owner-scoped queriestsc --noEmit— no type errorsnpm run lint— zero warnings
Risks#
- Dependency on #348 (TenantModule):
TenantConfigServicemust exist before this compiles. If #348 hasn't merged torc/multi-tenant, this PR will fail CI until it does. Mitigated by targetingrc/multi-tenantbranch where #348 will land first. - QueryBuilder in SqliteJobQueue: The
processNext()raw subquery needs carefulownerscoping in both the outer query and the innerNOT INsubquery to prevent cross-tenant job leakage.