|
1 | 1 | import type { AgentSpec } from '@truefoundry/trueforge-core/agent-session'; |
2 | | -import { sql, type Transaction } from 'kysely'; |
| 2 | +import { sql, type Kysely, type Transaction } from 'kysely'; |
3 | 3 | import { |
4 | 4 | type AgentRecord, |
5 | 5 | type CreateAgentInput, |
@@ -34,57 +34,55 @@ function toPutRemoteAgentPayload({ |
34 | 34 | }; |
35 | 35 | } |
36 | 36 |
|
37 | | -type TrueFoundryAgentInner<TTransaction> = IAgentStore<TTransaction> & { |
38 | | - withTransaction<T>(fn: (transaction: TTransaction) => Promise<T>): Promise<T>; |
39 | | -}; |
40 | | - |
41 | 37 | /** |
42 | 38 | * create: createDB(null) → putRemote → updateDB(external_id) | on put/update fail → deleteDB (+ deleteRemote if put ok) |
43 | 39 | * update: lock → get → putRemote(new) → updateDB | on DB fail → putRemote(old) | both fail → AggregateError |
44 | 40 | * delete: lock → get → deleteRemote(404 ok) → deleteDB |
45 | 41 | */ |
46 | | -export class TrueFoundryAgentStore< |
47 | | - TTransaction extends Transaction<Database> = Transaction<Database>, |
48 | | -> implements IAgentStore<TTransaction> { |
49 | | - readonly #inner: TrueFoundryAgentInner<TTransaction>; |
| 42 | +export class TrueFoundryAgentStore implements IAgentStore<Transaction<Database>> { |
| 43 | + readonly #inner: IAgentStore<Transaction<Database>>; |
50 | 44 | readonly #client: TrueFoundryServiceFoundryServerClient; |
51 | 45 | readonly #accessToken: string; |
| 46 | + readonly #db: Kysely<Database>; |
52 | 47 |
|
53 | 48 | constructor(input: { |
54 | | - inner: TrueFoundryAgentInner<TTransaction>; |
| 49 | + inner: IAgentStore<Transaction<Database>>; |
55 | 50 | client: TrueFoundryServiceFoundryServerClient; |
56 | 51 | accessToken: string; |
| 52 | + db: Kysely<Database>; |
57 | 53 | }) { |
58 | 54 | this.#inner = input.inner; |
59 | 55 | this.#client = input.client; |
60 | 56 | this.#accessToken = input.accessToken; |
| 57 | + this.#db = input.db; |
61 | 58 | } |
62 | 59 |
|
63 | | - listAgents(input: ListAgentsInput, transaction?: TTransaction): Promise<AgentRecord[]> { |
| 60 | + listAgents(input: ListAgentsInput, transaction?: Transaction<Database>): Promise<AgentRecord[]> { |
64 | 61 | return this.#inner.listAgents(input, transaction); |
65 | 62 | } |
66 | 63 |
|
67 | | - getAgent(input: GetAgentInput, transaction?: TTransaction): Promise<AgentRecord | undefined> { |
| 64 | + getAgent(input: GetAgentInput, transaction?: Transaction<Database>): Promise<AgentRecord | undefined> { |
68 | 65 | return this.#inner.getAgent(input, transaction); |
69 | 66 | } |
70 | 67 |
|
71 | 68 | // Serializes updates for this tenant ID and agent id. |
72 | 69 | // Prevents concurrent MCP writes from desyncing the remote agent. |
| 70 | + // Opens a txn via db when the caller omits transaction (does not require one). |
73 | 71 | #withUpdateLock<T>( |
74 | 72 | input: { tenant_id: string; id: string }, |
75 | | - transaction: TTransaction | undefined, |
76 | | - fn: (transaction: TTransaction) => Promise<T>, |
| 73 | + transaction: Transaction<Database> | undefined, |
| 74 | + fn: (transaction: Transaction<Database>) => Promise<T>, |
77 | 75 | ): Promise<T> { |
78 | | - const run = async (txn: TTransaction) => { |
| 76 | + const run = async (txn: Transaction<Database>) => { |
79 | 77 | const key = `tf:agent:${input.tenant_id}:${input.id}`; |
80 | 78 | await sql`SELECT pg_advisory_xact_lock(hashtext(${key}))`.execute(txn); |
81 | 79 | return fn(txn); |
82 | 80 | }; |
83 | | - return transaction !== undefined ? run(transaction) : this.#inner.withTransaction(run); |
| 81 | + return transaction !== undefined ? run(transaction) : this.#db.transaction().execute(run); |
84 | 82 | } |
85 | 83 |
|
86 | | - async createAgent(input: CreateAgentInput, transaction?: TTransaction): Promise<AgentRecord> { |
87 | | - // Lets the DB unique constraint pick one winner for this tenant ID and name. |
| 84 | + async createAgent(input: CreateAgentInput, transaction?: Transaction<Database>): Promise<AgentRecord> { |
| 85 | + // Unique (tenant_id, name) picks one winner even when transaction is omitted (auto-commit inserts). |
88 | 86 | // Prevents concurrent requests from both creating the same remote agent. |
89 | 87 | const created = await this.#inner.createAgent({ ...input, external_id: null }, transaction); |
90 | 88 |
|
@@ -123,7 +121,7 @@ export class TrueFoundryAgentStore< |
123 | 121 | } |
124 | 122 | } |
125 | 123 |
|
126 | | - async updateAgent(input: UpdateAgentInput, transaction?: TTransaction): Promise<AgentRecord | undefined> { |
| 124 | + async updateAgent(input: UpdateAgentInput, transaction?: Transaction<Database>): Promise<AgentRecord | undefined> { |
127 | 125 | const nextManifest = input.manifest; |
128 | 126 | if (nextManifest === undefined) { |
129 | 127 | // No manifest means only `external_id` changed; pass through to the inner store. |
@@ -169,7 +167,7 @@ export class TrueFoundryAgentStore< |
169 | 167 | }); |
170 | 168 | } |
171 | 169 |
|
172 | | - async deleteAgent(input: DeleteAgentInput, transaction?: TTransaction): Promise<void> { |
| 170 | + async deleteAgent(input: DeleteAgentInput, transaction?: Transaction<Database>): Promise<void> { |
173 | 171 | return this.#withUpdateLock(input, transaction, async txn => { |
174 | 172 | const previous = await this.#inner.getAgent({ tenant_id: input.tenant_id, id: input.id }, txn); |
175 | 173 | if (previous?.external_id) { |
|
0 commit comments