fix(agent): AgentInstanceRepository use DataSource directly, not TenantAwareRepository

agent_instances is in public schema — no tenant context needed.
Fixes 'Tenant context not initialized' when iAgent calls internal API via Bash.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-03-08 03:19:01 -07:00
parent 5c5c365736
commit b8979d521e
1 changed files with 13 additions and 20 deletions

View File

@ -1,43 +1,36 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm'; import { DataSource, Repository } from 'typeorm';
import { TenantAwareRepository } from '@it0/database';
import { AgentInstance } from '../../domain/entities/agent-instance.entity'; import { AgentInstance } from '../../domain/entities/agent-instance.entity';
@Injectable() @Injectable()
export class AgentInstanceRepository extends TenantAwareRepository<AgentInstance> { export class AgentInstanceRepository {
constructor(dataSource: DataSource) { private readonly repo: Repository<AgentInstance>;
super(dataSource, AgentInstance);
constructor(private readonly dataSource: DataSource) {
this.repo = dataSource.getRepository(AgentInstance);
} }
findAll(): Promise<AgentInstance[]> { findAll(): Promise<AgentInstance[]> {
return this.withRepository((repo) => return this.repo.find({ order: { createdAt: 'DESC' } });
repo.find({ order: { createdAt: 'DESC' } }),
);
} }
findById(id: string): Promise<AgentInstance | null> { findById(id: string): Promise<AgentInstance | null> {
return this.withRepository((repo) => return this.repo.findOne({ where: { id } as any });
repo.findOne({ where: { id } as any }),
);
} }
findByUserId(userId: string): Promise<AgentInstance[]> { findByUserId(userId: string): Promise<AgentInstance[]> {
return this.withRepository((repo) => return this.repo.find({ where: { userId } as any, order: { createdAt: 'DESC' } });
repo.find({ where: { userId } as any, order: { createdAt: 'DESC' } }),
);
} }
findByPoolServer(poolServerId: string): Promise<AgentInstance[]> { findByPoolServer(poolServerId: string): Promise<AgentInstance[]> {
return this.withRepository((repo) => return this.repo.find({ where: { poolServerId, status: 'running' } as any });
repo.find({ where: { poolServerId, status: 'running' } as any }),
);
} }
async save(instance: AgentInstance): Promise<AgentInstance> { save(instance: AgentInstance): Promise<AgentInstance> {
return this.withRepository((repo) => repo.save(instance)); return this.repo.save(instance);
} }
// Get all host ports in use on a specific server (across all tenants via public query) // Get all host ports in use on a specific server
async getUsedPortsOnServer(dataSource: DataSource, serverHost: string): Promise<number[]> { async getUsedPortsOnServer(dataSource: DataSource, serverHost: string): Promise<number[]> {
const rows = await dataSource.query( const rows = await dataSource.query(
`SELECT host_port FROM agent_instances WHERE server_host = $1 AND status != 'removed'`, `SELECT host_port FROM agent_instances WHERE server_host = $1 AND status != 'removed'`,