AI Agents SDE Task Viewer
      • Context
      • Plan
      • Prd
  1. Home
  2. AgentSDE
  3. agent-core
  4. gh-355
  5. plan
  6. plan.md
plan.md(3.3 KB)· Apr 8, 2026· 2 min read
  • Summary
  • Files
  • Steps
  • Verification
  • Risks

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#

FileActionDescription
src/database/entities/task.entity.tsmodifyAdd owner column, update unique constraint to ['owner', 'issue', 'repo']
src/database/entities/job.entity.tsmodifyAdd owner column with default 'AgentSDE'
src/database/entities/webhook-delivery.entity.tsmodifyAdd owner column with default 'AgentSDE'
src/task-state/task-state.service.tsmodifyInject TenantConfigService, scope all find/create queries by owner
src/task-state/task-state.module.tsmodifyImport TenantModule
src/queue/sqlite-job-queue.tsmodifyInject TenantConfigService, scope job creation and polling by owner
src/queue/queue.module.tsmodifyImport TenantModule
src/webhook/webhook.controller.tsmodifyInject TenantConfigService, set owner on delivery creation
src/webhook/webhook.module.tsmodifyImport TenantModule
src/task-state/task-state.service.spec.tsmodifyUpdate tests with owner in fixtures and assertions
src/queue/sqlite-job-queue.spec.tsmodifyUpdate tests with owner in fixtures and assertions

Steps#

  1. Add owner column to all three entities. Add @Column({ default: 'AgentSDE' }) owner!: string to TaskEntity, JobEntity, WebhookDeliveryEntity. Update TaskEntity unique constraint from ['issue', 'repo'] to ['owner', 'issue', 'repo'].
  2. Scope TaskStateService queries. Inject TenantConfigService, add owner to where clause in findByIssueAndRepo(), findByStatus(), findByStatuses(), findAll(). Set owner from tenantConfig.getConfig().org in createTask(). Import TenantModule in task-state.module.ts.
  3. Scope SqliteJobQueue queries. Inject TenantConfigService, add owner filter to enqueue() creation, processNext() QueryBuilder, and onModuleInit() stale-job recovery. Add owner to the subquery in processNext(). Import TenantModule in queue.module.ts.
  4. Scope WebhookDeliveryEntity creation. Inject TenantConfigService into WebhookController, set owner on deliveryRepo.create(). Import TenantModule in webhook.module.ts.
  5. Update unit tests. Add owner: 'AgentSDE' to entity fixtures in task-state.service.spec.ts and sqlite-job-queue.spec.ts. Mock TenantConfigService returning { org: 'AgentSDE' }. Verify scoped queries include owner.

Verification#

  • npm run test — all unit tests pass with owner-scoped queries
  • tsc --noEmit — no type errors
  • npm run lint — zero warnings

Risks#

  • Dependency on #348 (TenantModule): TenantConfigService must exist before this compiles. If #348 hasn't merged to rc/multi-tenant, this PR will fail CI until it does. Mitigated by targeting rc/multi-tenant branch where #348 will land first.
  • QueryBuilder in SqliteJobQueue: The processNext() raw subquery needs careful owner scoping in both the outer query and the inner NOT IN subquery to prevent cross-tenant job leakage.
ContextPrd